ตัวอักษรหรือ N8s


10

เลขย่อ (ยังเป็นที่รู้จักกันในชื่อ "หดตัวเลข") คือเมื่อเป็นคำที่สั้นลงโดยใช้ตัวเลข วิธีการหดตัวทั่วไปคือการใช้ความยาวของสตริงย่อยที่ถูกแทนที่เพื่อแทนที่ทั้งหมดยกเว้นอักขระตัวแรกและตัวสุดท้าย ตัวอย่างเช่นใช้i18nแทนinternationalizationหรือL10nlocalizationแทน (ตัวLพิมพ์ใหญ่นั้นเป็นตัวพิมพ์เล็กเนื่องจากตัวพิมพ์เล็กมีลักษณะคล้ายกับ1)

แน่นอนว่าคำหลายคำในวลีเดียวกันอาจมีรูปแบบย่อเหมือนกันดังนั้นงานของคุณคือการแปลงกลุ่มคำเป็นตัวเลขหรือในกรณีที่มีคำต่างๆ ที่มีตัวเลขเหมือนกันโปรแกรมของคุณควรให้ ผลลัพธ์ของA7s R4tย่อมาจากAmbiguous Result(ใช่ฉันรู้ว่าผลลัพธ์นี้เป็นผลลัพธ์ที่คลุมเครือ)

กฎ:

  • ใช้โปรแกรมหรือฟังก์ชั่นแล้วพิมพ์หรือส่งคืนผลลัพธ์
  • อินพุตถูกใช้เป็นสตริงเดี่ยว
  • เอาต์พุตเป็นสตริงของคำที่คั่นด้วยช่องว่าง
  • โปรแกรมของคุณไม่จำเป็นต้องแปลงคำที่มีความยาว 3 และไม่ควรแปลงคำที่สั้นกว่านี้
  • หากl(ell ตัวพิมพ์เล็ก) จะเกิดขึ้นก่อน1(หนึ่ง) มันควรจะเป็นตัวพิมพ์ใหญ่
  • หากI(ตาตัวพิมพ์ใหญ่) จะเกิดขึ้นก่อน1(หนึ่ง) ควรเป็นตัวพิมพ์เล็ก
  • อินพุตจะเป็น ASCII ที่พิมพ์ได้และช่องว่าง คำถูกคั่นด้วยช่องว่าง
  • รหัสที่สั้นที่สุดชนะ

ตัวอย่าง:

