ทำไม? เพื่อโปรดโปรแกรมเมอร์ที่ร่ำรวย!


21

คุณเคยถามผู้แปลว่า "ทำไม" พวกเราส่วนใหญ่ผิดหวังเมื่อรหัสไม่ทำงานตามที่ควร Mathworks ได้ใช้ฟังก์ชั่นเล็ก ๆ น้อย ๆ ที่ดีwhyตอบคำถาม เพื่อให้ตัวอย่างจาก MATLAB:

why
The programmer suggested it.

why
To fool the tall good and smart system manager.    

why(2)
You insisted on it.

why(46)
Bill insisted on it.

งานของคุณคือการใช้whyฟังก์ชั่นในภาษาของคุณ ฟังก์ชั่นควรทำงานกับและไม่มีอาร์กิวเมนต์อินพุต (หรือใช้อินพุต0หรือ-1) ฟังก์ชั่นจะต้องตั้งชื่อwhy(หรือการเขียนwhy(n)ใน STDIN ควรส่งผลให้สตริงที่เหมาะสมจะถูกพิมพ์)

หากไม่มีการกำหนดอาร์กิวเมนต์หรืออาร์กิวเมนต์เป็นศูนย์หรือลบสตริงเอาต์พุตควรเป็นวลีสุ่มที่ถูกต้อง ดังนั้นควรจะมีฟังก์ชั่นwhy, why(), why(0)หรือwhy(-1)ว่าผลตอบแทนที่เป็นประโยคแบบสุ่ม

หากมีการป้อนค่าอาร์กิวเมนต์nจะได้รับ (อาร์กิวเมนต์ของฟังก์ชันไม่ใช่ STDIN) เอาต์พุตควรเป็นสตริง n'th (กำหนดไว้ด้านล่าง) ดังนั้นwhy(1)ควรส่งผลลัพธ์ (พิมพ์ / แสดง) ผลลัพธ์เดียวกันเสมอ

ประโยคที่ถูกสร้างขึ้นดังต่อไปนี้ (ประเภท 1, ประเภท 2 และพิเศษ) !จบประโยคทั้งหมดที่มี

"Person" "ending" !
"Verb" "adjective" "Person" !
A list of special cases

รายชื่อบุคคล:

Stewie
Peter
Homer
The programmer
The system manager
You

รายการสิ้นสุด:

suggested it
insisted on it
did it

รายการคำกริยาคือ:

To fool
To satisfy
To please

รายการคำคุณศัพท์:

the smart
the bald
the tall
the rich
the stupid

รายการกรณีพิเศษ:

How should I know?
Stop asking!
Don't ask!

วิธีเลือกหมายเลขที่กำหนดคือ:

ประเภทของประโยค:

Odd number => Type 1
Even number => Type 2
n % 7 = 0 => Type 3 (% is the modulus operator)

ชื่อ: ชื่อที่ n ถูกกำหนดโดยใช้โมดูลัส (%)

n = 1:   1 % 7  => Stewie
n = 2:   2 % 7  => Peter
...
n = 6:   6 % 7  => You
n = 7:   7 % 7  => How should I know?
n = 11:  11 % 7 => The programmer
n = 14:  14 % 7 => Stop asking!
n = 21:  21 % 7 => Don't ask!

ตอนจบ: ตอนจบที่ n ยังถูกกำหนดโดยใช้โมดูลัส สมมติว่าตอนจบ (1, 2 และ 3) (1 2 2 3)มีการระบุไว้เช่น เนื่องจากตัวเลขคี่ใช้เสมอ((n+1)/2 % 4)

n = 1:    ((1+1)/2 % 4)  => suggested it
n = 3:    ((3+1)/2 % 4)  => insisted on it
n = 13:   ((13+1)/2 % 4) => did it

คำคุณศัพท์: คำคุณศัพท์ที่ n ถูกกำหนดโดยใช้โมดูลัส ในขณะที่ตัวเลขเสมอแม้ใช้:(n % 10)/2

n = 2:   (2 % 10)/2 => Smart
n = 6:   (6 % 10)/2 => The tall
...

