ตีกอล์ฟชื่อผู้ใช้ PPCG


32

ความคิดเห็นที่ฉันทำในการแชทและการสนทนาที่ตามมาเป็นแรงบันดาลใจให้ฉันเพื่อให้ความท้าทายนี้

ฉันเป็นคนเดียวที่อ้างอิงโดยชื่อย่อแถว ๆ นี้หรือไม่? เราทุกคนต่างก็เล่นกอล์ฟกัน เราสามารถมี MB และ D-nob และ ... O.

ถ้าฉันรู้จัก "CH" ฉันก็คิดว่าทุกคนควรจะมีชื่อเล่นเบื้องต้นด้วยเช่นกัน

นี่คือรายชื่อของผู้ใช้ 100 อันดับปริศนาเพื่อการเขียนโปรแกรมและรหัสกอล์ฟที่มีชื่อเสียงในการเล่นกับ:

Martin Büttner
Doorknob
Peter Taylor
Howard
marinus
Dennis
DigitalTrauma
David Carraher
primo
squeamish ossifrage
Keith Randall
Ilmari Karonen
Quincunx
Optimizer
grc
Calvin's Hobbies
ugoren
Mig
gnibbler
Sp3000
aditsu
histocrat
Ventero
xnor
mniip
Geobits
J B
Joe Z.
Gareth
Jan Dvorak
isaacg
edc65
Victor
steveverrill
feersum
ace
Danko Durbić
xfix
PhiNotPi
user23013
manatwork
es1024
Joey
daniero
boothby
nneonneo
Joey Adams
Timwi
FireFly
dansalmo
grovesNL
breadbox
Timtech
Flonk
algorithmshark
Johannes Kuhn
Yimin Rong
copy
belisarius
professorfish
Ypnypn
trichoplax
Darren Stone
Riot
ProgramFOX
TheDoctor
swish
minitech
Jason C
Tobia
Falko
PleaseStand
VisioN
leftaroundabout
alephalpha
FUZxxl
Peter Olson
Eelvex
marcog
MichaelT
w0lf
Ell
Kyle Kanos
qwr
flawr
James_pic
MtnViewMark
cjfaure
hammar
bitpwner
Heiko Oberdiek
proud haskeller
dan04
plannapus
Mr Lister
randomra
AShelly
ɐɔıʇǝɥʇuʎs
Alexandru
user unknown

( นี่คือวิธีที่ฉันได้รับ )

ท้าทาย

เขียนโปรแกรมหรือฟังก์ชั่นที่ใช้ในรายการของสตริงและส่งออกรายการของสตริงของชื่อเล่นขั้นต่ำสุดที่ไม่ซ้ำใครและไม่ซ้ำใครตามความต้องการ

ใช้วิธีนี้กับแต่ละสตริง S ในรายการตามลำดับที่กำหนดเพื่อสร้างชื่อเล่น:

  1. แยก S เป็นคำโดยคั่นด้วยช่องว่างแล้วลบช่องว่างทั้งหมดในกระบวนการ
  2. แสดงรายการส่วนนำหน้าแบบไม่เว้นว่างของสตริงของตัวอักษรตัวแรกของคำใน S ตั้งแต่สั้นที่สุดไปจนถึงยาวที่สุด
    เช่นJust Some NameJ, JS,JSN
  3. เลือกรายการแรกในรายการนี้ที่ไม่เหมือนกับชื่อเล่นที่เลือกไว้แล้วเป็นชื่อเล่นสำหรับ S. Stop หากเลือกชื่อเล่นแล้วให้ทำขั้นตอนที่ 4 ต่อไป
    เช่นถ้าJust Some Nameเป็นสายอักขระแรกJจะมีการรับประกันว่าเป็นชื่อเล่น
  4. ทำรายการคำนำหน้าอีกครั้ง แต่คราวนี้รวมตัวอักษรตัวที่สองของคำแรกในตำแหน่งที่เป็นธรรมชาติ
    เช่นJust Some NameJu, JuS,JuSN
  5. ทำเช่นเดียวกับในขั้นตอนที่ 3 สำหรับรายการนี้หยุดถ้าพบชื่อเล่นที่ไม่ซ้ำกัน
  6. ทำซ้ำขั้นตอนนี้ด้วยตัวอักษรที่เหลือของคำแรกในที่สุดแทรกตัวอักษรลงในคำที่สองจากนั้นตามด้วยตัวอักษรที่สามและต่อ ๆ ไปจนกว่าจะพบชื่อเล่นที่ไม่ซ้ำกัน
    เช่นสตริงที่ไม่ซ้ำกันครั้งแรกไว้ที่นี่จะเป็นชื่อเล่น:
    Jus, JusS, JusSN
    Just, JustS, JustSN
    Just, JustSo, JustSoN(หมายเหตุที่oไม่ได้ถูกเพิ่มเข้ามาหลังจากJust)
    Just, JustSom, JustSomN
    Just, JustSome, JustSomeN
    Just, JustSome, JustSomeNa
    Just, JustSome, JustSomeNam
    Just, JustSome,JustSomeName