A7s R4t -> A7s R4t (OR: A1s R1t, etc)
Ambiguous Result -> A7s R4t
Billy goats gruff -> B3y g3s g3f
Thanks for the Memories -> T4s f1r the M6s (one possible answer, NOT: Thnks fr th Mmrs)
Programming Puzzles & Code Golf -> P9g P5s & C2e G2f
globalization -> g11n
localizability -> L12y
Internationalization or antidisestablishmentarianism -> i18n or a26m
Internationalization or InternXXXXXalization -> A7s R4t
what is this fiddle and faddle -> A7s R4t
A be see -> A be s1e (OR: A be see)
see sea -> s1e s1a (OR: see sea)
2B or not 2B -> 2B or not 2B (OR: 2B or n1t 2B. 2 letters, don't change, don't count as ambiguous)
this example is this example -> t2s e5e is t2s e5e (same words aren't ambiguous)
l1 -> l1 (2 letters, don't change.)
I1 -> I1 (2 letters, don't change.)

แก้ไข: หากใครไม่ได้รับการอ้างอิง: Thnks fr th Mmrs


อินพุตสามารถมีตัวเลขได้หรือไม่? ถ้าเป็นเช่นนั้นการป้อนข้อมูลของl1กลายเป็นL1หรืออยู่เป็นl1?
Doorknob

ผมเข้าใจจากการเน้นที่แตกต่างกันว่าexample exampleจะเป็นe5e e5eแต่มันจะดีที่จะรวมถึงกรณีทดสอบซึ่งครอบคลุมนี้
Peter Taylor

เพิ่มกรณีทดสอบสำหรับคำถามทั้งสองข้อของคุณ ( 2B or not 2Bและหลังจากนั้น)
mbomb007

คำตอบ:


1

J, 92 ไบต์

(' 'joinstring(<&(]`(rplc&('I1';'i1')&(rplc&('l1';'L1'))&({.,([:":#-2:),{:))@.(#>3:))&>&;:))

สายโซ่ของการโทรที่สงบ ส่วนแรกเป็นเพียงทางแยก: ตัวอักษรjoinstringและฟังก์ชั่น (ใน J 1 (10 + +) 2คือ(10 + (1 + 2))) ส่วนที่สองคือฟังก์ชั่น การrplcเรียกทั้งสองครั้งจะแทนที่ I / l เมื่อพวกเขาอาจสับสน &คือการจัดองค์ประกอบและการเขียนโอเปอเรเตอร์ที่มีค่าแกงกะหรี่ ( 3&+ให้ฟังก์ชันที่คาดว่าจะเพิ่มอาร์กิวเมนต์ที่ 2) ในที่สุดส่วนสุดท้ายคือฟังก์ชั่นที่ใช้ครั้งแรกความยาว -2 (ประกอบด้วย stringification) และสุดท้าย &>&;:บิตที่ปลายประกอบด้วยฟังก์ชั่นนี้ (ซึ่งควรนำไปใช้กับทุกอาร์กิวเมนต์) ไป>(unboxing) เพราะ;:(แยกคำ) ส่งกลับรายการกล่อง (เพื่อให้องค์ประกอบทุกคนสามารถมีความยาวแตกต่างกัน)

ตัวอย่าง:

     (' 'joinstring(<&(]`(rplc&('I1';'i1')&(rplc&('l1';'L1'))&({.,([:":#-2:),{:))@.(#>3:))&>&;:)) 'hey you baby Iooooooooneey I1'
hey you b2y i11y I1

4

Caché ObjectScriptขนาด 231 ไบต์

r(a,b) s o=$REPLACE(o,a,b) q
z(s) f i=1:1:$L(s," ") s u=$P(s," ",i),l=$L(u),o=$S(l<4:u,1:$E(u)_(l-2)_$E(u,l)) d:l>3 r("I1","i1"),r("l1","L1") d  g:r z+4
    . i '(l<4!(v(o)=u!'$D(v(o)))) s r=1 q
    . s v(o)=u,t=t_o_" "
    q t
    q "A7s R4t"

นี่จะเป็น MUMPS ที่เป็นไปตามมาตรฐานที่ดีของ ol ถ้าไม่ใช่สำหรับการ$REPLACEโทรที่น่ารำคาญซึ่งไม่ได้เป็นส่วนหนึ่งของมาตรฐาน การนำมันกลับมาใช้ใหม่ในบริสุทธิ์ M นั้นมีขนาด 80 ไบต์ที่ดีดังนั้นฉันจึงไม่ลงเส้นทางนั้น

จุดเริ่มต้นคือ$$z("your string here")สิ่งที่จะกลับ"y2r s4g h2e"มาและอื่น ๆ


3
คุณอาจจะเชื่อมโยงกับภาษาได้ไหม? ฉันไม่เคยได้ยินเลย
mbomb007

ฉันหมายถึงมันเป็นสิ่งที่เกิดขึ้นเมื่อคุณหามัน ( docs.intersystems.com/cache20152/csp/docbook/… ) - รสชาติที่เป็นกรรมสิทธิ์ของ MUMPS
senshin

3

C #, 280 274 ไบต์

นักกอล์ฟครั้งแรกที่นี่! เมื่อไม่นานมานี้มีความสุขกับการอ่านและฉันคิดว่าฉันอาจลองเอง! อาจไม่ใช่ทางออกที่ดีที่สุด แต่ก็ดี!

class B{static void Main(string[] a){string[] n=Console.ReadLine().Split(' ');string o="";int j,i=j=0;for(;j<n.Length;j++){int s=n[j].Length;n[j]=((s<4)?n[j]:""+n[j][0]+(s-2)+n[j][s-1])+" ";o+=n[j];for(;i<j;i++)if(n[j]==n[i]){o="A7s R4t";j=n.Length;}}Console.WriteLine(o);}}

สิ่งเดียวกันไม่ดี

class B
{
    static void Main(string[] a)
    {            
        string[] n = Console.ReadLine().Split(' ');
        string o = "";
        int j, i = j = 0;
        for(; j < n.Length;j++)
        {
            int s = n[j].Length;

            n[j] = ((s<4) ? n[j] : "" + n[j][0] + (s - 2) + n[j][s - 1]) + " ";
            o += n[j];
            for (; i < j; i++)
            {
                if (n[j] == n[i]) { o = "A7s R4t"; j=n.Length;}
            }                              
        }
        Console.WriteLine(o);
    }
}

ขอบคุณเพื่อน!


ยินดีต้อนรับสู่ PPCG! c:
Deusovi

2

Perl, 131 120 ไบต์

ฉันได้เพิ่มไบต์สำหรับการใช้-pสวิตช์:

s/\B(\w+)(\w)/length($1)."$2_$1"/ge;$_="A7s R4t\n"if/(\w\d+\w)(\w+)\b.*\1(?!\2)/;s/_\w+//g;s/I1\w/\l$&/g;s/l1\w/\u$&/g;

คำอธิบาย

# Replace internal letters with count, but keep them around for the next test.
s/\B(\w+)(\w)/length($1)."$2_$1"/ge;
# Detect ambiguous result
$_ = "A7s R4t\n" if
    # Use negative look-ahead assertion to find conflicts
    /(\w\d+\w)(\w+)\b.*\1(?!\2)/;
# We're done with the internal letters now
s/_\w+//g;
# Transform case of initial 'I' and 'l', but only before '1'
s/I1\w/\l$&/g;
s/l1\w/\u$&/g;

ไม่แน่ใจว่าถูกต้องหรือไม่ แต่ฉันคาดว่า“ l” ใน“ like” จะไม่ได้รับตัวพิมพ์ใหญ่เมื่อเปลี่ยนเป็น“ l2e” ตามที่ไม่ได้ตามด้วย“ 1”
จัดการ

ผ่านการทดสอบเพียงไม่กี่กรณี แต่ดูเหมือนจะทำงาน: →s/(\w)(\w+)(\w)/$1.length($2)."$3_$2"/ge s/\B(\w+)(\w)/length($1)."$2_$1"/ge
จัดการ

ขอบคุณ @manatwork ฉันมองข้ามความต้องการของ casewap [Il]มาก่อน1- บันทึกไว้สองสามไบต์เช่นกัน!
Toby Speight

1

JavaScript (ES6), 165 ไบต์

s=>(r=s.replace(/\S+/g,w=>(m=w.length-2)<2?w:(i=d.indexOf(n=((m+s)[0]<2&&{I:"i",l:"L"}[w[0]]||w[0])+m+w[m+1]))>=0&d[i+1]!=w?v=0:d.push(n,w)&&n,v=d=[]),v?r:"A7s R4t")

คำอธิบาย

s=>(                         // s = input string
  r=s.replace(               // r = result of s with words replaced by numeronyms
    /\S+/g,                  // match each word
    w=>                      // w = matched word
      (m=w.length-2)         // m = number of characters in the middle of the word
        <2?w:                // if the word length is less than 4 leave it as it is
      (i=d.indexOf(          // check if this numeronym has been used before
        n=                   // n = numeronymified word
          ((m+s)[0]<2&&      // if the number starts with 1 we may need to replace
            {I:"i",l:"L"}    //     the first character with one of these
              [w[0]]||w[0])+ // return the replaced or original character
          m+w[m+1]
      ))>=0&d[i+1]!=w?       // set result as invalid if the same numeronym has been
        v=0:                 //     used before with a different word
      d.push(n,w)&&n,        // if everything is fine return n and add it to the list
    v=                       // v = true if result is valid
      d=[]                   // d = array of numeronyms used followed by their original word
  ),
  v?r:"A7s R4t"              // return the result
)

ทดสอบ


1

JavaScript ES6, 162

w=>(v=(w=w.split` `).map(x=>(l=x.length-2+'')>1?((l[0]>1||{I:'i',l:'L'})[x[0]]||x[0])+l+x[-~l]:x)).some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j]))?'A7s R4t':v.join` `

น้อย golfed

// Less golfed
f=w=>{
  w = w.split` ` // original text splitted in words
  v = w.map(x=> { // build modified words in array v

    l = x.length - 2 // word length - 2
    if (l > 1) // if word length is 4 or more
    {
      a = x[0] // get first char of word
      l = l+'' // convert to string to get the first digit
      m = l[0] > 1 || {I:'i', l:'L'} // only if first digit is 1, prepare to remap I to i and l to L
      a = m[a] || a // remap
      return a + l + x[-~l] // note: -~ convert back to number and add 1
    }
    else
      return x // word unchanged
  })
  return v.some((a,i)=>v.some((b,j)=>a==b&w[i]!=w[j])) // look for equals Numeronyms on different words
  ? 'A7s R4t' 
  : v.join` `
}  

ทดสอบ


1

Python 2, 185 ไบต์

d={}
r=''
for w in input().split():
 l=len(w);x=(w[0]+`l-2`+w[-1]).replace('l1','L1').replace('I1','i1')
 if l<4:x=w
 if d.get(x,w)!=w:r='A7s R4t';break
 d[x]=w;r+=x+' '
print r.strip()

1

Python 3, 160

ฉันชอบที่จะหาวิธีที่ดีในการแทนที่การโทรเหล่านั้น

def f(a):y=[(x,(x[0]+str(len(x)-2)+x[-1]).replace('l1','L1').replace('I1','i1'))[len(x)>3]for x in a.split()];return('A7s R4t',' '.join(y))[len(set(y))==len(y)]

ในบางกรณีการทดสอบ:

assert f('Billy goats gruff') == 'B3y g3s g3f'
assert f('Programming Puzzles & Code Golf') == 'P9g P5s & C2e G2f'
assert f('Internationalization or InternXXXXXalization') == 'A7s R4t'

1

ตัวคูณ48 35 ไบต์ไม่ใช่การแข่งขัน

มันเป็นแลมบ์ดาที่เกิดขึ้นในกองซ้อนซึ่งไม่เป็นไปตามข้อกำหนดทางเทคนิคที่ยากลำบากอย่างน่ารำคาญจริงๆที่ฉันพยายามมองข้าม

[ " " split [ a10n ] map " " join ]

ใช้englishคำศัพท์

หรือถ้าเราอินไลน์a10nคำในไลบรารีดังนั้น 131 ไบต์ (พร้อมการนำเข้าอัตโนมัติ):

: a ( b -- c ) " " split [ dup length 3 > [ [ 1 head ] [ length 2 - number>string ] [ 1 tail* ] tri 3append ] when ] map " " join ;

สิ่งนี้จัดการกับสิ่งที่ฉัน / L และ A7s R4t หรือไม่?
Robert Fraser

@ RobertFraser ฉันแน่ใจว่ามันเป็นอย่างนั้นแม้ว่าฉันจะไม่ได้เป็นผู้รวบรวมปัจจัยในนาทีนี้ ฉันจะเพิ่มการทดสอบหน่วยผ่านเมื่อฉัน : D
cat

ใช่ขอโทษเกี่ยวกับข้อกำหนด หนึ่งสัปดาห์หลังจากที่ฉันทำคำถามเสร็จฉันก็เหมือน "ไม่ฉันทำอะไรไปแล้ว" แต่มันก็สายเกินไป ฉันอาจทำให้รุ่นที่ง่ายกว่าเป็นความท้าทายที่แยกต่างหาก
mbomb007
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.