คำกริยา: คำกริยาที่ n ยังถูกกำหนดโดยใช้โมดูลัส สมมติว่าคำกริยา (1, 2 และ 3) มีการระบุไว้เช่น(1 2 2 3)เดียวกับที่ตัวเลขมักจะเป็นคำกริยาให้ใช้เสมอ(n % 8) / 2

n = 2:   (2 % 8)/2 => To fool
n = 4:   (4 % 8)/2 => To satisfy
n = 6:   (6 % 8)/2 => To satisfy
n = 8:   (8 % 8)/2 => To please

ตอนนี้วิธีการสร้างแบบสุ่มควรจะค่อนข้างง่ายเพียงเลือกแบบสุ่ม nตอนนี้วิธีที่จะสร้างแบบสุ่มหนึ่งควรจะค่อนข้างง่ายเพียงแค่เลือกแบบสุ่ม

ตัวอย่างบางส่วน:

why
You suggested it!

why
To fool the tall Homer!

why
Don't ask!

why(1)
Stewie suggested it!

why(14)
Stop asking!

why(8)
To please the rich Stewie!

บังคับใช้กฎกอล์ฟมาตรฐาน ผู้ชนะจะได้รับการคัดเลือกหนึ่งสัปดาห์จากวันที่มีการโพสต์ความท้าทาย


เขียนว่าทำไมใน stdin ควรคืนสายฉันอาจจะสามารถทำงานกับที่ มันจะต้องเป็นตัวพิมพ์เล็กwhyหรือจะWHYเป็นที่ยอมรับ?
เดนนิส

ตัวพิมพ์เล็กและตัวพิมพ์ใหญ่นั้นใช้ได้ทั้งคู่
Stewie Griffin

นอกจากนี้ตอนจบไม่ควรต้อง/2ทำงาน ที่ให้ค่าเศษส่วน 13ควรเป็นinsisted(14/2 = 7% 4 = 3 = 2nd ยืนยัน)
Jonathan Leech-Pepin

3
มันสำคัญถ้าคุณจบลงด้วยกรณีเช่นthe rich The programmerเนื่องจากเนื่องจากที่ระบุไว้the?
Jonathan Leech-Pepin

1
@StewieGriffin คำTheและToในรายการของคุณควรอาจจะเป็นตัวพิมพ์เล็กเพื่อให้ตรงกับตัวอย่างของคุณ ...
mbomb007

คำตอบ:


5

JavaScript (ES6) 345

ไม่แน่ใจเกี่ยวกับตัวเลข แต่นี่คือความพยายามของฉัน

ทดสอบการเรียกใช้ข้อมูลโค้ดด้านล่างในเบราว์เซอร์ที่สอดคล้องกับ EcmaScript

why=n=>(n<1?n=Math.random()*840|0:0,s="suggested,insisted on,did,fool,satisfy,please,stupid,smart,bald,tall,rich,Don't ask!,How should I know?,Stop asking!,Stewie,Peter,Homer,programmer,system manager,You".split`,`,n%7?(p=s[n%7+13],n&1?(p>'a'?'The ':'')+p+` ${s['2011'[-~n/2%4]]} it!`:`To ${s['5344'[n%8/2]]} the ${s[n/2%5+6]} ${p}!`):s[11+n%3])  

for(o='',i=0;++i<1e3;)o+=i+':'+why(i)+'\n';O.innerHTML=o

function test() { R.innerHTML=why(+I.value) }

// Less golfed

WHY=n=>(
  n=n<1?Math.random()*999|0:n,
  s=["suggested", "insisted on", "did", "fool", "satisfy", "please",
     "stupid", "smart", "bald", "tall", "rich",
     "Don't ask!", "How should I know?", "Stop asking!",
     "Stewie", "Peter", "Homer", "programmer", "system manager", "You"],
  n%7
    ? ( p=s[n%7+13],
        n&1
        ? (p>'a'?'The ':'')+p+` ${s['2011'[-~n/2%4]]} it!`
        : `To ${s['5344'[n%8/2]]} the ${s[n/2%5+6]} ${p}!`)
    : s[11+n%3]
)  
#O { height:300px; width:50%; overflow:auto }
#I { width:2em }
Why(<input id=I>)<button onclick=test()>-></button><span id=R></span>
<pre id=O>


