ข้ามตัวอักษร


14

ข้ามตัวอักษร

ในการท้าทายนี้คุณมีปัญหาในการจดจำตัวอักษรของตัวอักษร ในการหลีกเลี่ยงสิ่งนี้คุณต้องขึ้นและลงอักษรจนกว่าจะถึงตัวอักษร

เนื่องจากคุณต้องการให้รหัสของคุณเป็นแบบพกพาคุณจะเขียนด้วยตัวอักษรบล็อก คุณมีบล็อกจำนวน จำกัด เนื่องจากส่วนใหญ่ถูกขโมยดังนั้นคุณต้องแน่ใจว่ารหัสของคุณสั้นที่สุด

ตัวอย่าง

คู่อินพุต / เอาต์พุตถูกคั่นด้วยบรรทัดว่าง:

Ac
ABc

Ad
ABcd

fA
fedCBA

adB
abcdcB


Hello, World!
HGfefghijkllmno, WVUTSrqpopqrqponmlkjihgfed!

ท้าทาย

เป้าหมายของคุณคือการA-Za-zเชื่อมโยงตัวอักษรที่อยู่ติดกันด้วยตัวอักษรกลางทั้งหมดของตัวอักษร ( ) ระหว่างพวกเขา หากตัวพิมพ์ใหญ่แตกต่างกันการแปลงเป็นตัวพิมพ์ใหญ่ควรจะอยู่ตรงกลาง หากโครงสร้างเงินทุนไม่สามารถเปลี่ยนอย่างสม่ำเสมอในช่วงกลางก็เสียขึ้นหลังจากกลาง หากตัวละครไม่ใช่ตัวอักษรตัวอักษรไม่ควรทำการแปลงใด ๆ

การชนะ

นี่คือสั้นที่สุดในหน่วยไบต์!

โบนัส -10%:หากรหัสของคุณเชื่อมโยงหลัก


1
คุณหมายถึงอะไรโดยบล็อกตัวอักษร?
LegionMammal978

@ LegionMammal978 บล็อกจดหมาย ไม่เกี่ยวข้องกับความท้าทายเพียงแค่เหตุผลสุ่มที่ฉันได้รับพร้อมกับรหัสสั้น ๆ
Downgoat

โอเคแค่สงสัยว่าคุณหมายถึงแหล่งที่ถูก จำกัด หรือไม่หรือไม่
LegionMammal978

ตามกฎของคุณคุณไม่คิดว่าadBควรจะเปลี่ยนเป็นabcdCBเพราะ c อยู่ตรงกลางของ d และ b
geokavel

ค่อนข้างคล้ายกับตัวอักษรของฉันระหว่างการเข้ารหัสแต่ตอนนี้มีสองคะแนนแล้วดังนั้นฉันจะทำเครื่องหมายของฉัน
ระยะเวลา

คำตอบ:



2

Python 2, 303 291 288 282 276 261 253 ไบต์

นี่เป็นอัลกอริทึมที่แตกต่างอย่างสิ้นเชิงจาก Hannes Karppila และหลังจากเล่นกอล์ฟมาหลายครั้งฉันก็สามารถพัฒนาความยาวได้อย่างมาก ฉันคิดว่าอัลกอริทึมนี้อาจอนุญาตให้หนึ่งในรหัสที่สั้นที่สุดในภาษาอื่น ๆ เช่นกันโดยเฉพาะอย่างยิ่งภาษาที่มีฟังก์ชั่นลูปและ do-while ในตัว คำแนะนำสำหรับการปรับปรุงเพิ่มเติมยินดีต้อนรับ (มีบางอย่างบอกฉันว่าวงในทั้งหมดควรถูกเขียนใหม่เพื่อความเข้าใจในรายการ)

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

ประหยัด 1 ถ่านโดยใช้ charCodeAt () โดยไม่มีอาร์กิวเมนต์
edc65

และบันทึก 2 ตัวอักษรที่หลีกเลี่ยง Math.abs a>b?a-b:b-a... และยังมีลูกเล่นอื่น ๆ ด้วยวิธีการแก้ไขของคุณคุณสามารถเอาชนะคะแนนของฉัน ตรวจสอบคำแนะนำในเว็บไซต์นี้
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
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.