请按照下面给出的示例输入和输出,使用最短的源代码回答一个程序,该程序将任意明文转换为相应的密文。奖励积分*表示最短的CPU时间或最少的内存使用量。

示例1:

明文:快速的棕色狐狸跳过懒狗。 Supercalifragilisticexpialidocious!

Ciphertext: eTh kiquc nobrw xfo smjup rvoe eth yalz .odg!uioiapeislgriarpSueclfaiitcxildcos

示例2:

明文:123 1234 12345 123456 1234567 12345678 123456789

密文: 312 4213 53124 642135 7531246 86421357 975312468

规则:

  1. 标点符号定义为包含在最接近的单词中。
  2. 单词的中心定义为上限((strlen(word)+1)/ 2)。
  3. 忽略(或折叠)空格。
  4. 奇怪的话首先向右移动。甚至单词首先向左移动。
  5. 您可以将其视为向后读取所有其他字符(从单词的结尾开始),然后是剩余的字符向前。公司= <!> gt; XoXpXrXtXoX = <!> gt; niaorCoprto。

    感谢那些指出我说明中不一致的人。这导致你们许多人走上了错误的道路,我为此道歉。规则#4应该清理。

    *只有杰夫阿特伍德决定这样做才能获得奖励积分。由于我没有跟他核实过,机会很小。遗憾。

有帮助吗?

解决方案

Python,50个字符

输入i

' '.join(x[::-2]+x[len(x)%2::2]for x in i.split())

处理自己的IO的替代版本:

print ' '.join(x[::-2]+x[len(x)%2::2]for x in raw_input().split())

如果包含空格,则总共66个字符。 (从技术上讲,如果从命令行运行,则可以省略print,因为默认情况下,代码的计算值显示为输出。)


使用reduce的备用版本:

' '.join(reduce(lambda x,y:y+x[::-1],x) for x in i.split())

59个字符。

<=>:

中输入的原始版本(偶数和奇数都先右转)
' '.join(x[::2][::-1]+x[1::2]for x in i.split())

48个字符,包括空格。

另一个替代版本(虽然稍微长一点)效率稍高:

' '.join(x[len(x)%2-2::-2]+x[1::2]for x in i.split())

(53个字符)

其他提示

J ,58个字符

>,&.>/({~(,~(>:@+:@i.@-@<.,+:@i.@>.)@-:)@<:@#)&.><;.2,&' '

Haskell ,64个字符

unwords.map(map snd.sort.zip(zipWith(*)[0..]$cycle[-1,1])).words

好吧,好吧,76如果你加入必要的<!>“import List <!>”;

Python - 69个字符

(包括空格和换行符)

这可以处理所有I / O.

for w in raw_input().split():
 o=""
 for c in w:o=c+o[::-1]
 print o,

Perl,78个字符

输入$_。如果这是不可接受的,请在开头为$_=<>;$_=$s;添加六个字符。换行符仅用于阅读。

for(split){$i=length;print substr$_,$i--,1,''while$i-->0;
print"$_ ";}print $/

C,140个字符

格式很好:

main(c, v)
  char **v;
{
  for( ; *++v; )
  {
    char *e = *v + strlen(*v), *x;
    for(x = e-1; x >= *v; x -= 2)
      putchar(*x);
    for(x = *v + (x < *v-1); x < e; x += 2)
      putchar(*x);
    putchar(' ');
  }
}

压缩:

main(c,v)char**v;{for(;*++v;){char*e=*v+strlen(*v),*x;for(x=e-1;x>=*v;x-=2)putchar(*x);for(x=*v+(x<*v-1);x<e;x+=2)putchar(*x);putchar(32);}}

的Lua

130 char函数,147 char函数程序

Lua在代码高尔夫中没有得到足够的爱 - 也许是因为当你拥有像function / endif / then / <pre></pre>这样的长关键词时,很难编写一个简短的程序,等

首先我以详细的方式编写函数并附带解释,然后我将其重写为压缩的独立函数,然后在命令行指定的 single 参数上调用该函数。

我必须使用<=>标签格式化代码,因为Markdown在格式化Lua方面做得非常糟糕。

从技术上讲,你可以通过内联函数获得一个较小的运行程序,但这种方式更加模块化:)

t = "The quick brown fox jumps over the lazy dog. Supercalifragilisticexpialidocious!"
T = t:gsub("%S+", -- for each word in t...
                  function(w) -- argument: current word in t
                    W = "" -- initialize new Word
                    for i = 1,#w do -- iterate over each character in word
                        c = w:sub(i,i) -- extract current character
                        -- determine whether letter goes on right or left end
                        W = (#w % 2 ~= i % 2) and W .. c or c .. W
                    end
                    return W -- swap word in t with inverted Word
                  end)


-- code-golf unit test
assert(T == "eTh kiquc nobrw xfo smjup rvoe eth yalz .odg !uioiapeislgriarpSueclfaiitcxildcos")

-- need to assign to a variable and return it,
-- because gsub returns a pair and we only want the first element
f=function(s)c=s:gsub("%S+",function(w)W=""for i=1,#w do c=w:sub(i,i)W=(#w%2~=i%2)and W ..c or c ..W end return W end)return c end
--       1         2         3         4         5         6         7         8         9        10        11        12        13
--34567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
-- 130 chars, compressed and written as a proper function

print(f(arg[1]))
--34567890123456
-- 16 (+1 whitespace needed) chars to make it a functioning Lua program, 
-- operating on command line argument

输出:

$ lua insideout.lua 'The quick brown fox jumps over the lazy dog. Supercalifragilisticexpialidocious!'
eTh kiquc nobrw xfo smjup rvoe eth yalz .odg !uioiapeislgriarpSueclfaiitcxildcos

我还是Lua的新手,所以如果有的话,我想看一个更短的解决方案。


对于所有args到stdin的最小密码,我们可以做111个字符:

for _,w in ipairs(arg)do W=""for i=1,#w do c=w:sub(i,i)W=(#w%2~=i%2)and W ..c or c ..W end io.write(W ..' ')end

但是这种方法确实会像其他一些解决方案一样输出尾随空格。

输入s

f=lambda t,r="":t and f(t[1:],len(t)&1and t[0]+r or r+t[0])or r
" ".join(map(f,s.split()))

Python,90个字符,包括空格。

TCL

125个字符

set s set f foreach l {}
$f w [gets stdin] {$s r {}
$f c [split $w {}] {$s r $c[string reverse $r]}
$s l "$l $r"}
puts $l

Bash - 133,假设输入是$ w变量

漂亮

for x in $w; do 
    z="";
    for l in `echo $x|sed 's/\(.\)/ \1/g'`; do
        if ((${#z}%2)); then
            z=$z$l;
        else
            z=$l$z;
        fi;
    done;
    echo -n "$z ";
done;
echo

压缩

for x in $w;do z="";for l in `echo $x|sed 's/\(.\)/ \1/g'`;do if ((${#z}%2));then z=$z$l;else z=$l$z;fi;done;echo -n "$z ";done;echo

好的,所以它会输出一个尾随空格。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top