คำตอบที่ดี! ไม่สามารถพูดอย่างตรงไปตรงมาเกี่ยวกับความถูกต้องของจำนวน แต่คุณสามารถบันทึก 1 ไบต์ (!) โดยใช้0เป็นตัวคั่นสตริงของคุณแทน,!
Dom Hastings

1
@DomHastings ฉันคิดว่าไม่จำเป็นต้องใช้กลเม็ดกับเทมเพลตสตริงอีกต่อไป split(0)มีความยาวเท่ากันsplit','(ทำท่าว่านี่คือ backticks)
edc65

อ่าาาขอโทษฉันคิดว่าคุณอาจสูญเสียวงเล็บนี่ไม่ใช่กาแฟ! :)
Dom Hastings

3

C #, 502 ไบต์

โครงการนี้จำเป็นต้องมีการตั้งชื่อAssemblyName ว่าทำไมที่จะสร้างการปฏิบัติการที่มีชื่อที่ถูกต้อง

golfed อย่างเต็มที่:

using System;class W{static void Main(string[]i)int n=i.Length>0?int.Parse(i[0]):new Random().Next();string[]s={"Don't ask!","How should I know?","Stop asking!"},v={"To please ","To fool ","To satisfy ",null},a={"the stupid","the smart","the bald","the tall","the rich"},p={"","Stewie","Peter","Homer","The programmer","The system manager","You"},e={"suggested it!","insisted on it!",null,"did it!"};Console.Write(n%7<1?s[n%3]:n%2<1?(v[n%8/2]??v[2])+a[n%10/2]+" {0}!":"{0} "+(e[n/2%4]??e[1]),p[n%7]);}}

การเยื้องและบรรทัดใหม่เพื่อความชัดเจน:

using System;
class W{
    static void Main(string[]i)
        int n=i.Length>0
            ?int.Parse(i[0])
            :new Random().Next();
        string[]
            s={"Don't ask!","How should I know?","Stop asking!"},
            v={"To please ","To fool ","To satisfy ",null},
            a={"the stupid","the smart","the bald","the tall","the rich"},
            p={"","Stewie","Peter","Homer","The programmer","The system manager","You"},
            e={"suggested it!","insisted on it!",null,"did it!"};
        Console.Write(
            n%7<1
                ?s[n%3]
                :n%2<1
                    ?(v[n%8/2]??v[2])+a[n%10/2]+" {0}!"
                    :"{0} "+(e[n/2%4]??e[1]),
            p[n%7]
        );
    }
}

ตัวอย่างอินพุต / เอาต์พุต:

>Why
To fool the bald Homer!
>Why 1
Stewie suggested it!

2

Powershell 437 461 453 Bytes

แก้ไข: พลาดคำกริยาซ้ำ

แยกระหว่างคลังข้อมูลและการคำนวณจำนวนไบต์

  • 267 ข้อความไบต์ = Pre-coded (ไม่รวมto, the, itและ!เนื่องจากพวกเขามีการแก้ไขสถานที่)
  • 186 Bytes = การคำนวณ

ตั้งค่าอาร์กิวเมนต์เริ่มต้นเป็น 0 หากไม่ได้ระบุไว้ หากมีการโต้แย้ง<1ก็จะได้รับตัวเลขสุ่ม<99 fn:1และเรียกใช้อีกครั้ง เทคนิควิธีการนี้-50จะทำงานได้ดีเช่นกันโดยถือว่าเป็นกรณีสุ่ม

function why{param($b=0)$p=@('Stewie','Peter','Homer','The programmer','The system manager','You');$e=@('did','suggested','insisted on','insisted on');$v=@('please','fool','satisfy','satisfy');$a=@('stupid','smart','bald','tall','rich');$s=@("Don't ask!",'How should I know?','Stop asking!');if($b-le0){why(Get-Random 99)}elseif(!($b%7)){$s[$b/7%3]}else{$n=$p[$b%7-1];if($b%2){"$n $($e[($b+1)/2%4]) it!"}else{"To $($v[$b%8/2]) the $($a[$b%10/2]) $n!"}}}

