整个字母


14

整个字母

在此挑战中,您很难记住字母。为了避免这种情况,您可以上下移动字母,直到找到字母为止。

因为您希望代码具有可移植性,所以将使用字母块来编写代码。您的字母块数量有限,因为大多数字母块都被盗了,因此您需要确保代码尽可能短。

例子

输入/输出对由空白行分隔:

Ac
ABc

Ad
ABcd

fA
fedCBA

adB
abcdcB


Hello, World!
HGfefghijkllmno, WVUTSrqpopqrqponmlkjihgfed!

挑战

您的目标是将相邻字母与A-Za-z它们之间所有的字母中间字母()链接在一起。如果大小写不同,则应在中间转换大小写。如果资本不能在中间平均转换,则在中间之后分解。如果字符不是字母字符,则不应进行任何转换。

获奖

这是因此以字节为单位的最短代码胜出!

-10%奖金:如果您的代码链接数字


1
字母拼写是什么意思?
LegionMammal978 2015年

@ LegionMammal978 字母块。与挑战并不完全相关,只是我想出短代码的一个随机原因
Downgoat 2015年

好的,只是想知道您是否意味着受限来源
LegionMammal978 2015年

根据您的规则,您不认为adB应该转换为,abcdCB因为c位于d和b的中间。
geokavel 2015年

与我的“加密之间的字母 ”非常相似,但这已经获得了两倍的选票,所以我只标记我的名字。
阶段

Answers:



2

Python 2中,303 291 288 282 276 261 253字节

这与Hannes Karppila的算法完全不同,经过多次打高尔夫球,我在长度上取得了很大的进步。我认为该算法也可能允许使用其他语言中最短的代码之一,尤其是具有do-while循环和内置signum函数的语言。欢迎提出进一步改进的建议。(有些事情告诉我,整个内部循环应该重写为列表理解。)

l=map(ord,list(raw_input()));f=q=1
while q:
 q=0;m=~-f/2;c=m
 while abs(c)<len(l)-1:
  u=c+f;d=(l[u]-96)%32-(l[c]-96)%32
  if chr(l[c]).isalpha()*chr(l[u]).isalpha()*(d*d>1):l[:u-m]+=[l[c]+d/abs(d)];u+=f;q=1
  c=u
 f=-f
print "".join(map(chr,l))

1

JavaScript(ES6),198 197 194字节

f=s=>(o="",a=u=0,[...s].map(c=>{j=c.toUpperCase();p=j==c;b=j<"A"|j>"Z"?0:j.charCodeAt();for(i=0,m=a<b?b-a:a-b;a&&b&&++i<m;)o+=String.fromCharCode(i*(a<b||-1)+a+32*!(i>m/2?p:u));a=b;u=p;o+=c}),o)

用法

f("Hello, World!")
=> "HGfefghijkllmno, WVUTSrqpopqrqponmlkjihgfed!"

说明

f=s=>(
  o="",                                   // o = output string
  a=                                      // a = previous character code (or 0 if symbol)
    u=0,                                  // u = 1 if previous character was upper-case
  [...s].map(c=>{                         // iterate through each letter of input

    // Get information about the current character
    j=c.toUpperCase();                    // j = current character in upper-case
    p=j==c;                               // p = current character is upper-case
    b=j<"A"|j>"Z"?0:j.charCodeAt();       // b = current character code (or 0 if symbol)

    // Interpolate characters (unless A or B is a symbol)
    for(i=0,m=a<b?b-a:a-b;a&&b&&++i<m;)   // loop for each character between A and B
      o+=String.fromCharCode(             // add interpolated character to output
        i*(a<b||-1)+a+                    // interpolate character code
          32*!(i>m/2?p:u)                 // apply case of the nearest character
      );

    // Set character A values to B for the next character
    a=b;
    u=p;
    o+=c                                  // add B itself to the output

  }),
  o                                       // return the output
)

1
使用\w将失败,并带有数字。试试“ 09”
edc65 2015年

使用不带参数的charCodeAt()保存1个字符
edc65

并节省2个字符,避免使用Math.abs a>b?a-b:b-a...,还有更多其他“标准”技巧可以缩短javascript。使用插值方法,您可以击败我的得分。检查此站点中的提示
edc65


谢谢(你的)信息!我仍然喜欢编码高尔夫。:)
user81655

1

的JavaScript ES6,168(186-10%)176 193

编辑修改后可获得10%的奖励

使用兼容EcmaScript 6的浏览器测试运行以下代码段(我使用FireFox)

f=s=>[...s].map(c=>{a=parseInt(c,36),m=(a-q)/(d=a>q?1:-1);for(n=1;m&&(a>9)==(q>9)&&(q+=d)!=a;n+=2)r=q.toString(36),o+=n<m&p<'a'|n>=m&c<'a'?r.toUpperCase():r;p=c,q=a,o+=c},o='',p=q=-f)&&o

// Explained
U=s=>(
  o = '', // initialize output
  p = '', // provious char, initialize to none
  q = NaN, // previous char code, initialize to none
  [...s].map( c => { // for each char 
    a = parseInt(c,36), // convert digit/letter to numeric code, case invariant, NaN if invalid
    d = a > q ? 1 : -1, // sign of difference (if not equal)
    m = (a - q) / d; // absolute value of difference or NaN 
    if (m && (a>9)==(q>9)) // if current and prev are different and both alpha or both digits  
      for( n = 1; 
          (q += d) != a; // loop from prev char (not included) to current (not included)
           n += 2)
        r=q.toString(36),
        // add intermediate char to output
        // upcase if: left side & prev is upcase or right side and current is upcase
        o+= n<m&p<'a'|n>=m&c<'a'?r.toUpperCase():r;
    p = c, // copy current to previous
    q = a, // copy current to previous
    o += c // add current char to ouput
  }),
  o
)  

// test
console.log=(...x)=>O.innerHTML+=x+'\n'

;['Ac','Ad','fA','adB','04aQ27','Hello World!'].
forEach(x=>console.log(x + ' -> ' + f(x)))
<pre id=O></pre>


0

Python 2,349字节

时间太长,但至少是第一时间。

f=lambda p:ord(p.lower())
u=lambda p:"".join(p).upper()
s=raw_input()
w=s[0]
r=w
for q in s[1:]:
 o=q+w
 if q==w:o=""
 if o.isalpha():
  m=(f(w)<f(q))*2-1
  e=map(chr,range(f(w)+m,f(q)+m,m))
  if o==u(o):e=u(e)
  elif q==u(q):e[len(e)/2:]=u(e[len(e)/2:])
  elif -o.islower()+1:e[:len(e)/2]=u(e[:len(e)/2])
  r+="".join(e)
 else:
  r+=q
 w=q
print r
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.