알파벳을 가로 질러


14

알파벳을 가로 질러

이 도전에서는 알파벳 글자를 기억하는 데 어려움이 있습니다. 이것을 피하기 위해, 당신은 편지에 도착할 때까지 알파벳을 위아래로 이동합니다.

코드를 이식 가능하게하려면 문자 블록으로 작성해야합니다. 대부분의 문자가 도난 되었기 때문에 문자 블록의 양이 제한되어 있으므로 가능한 한 코드가 짧아야합니다.

입력 / 출력 쌍은 빈 줄로 구분됩니다.

Ac
ABc

Ad
ABcd

fA
fedCBA

adB
abcdcB


Hello, World!
HGfefghijkllmno, WVUTSrqpopqrqponmlkjihgfed!

도전

당신의 목표는 인접한 문자를 알파벳의 중간 문자 ( A-Za-z)로 묶는 것입니다. 대문자가 다른 경우 대문자로 변환해야합니다. 중간에 대문자를 균등하게 변환 할 수 없으면 중간 이후 에 분할됩니다. 문자가 알파벳 문자가 아닌 경우 변환을 수행하지 않아야합니다.

승리

이것은 이므로 바이트 단위의 가장 짧은 코드가 승리합니다!

-10 % 보너스 : 코드 체인 숫자


1
문자 블록이란 무엇입니까?
LegionMammal978

@ LegionMammal978 문자 블록 . 도전과는 관련이
없고

자, 제한된 소스 를 의미하는지 궁금합니다 .
LegionMammal978

규칙에 따라 c가 d와 b의 중간에 있기 때문에 adB변환해야 한다고 생각하지 마십시오 abcdCB.
geokavel

알파벳 사이의 암호화 와 매우 유사 하지만 이미 투표 수의 두 배가되었으므로 플래그를 지정합니다.

답변:



2

파이썬 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

자바 스크립트 (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

인수없이 charCodeAt ()를 사용하여 1 개의 문자 저장
edc65

그리고 Math.abs를 피하면서 2 문자를 절약하십시오 a>b?a-b:b-a... 그리고 자바 스크립트를 단축하는 더 많은 '표준'트릭이 있습니다. 보간법을 사용하면 내 점수를 이길 수 있습니다. 이 사이트의 힌트를 확인하십시오
edc65


정보 주셔서 감사합니다! 여전히 코드 골프가 끊어지고 있습니다. :)
user81655 1

1

자바 스크립트 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

파이썬 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
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.