คำอธิบาย:

# Any -1 in calculations is to account for arrays starting at index 0
# Final key placed in position 0 for indexing (4%4 = 0)
# Powershell 1 = True, 0 = False
if($b-le0){why(Get-Random 99)}          # If not positive, choose random number
elseif(!($b%7))                         # $b%7 means special so use that
  {$s[$b/7%3]}                          # Divide by 7 and find modulo, use that phrase.
else{$n=$p[$b%7-1]                      # Get the correct person (Used in both types). 6 max
if($b%2){"$n $($e[($b+1)/2%4]) it!"}    # Create type 1 sentence
else{"To $($v[$b%8/2]) the $($a[$b%10/2]) $n!"} # Type 2

fn:199 เลือกเพื่อบันทึกไบต์ หากมีมากกว่า 99 ประโยคที่เป็นไปได้ข้างต้น (ไม่ได้คำนวณ) เพิ่มเป็น 999 หรือ 9999 ตามความเหมาะสม (+1/2 ไบต์)


2

MUMPS, 379 ไบต์

f(s,i) s:'i i=$L(s,"^") q $P(s,"^",i)
why(n) s:'n n=$R(840) s p="Stewie^Peter^Homer^The programmer^The system manager^You",e="suggested^insisted on^did",v="fool^satisfy^please",a="smart^bald^tall^rich^stupid",s="How should I know?^Stop asking!^Don't ask!" q:n#7=0 $$f(s,n#3) q:n#2 $$f(p,n#7)_" "_$$f(e,n+1/2#4)_" it!" q "To "_$$f(v,n#8/2)_" the "_$$f(a,n#10/2)_" "_$$f(p,n#7)_"!"

เมื่อไม่มีการป้อนข้อมูลจะมีการสร้างตัวเลขสุ่มใน 0..839

การใช้งาน:

>w $$why(15)
Stewie did it!
>w $$why()
Don't ask!

กลยุทธ์การประเมินผลจากซ้ายไปขวาของ MUMPS ช่วยให้คุณประหยัดค่าใช้จ่ายได้ไม่กี่ไบต์ในวงเล็บที่นี่

หมายเหตุด้านข้าง: ดูสตริงที่มีลักษณะ"foo^bar^baz^qux"อย่างไร สิ่งเหล่านี้เรียกว่า "ตัวคั่นสตริง" และเป็นวิธีมาตรฐานในการจัดเก็บรายการที่เหมาะสมภายในขีด จำกัด ขนาดสตริงสูงสุดเนื่องจาก MUMPS ไม่มีรายการ / อาร์เรย์ (หรือแน่นอนโครงสร้างข้อมูลใด ๆ นอกเหนือจากต้นไม้) สำหรับรายการที่มีขนาดใหญ่เกินไปที่จะใส่ลงในสายเดียวเราใช้ tree of depth 1 แทนและวางค่าไว้บนใบไม้ของ tree สนุก!


+1: MUMPS ทุกภาษาต้องการwhyฟังก์ชั่นจริงๆ ;)
DLosc

1

Emacs Lisp 473 Bytes