ในที่สุดสตริงอินพุตทั้งหมดควรลงท้ายด้วยชื่อเล่นที่ไม่ซ้ำกัน (อาจเหมือนกับสตริง) คุณอาจสมมติว่าไม่มีสตริงอินพุตใดที่จะแม็พกับชื่อเล่นเดียวกันโดยใช้วิธีนี้

ตัวอย่าง

อัปเดตเพื่อแก้ไขข้อผิดพลาดของฉัน!

สำหรับการป้อนข้อมูล

Martin Buttner
Doorknob
Peter Taylor
Howard
marinus
Dennis
DigitalTrauma
David Carraher
Martin Bitter
Martin Butter
Martin Battle
Martini Beer
Mart Beer
Mars Bar
Mars Barn

ชื่อเล่นจะเป็น

M
D
P
H
m
De
Di
DC
MB
Ma
MaB
Mar
MarB
Mars
MarsB

รายละเอียด

  • อินพุตอาจมาจากไฟล์ (หนึ่งชื่อต่อบรรทัด) หรือชื่อทีละครั้งผ่าน stdin / command line หรือเป็นอาร์กิวเมนต์ฟังก์ชันของรายการสตริงหรือเป็นฟังก์ชัน arg ของสตริงเดี่ยวที่มีบรรทัดใหม่ระหว่างชื่อ
  • เอาต์พุตควรถูกพิมพ์ไปยัง stdout (หนึ่งชื่อเล่นต่อบรรทัด) หรือส่งคืนโดยฟังก์ชันเป็นรายการสตริงหรือเป็นหนึ่งสตริงที่มีบรรทัดใหม่ระหว่างชื่อเล่น
  • จะเป็นการดีที่โปรแกรมจะทำงานสำหรับชื่อที่มีตัวอักษรใด ๆ ยกเว้นจุดสิ้นสุดบรรทัด อย่างไรก็ตามคุณอาจสันนิษฐานว่าชื่อทั้งหมดมีASCII ที่พิมพ์ได้เท่านั้น (ชื่อ PPCG ไม่ได้)
  • เฉพาะอักขระช่องว่างปกติเท่านั้นที่นับเป็นตัวแยกคำ ช่องว่างนำหน้าและต่อท้ายสามารถละเว้นได้

เกณฑ์การให้คะแนน

การส่งที่สั้นที่สุดในหน่วยไบต์ชนะ Tiebreaker จะไปที่คำตอบที่โพสต์เร็วที่สุด


49
สิ่งนี้อธิบายว่าทำไมฉันตื่นขึ้นมากลางดึกด้วยความรู้สึกที่คลุมเครือของความรู้สึกที่ถูกละเมิด
Martin Ender

คำตอบ:


8

CJam, 58 53 ไบต์

นี่สามารถเล่นกอล์ฟได้มาก .. แต่สำหรับผู้เริ่ม:

LqN/{:Q1<aQ,,:)QS/f{{1$<_,@-z1e>}%W<s}+{a1$&!}=a+}/N*

การขยายรหัส :

L                         "Put an empty array on stack. This is the final nickname array";
 qN/{  ...   }/           "Read the input and split it on new lines. Run the block for each";
     :Q1<a                "Store each name in Q and get its first char. Wrap it in an array";
          Q,,:)           "Get an array of 1 to length(name) integers";
               QS/        "Split the name on spaces";
f{{           }%   }      "for each of the integer in the array, run the code block";
                          "and then for each of the name part, run the inner code block";
   1$<                    "Copy the integer, take first that many characters from the";
                          "first part of the name";
      _,@-z1e>            "Get the actual length of the part and the number of characters";
                          "to be taken from the next name part, minimum being 1";
                W<        "Get rid of the last integer which equals 1";
                  s       "Concat all name parts in the array";
                    +     "Add the list of nick names as per spec with the first character";
{     }=                  "Get the first nick name that matches the criteria";
 a1$&                     "Wrap the nick name in an array and do set intersection with";
                          "the copy of existing nick names";
     !                    "Choose this nick name if the intersection is empty";
N*                        "After the { ... }/ for loop, the stack contains the final";
                          "nick names array. Print it separated with new lines";

ลองออนไลน์ได้ที่นี่


2
ดูความคิดเห็นของฉันที่ OP: ถ้า 'Ju' หรือ 'Jus' เป็นชื่อเล่นที่ถูกต้องสำหรับ 'Just Some Name', 'Maertin Butter' sholud เป็น 'Ma' ดังนั้น 'MaB', 'Mar', 'MarB'
edc65

สิ่งที่ @ edc65 พูดว่าเป็นความจริง ขอโทษสำหรับเรื่องนั้น. ฉันจะอนุญาตให้คุณไม่เปลี่ยนแปลงสิ่งต่าง ๆ หากคุณต้องการ; มันเป็นความผิดพลาดของฉัน
งานอดิเรกของ Calvin

9

JavaScript (ES6) 159