(defun why(n)(if(and n(> n 0))(let*((p'(t"Stewie""Peter""Homer""The programmer""The system manager""You"))(e'("did""suggested""insisted on""insisted on"))(v'("please""fool""satisfy""satisfy"))(a'("stupid""smart""bald""tall""rich"))(s'("Don't ask!""How should I know?""Stop asking!"))(q(nth(% n 7)p)))(cond((=(% n 7)0)(nth(%(/ n 7)3)s))((=(% n 2)1)(format"%s %s it!"q(nth(%(/(1+ n)2)4)e)))(t(format"To %s the %s %s!"(nth(/(% n 8)2)v)(nth(/(% n 10)2)a)q))))(why(random 99))))

ที่ใหญ่ที่สุด 'ขยะ' น่าจะเป็นformat, %s... ส่วน หากตัวแปรสามารถถูกแทรกลงในสตริงโดยไม่ต้องระบุข้อมูลจำเพาะมันจะช่วยประหยัด 10 ไบต์%sและอีก 12 บนformat


1

Ruby 396 378 372 ไบต์

ฉันแน่ใจว่านี่ไม่ใช่ golfed ให้สูงสุด

p=%w[a Stewie Peter Homer The\ programmer The\ system\ manager You]
why=->n{n<1 ? why[rand(99)+1]:n%7<1 ? $><<%w[a How\ should\ I\ know? Stop\ asking! Don't\ ask!][n/7]:n%2<1 ? $><<'To '+%w[fool satisfy satisfy please][n%8/2-1]+' the '+%w[smart bald tall rich stupid][n%10/2-1]+' '+p[n%7]+?!:$><<p[n%7]+' '+%w[a suggested insisted\ on insisted\ on did][(n+1)/2%4]+' it!'}

แก้ไข: ฉันเพิ่งรู้ว่าฉันไม่ทราบความสำคัญของผู้ดำเนินการ โอ้ ..


rand (21) ดูเหมือนว่าเล็กเกินไป
edc65

ประมาณ 99 : P
Peter Lenkefi

มีประโยครวม 108 ประโยคและอีก 3 กรณีพิเศษ ลำดับของประโยคเป็นคาบ แต่ระยะเวลา ... ฉันไม่รู้
edc65

ช่วงเวลาน่าจะเป็น 840 ... ตัวเลือกที่ดีของตัวเลข
edc65

1

CJam, 281 ไบต์

99mrqi_])0>=i:A7md{;A2%{S["suggested""insisted on""did"]A2/=+" it"+}{"To "["fool""satisfy"_"please"]A(2/:B=" the "["smart""bald""tall""rich""stupid"]B=SM}?[M"Stewie""Peter""Homer""The programmer""The system manager""You"]A=\'!}{["How should I know?""Stop asking!""Don't ask!"]\(=}?

Permalink

ยังไม่เคยใช้ CJam มาก่อนดังนั้นฉันจะใช้คำแนะนำใด ๆ ฉันแน่ใจว่ามีเทคนิคมากมายที่ฉันไม่รู้!

(ฉันไม่สามารถหาวิธีตั้งชื่อนี้เป็นฟังก์ชันที่เรียกว่า "ทำไม" - ปรากฏว่าฟังก์ชันไม่มีอยู่ใน CJam - ดังนั้นฉันไม่แน่ใจว่าคำตอบ CJam นั้นใช้ได้หรือไม่ ... )


ฟังก์ชั่นทำอยู่ แต่มีเพียง 26 ชื่อฟังก์ชั่นที่ถูกต้อง (A ถึง Z)
เดนนิส

1

Lua 5.3.0, 452 460 446 ไบต์

นี่เป็นความพยายามครั้งแรกของฉันที่การตีกอล์ฟดังนั้นโปรดแก้ไขให้ฉันถ้าฉันทำอะไรผิดพลาด!

a={"Stewie","Peter","Homer","The Programmer","The system manager","You"}b={"fool ","satisfy ",n,"please "}b[3]=b[2]why=function(n)pcall(math.randomseed,n and n>0 and n)n=math.random(1e9)q=n>>4r=n>>8return 1>n&7 and({"How should I Know?","Stop asking!","Don't ask!"})[q%3+1]or 0<n%2 and a[q%6+1]..({" suggested"," insisted on"," did"})[r%3+1].." it"or"To "..b[q%4+1].."the "..({"smart ","bald ","tall ","rich ","stupid "})[r%5+1]..a[(r>>4)%6+1]end

Ungolfed:

a={
    "Stewie",
    "Peter",
    "Homer",
    "The Programmer",
    "The system manager",
    "You"
}
b={
    "fool ",
    "satisfy ",
    n,
    "please "
}
b[3]=b[2]
why=function(n)
    pcall(math.randomseed,n and n>0 and n)
    n=math.random(1e9)
    q=n>>4
    r=n>>8
    return
            1>n&7 and
                ({"How should I Know?","Stop asking!","Don't ask!"})[q%3+1]
            or 0<n%2 and
                a[q%6+1]..({" suggested"," insisted on"," did"})[r%3+1].." it"
            or
                "To "..b[q%4+1].."the "..({"smart ","bald ","tall ","rich ","stupid "})[r%5+1]..a[(r>>4)%6+1]
end

0

Python (2), 692 ไบต์

ฉันยังคงเรียนรู้ดังนั้นโปรดเป็นคนอ่อนโยน! :)

from sys import argv,maxint
import random
def why(*O):
 P=["You","Stewie","Peter","Homer","programmer","system manager"];E=["did","suggested","insisted on","insisted on"];I=["please","fool","satisfy","satisfy"];Q=["stupid","smart","bald","tall","rich"];S=["Don't ask!","How should I know?","Stop asking!"]
 if len(argv)>1:A=O[0]
 else:A=random.randint(-maxint-1,maxint)
 if A%7==0:print S[A%3]
 else:
  M=P[A%6]
  if A%11==0:
   M=P[4]
  if A%2==0:
   if M==P[0]:M="you"
   U=I[(A%8)/2];Z=Q[(A%10)/2];print"To %s the %s %s!"%(U,Z,M)
  if A%2==1:
   if M==P[4]:M="The %s"%P[4]
   if M==P[5]:M="The %s"%P[5]
   C=E[((A+1)/2)%4];print"%s %s it!"%(M,C)
if len(argv)>1:why(int(argv[1]))
else:why()

ทำงานร่วมกับหรือไม่มี int เป็นอาร์กิวเมนต์บรรทัดคำสั่ง

ฉันพยายามเน้นความถูกต้องของรหัสให้มากที่สุดเท่าที่จะทำได้เช่นเดียวกับการสร้างตัวเลขสุ่มจาก-sys.maxint - 1ไปยังsys.maxintและแสดงประโยคในกรณีที่ถูกต้อง

รหัสขึ้นอยู่กับ if-statement อย่างมากซึ่งฉันแน่ใจว่าสามารถถูกแทนที่ด้วยบางสิ่งที่มีประสิทธิภาพมากกว่า

ข้อเสนอแนะยินดีต้อนรับมาก!

Ungolfed (1341 ไบต์)

from sys import argv, maxint
import random

def Why(*Optional):
    Persons = ["You", "Stewie", "Peter", "Homer", "programmer", "system manager"]
    Endings = ["did it", "suggested it", "insisted on it", "insisted on it"]
    Verbs = ["please", "fool", "satisfy", "satisfy"]
    Adjectives = ["stupid", "smart", "bald", "tall", "rich"]
    SpecialCases = ["Don't ask!", "How should I know?", "Stop asking!"]

    if len(argv) > 1:
        Argument = Optional[0]
    else:
        Argument = random.randint(-maxint - 1, maxint)

    if Argument % 7 is 0:
        print SpecialCases[Argument % 3]
    else:
        Person = Persons[(Argument) % 6]
        if Argument % 11 is 0:
            Person = "programmer"
        if Argument % 2 is 0:
            if Person is "You":
                Person = "you"
            Verb = Verbs[(Argument % 8) / 2]
            Adjective = Adjectives[(Argument % 10) / 2]
            print "To %s the %s %s!" % (Verb, Adjective, Person)
        if Argument % 2 is 1:
            if Person is "programmer":
                Person = "The programmer"
            if Person is "system manager":
                Person = "The system manager"
            Ending = Endings[((Argument + 1) / 2) % 4]
            print "%s %s!" % (Person, Ending)

if len(argv) > 1:
    Why(int(argv[1]))
else:
    Why()

2
ฉันขอแนะนำให้คุณอ่าน [ codegolf.stackexchange.com/questions/54/…—โดยเฉพาะอย่างยิ่งคุณสามารถบันทึกไบต์จำนวนมากโดยใช้เคล็ดลับแรกในหน้านั้น นอกจากนี้คุณยังสามารถสร้างดัชนีจากด้านหลังของรายการกริยาเพื่อให้คุณต้องการเพียง "สนอง": ใช้argument%8/2-1และลบอันที่สอง นอกจากนี้คุณยังสามารถแทนที่ด้วย==0 <1
lirtosiast
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.