ทำตามข้อกำหนดและไม่ใช่ตัวอย่าง

ฉันสร้างชื่อเล่นผู้สมัครที่มีคำกลางปัจจุบัน (ที่จุดเริ่มต้นคำแรก) คำก่อนที่กระแสจะถูกใช้ 'ตามสภาพ' คำที่อยู่หลังคำตอบปัจจุบันโดยไม่มี - หรือตัวอักษรแรก คำปัจจุบันมีส่วนเพิ่มอีก 1 อักขระสำหรับแต่ละลูป

ตัวอย่าง 'แค่บางชื่อ' => 'แค่', 'บาง', 'ชื่อ'
Cw Justตำแหน่ง1ลองJ, JS, JSN
Cw Justตำแหน่ง2ลองJu, JuS, JuSN
Cw Justตำแหน่ง3ลองJus, JusS, JusSN
Cw Justตำแหน่ง4ลองJust, JustS, JustSN
ตอนนี้Justหมด , Someกลายเป็น Cw, รีสตาร์ทตำแหน่งเป็น 2 (สำหรับตำแหน่ง 1, พยายามทั้งหมดแล้ว)

cw Someตำแหน่ง2, ลองJust, JustSo, JustSoN
cw Someตำแหน่ง3ลองJust, JustSom, JustSomN
cw Someตำแหน่ง4ลองJust, JustSome, JustSomeN
ตอนนี้SomeคือหมดNameกลายเป็น cw ตำแหน่งเริ่มต้นใหม่ 2

cw Nameตำแหน่ง2, ลองJust, JustSome, JustSomeNa
cw Nameตำแหน่ง3ลองJust, JustSome, JustSomeNam
cw Nameตำแหน่ง4ลองJust, JustSome, JustSomeName
นั่นคือคนทุกคน!

รหัส

(q คือตำแหน่งคำปัจจุบัน p คือตำแหน่งการตัด)

F=l=>
  l.map(w=>{ 
    for(w=w.match(/[^ ]+/g),q=p=0;
        w.every((w,i)=>~o.indexOf(t+=i<q?w:i>q?w[0]:w.slice(0,p+1)),t='')
        &&(w[q][p++]||(p=1,w[++q]));
       );
    o.push(t)
  },o=[])&&o

ทดสอบในคอนโซล Firefox / FireBug

F(['Martin Buttner','Doorknob','Peter Taylor','Howard','marinus'
  ,'Dennis','DigitalTrauma','David Carraher'
  ,'Martin Bitter','Martin Butter','Martin Battle','Martini Beer','Mart Beer'])

["M", "D", "P", "H", "m", "De", "Di", "DC", "MB", "MB", "Ma", "MaB", "Mar", " MarB "]


2

PHP, 327 289 275 274 270

อาจมีศักยภาพในการเล่นกอล์ฟเล็กน้อย

while($n=fgets(STDIN)){$c=count($w=preg_split('/\s+/',trim($n)));$p=[];for($k=0;$k<$c;$p[$k]++){for($t='',$j=0;$j<$c;$j++)$t.=substr($w[$j],0,$p[$j]+1);for($j=1;$j<=strlen($t);$j++)if(!in_array($v=substr($t,0,$j),$u))break 2;$k+=$p[$k]==strlen($w[$k]);}echo$u[]=$v,'
';}
  • โปรแกรมทำงานบน stdin / stdout, ทำงานบน ASCII, buggy บน UTF
  • การใช้งาน: php -d error_reporting=0 golfnicks.php < nicknames.txt
  • หรือcat <<EOF | php -d error_reporting=0 golfnicks.php+ รายชื่อ +EOF
  • ในการทดสอบเป็นฟังก์ชั่นในเว็บเบราเซอร์: ดึงข้อมูลรายละเอียดที่ uncomment สายทั้งหมดที่มีเครื่องหมายและแสดงความคิดเห็นอย่างใดอย่างหนึ่งที่มีเครื่องหมาย// FUNC //PROGลองf(array_fill(0,21,'Just Some Name'));

ทำให้พังถล่ม

#error_reporting(0);function f($a){echo'<pre>'; // FUNC
#foreach($a as$n) // FUNC
while($n=fgets(STDIN)) // PROG
{
    $c=count($w=preg_split('/\s+/',trim($n)));     // split name to words, count them
    $p=[];                                         // initialize cursors
    for($k=0;$k<$c;$p[$k]++)
    {
        for($t='',$j=0;$j<$c;$j++)$t.=substr($w[$j],0,$p[$j]+1); // concatenate prefixes
        for($j=1;$j<=strlen($t);$j++)              // loop through possible nicks
            if(!in_array($v=substr($t,0,$j),$u))   // unused nick found
                break 2;                           // -> break cursor loop
        $k+=$p[$k]==strlen($w[$k]);                // if Cw exhausted -> next word
        // strlen()-1 would be correct; but this works too)
    }
    echo$u[]=$v,'
';
}
#echo '</pre>';} // FUNC
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.