การปรุงอาหารด้วยรหัส


24

คุณเป็นพ่อครัวและคุณรักการทำอาหารด้วยเครื่องเทศ แต่เมื่อไม่นานมานี้คุณชอบที่จะจัดระเบียบเครื่องเทศตามความถี่ที่คุณใช้ แต่คุณไม่มีเวลาที่จะจดบันทึกเมื่อคุณใช้เครื่องเทศครั้งสุดท้าย เพียงแค่คุณสลับและเคลื่อนย้ายเครื่องเทศไปรอบ ๆ และสิ่งนี้ดูเหมือนจะเป็นการหลอกลวง

แต่แน่นอนคุณเป็นพ่อครัวและนั่นหมายความว่าคุณมีพ่อครัวกับคุณ คุณตัดสินใจที่จะบอกพวกเขาถึงกฎง่ายๆของการมีส่วนร่วมกับเครื่องเทศของคุณ

  1. หากคุณเพิ่งใช้เครื่องเทศย้ายมันขึ้นมาหนึ่งในชั้นวางเครื่องเทศ

  2. หากคุณไม่ได้ใช้เครื่องเทศเลยเช่น[]รายการการเคลื่อนไหวที่ว่างเปล่ารายการเครื่องเทศจะไม่ได้รับผลกระทบ

  3. คุณอาจใส่เครื่องเทศเข้าไปในที่ยึดเครื่องเทศของฉัน แต่ถ้าคุณใช้ให้แน่ใจว่าได้ย้าย

  4. รายการสามารถมีอะไรก็ได้ แต่เนื่องจากเครื่องเทศเหล่านี้เรากำลังทำงานด้วย ขอแนะนำให้คุณใช้ชื่อของเครื่องเทศ

  5. เครื่องเทศควรไม่ซ้ำกัน เครื่องเทศเดียวกันมากเกินไปทำให้น้ำซุปเสีย ... หรือว่าคำพูดจะไป

มีการใช้กฎของสนามกอล์ฟรหัสปกติ

ตัวอย่างของ Oregano ที่มีการใช้งานซ้ำแล้วซ้ำอีก

pepper  pepper  pepper  pepper  oregano
paprika paprika paprika oregano pepper
salt    salt    oregano paprika paprika
cumin   oregano salt    salt    salt
oregano cumin   cumin   cumin   cumin

งาน

ใส่รายการของเครื่องเทศและรายการของเครื่องเทศที่ใช้แล้วออกรายการสุดท้าย

ตัวอย่าง

อินพุต

[pepper, paprika, salt, cumin, oregano], [oregano, cumin, cumin, salt, salt, salt]

เอาท์พุต

[salt, pepper, paprika, cumin, oregano]

มันมีลักษณะอย่างไร

pepper  pepper  pepper  pepper  pepper  pepper  salt
paprika paprika paprika paprika paprika salt    pepper
salt    salt    salt    cumin   salt    paprika paprika
cumin   oregano cumin   salt    cumin   cumin   cumin
oregano cumin   oregano oregano oregano oregano oregano

อินพุต

[pepper, paprika, salt, cumin, oregano], [salt, salt, salt, salt, salt, salt, salt, salt, salt, salt, salt, salt, oregano]

เอาท์พุต

[salt, pepper, paprika, oregano, cumin]

รายการในรายการเครื่องเทศมีเอกลักษณ์หรือไม่?
tsh

ใช่พวกเขาจะไม่เหมือนใคร
tisaconundrum

31
ฉันทำสิ่งนี้ได้ในพ่อครัวแต่ในที่สุดมันก็เหนื่อยเกินไป! ฉันจะให้รางวัลแก่คน 50 คนหากพวกเขาทำได้
geokavel

5
นี่คือส่วนสำคัญของการป้อนข้อมูลลงในชามผสม ส่วนที่เหลือจะยากมาก แต่ทำได้สำหรับคนที่ใช่!
geokavel

คำตอบ:


4

Husk , 15 14 ไบต์

Fλṁ↔`C⁰tMo→=¢⁰

อินพุตเป็นรายการของสตริง (นอกจากนี้ยังสามารถใช้ได้กับรายการประเภทอื่น ๆ ) ลองออนไลน์!

-1 ไบต์ต้องขอบคุณ H.PWiz

คำอธิบาย

Fλṁ↔`C⁰tMo→=¢⁰  Implicit inputs (two lists).
F               Fold second input using the first as initial value
 λ              with this anonymous function:
                 Arguments x (list of spices) and s (used spice).
                 For example, x=["a","b","c","d"] and s="c".
            ¢⁰   Repeat x infinitely: ["a","b","c","d","a","b","c","d"..
        M        For each item,
           =     test equality to s: [0,0,1,0,0,0,1,0..
         o→      and increment: [1,1,2,1,1,1,2,1..
       t         Drop first element: [1,2,1,1,1,2,1..
    `C⁰          Cut x to these lengths: [["a"],["b","c"],["d"]]
  ṁ↔             Reverse each slice and concatenate: ["a","c","b","d"]

ฉันต้องทำซ้ำxอย่างไม่สิ้นสุดเนื่องจากมิฉะนั้นรายการจะสูญเสียองค์ประกอบสุดท้ายเมื่อเราใช้เครื่องเทศชั้นสูงสุด มันจะเพียงพอที่จะเพิ่ม 1 ต่อท้าย แต่การทำซ้ำจะน้อยกว่าไบต์ วิธีที่ดีกว่าคือการหมุนรายการแทนที่จะวางองค์ประกอบแรกลงไป แต่ Husk ไม่มีตัวในตัวสำหรับสิ่งนั้น


Σmเป็นหนึ่งไบต์
H.PWiz

8

Haskell , 48 ไบต์

foldl(?)เป็นฟังก์ชั่นที่ไม่ระบุชื่อซึ่งรับสองอาร์กิวเมนต์ของรายการและส่งคืนรายการโดยมีองค์ประกอบทั้งหมดของชนิด ( Eq-comparable) ที่เหมือนกัน

foldl(?)["pepper", "paprika", "salt", "cumin", "oregano"]["oregano", "cumin", "cumin", "salt", "salt", "salt"]ใช้เป็น

foldl(?)
(x:y:r)?n|y==n=y:x:r|s<-y:r=x:s?n
s?n=s

ลองออนไลน์!

  • foldl(?) s mเริ่มต้นด้วย (เครื่องเทศชั้น) รายการsและรวมกับแต่ละองค์ประกอบ (เครื่องเทศ) จากเพื่อใช้ประกอบการm?
  • s?nใช้เครื่องเทศnจากชั้นวางเครื่องเทศsและส่งคืนชั้นวางเครื่องเทศที่ได้
    • หากsมีองค์ประกอบอย่างน้อยสองรายการให้?ตรวจสอบว่าองค์ประกอบที่สองนั้นเท่ากับnหรือไม่และหากเป็นเช่นนั้นให้เปลี่ยนองค์ประกอบสองรายการแรก ถ้าไม่เท่ากัน?ให้แก้ไของค์ประกอบแรกและเรียกส่วนที่เหลือซ้ำ
    • หากsมีอย่างน้อยหนึ่งองค์ประกอบ?จะส่งคืนโดยไม่มีการเปลี่ยนแปลง

7

พ่อครัว , 875 843 ไบต์

S.

Ingredients.
1 g T
0 g Z
J
I

Method.
Put Z into mixing bowl.Take I from refrigerator.B I.Put I into mixing bowl.Take I from refrigerator.B until bed.Take I from refrigerator.V I.Put Z into 2nd mixing bowl.N T.Fold J into mixing bowl.Put J into mixing bowl.Remove I.Fold T into mixing bowl.Put J into 2nd mixing bowl.N until ned.Fold T into mixing bowl.Put T into mixing bowl.Fold J into 2nd mixing bowl.Put J into mixing bowl.C T.Stir for 1 minute.Put Z into mixing bowl.Fold T into mixing bowl.C until ced.Fold T into 2nd mixing bowl.G T.Put T into mixing bowl.Fold T into 2nd mixing bowl.G until ged.Put I into mixing bowl.Fold T into mixing bowl.Take I from refrigerator.V until ved.Fold T into mixing bowl.L T.Put T into 2nd mixing bowl.Fold T into mixing bowl.L until led.Pour contents of 2nd mixing bowl into baking dish.

Serves 1.

-32 ไบต์ขอบคุณJonathan Allanโดยการลบtheที่ฉันไม่คิดว่ามันจะทำงาน

Chef ไม่มีประเภทสตริงดังนั้นส่วนผสมจึงเป็นจำนวนเต็มบวก 0 ใช้เพื่อแยกรายการเริ่มต้นจากส่วนผสมที่ใช้แล้วและเพื่อยุติรายการส่วนผสมที่ใช้ ดูลิงก์ TIO สำหรับตัวอย่าง

การอธิบาย Pseudocode:

A, B: stack
T, J, IN: int
T = 1
A.push(0) // used as the marker for the bottom of the stack
IN = input() // input the first list
while(IN):
    A.push(IN)
    IN = input()
IN = input() // for each used ingredient
while(IN):
    B.push(0)
    while(T): // move all ingredients up to and including the one we are moving right now to the second stack
        T = A.peek() - IN
        B.push(A.pop())
    A.push(B.pop())
    if(A.peekUnderTop() != 0):
        A.swapTopTwoItems()
    T = B.pop() // move the ingredients from the second stack back to the first
    while(T):
        A.push(T)
        T = B.pop()
    T = IN // to make it non-zero for next iteration
    IN = input(0
print(A.inverted())

ลองออนไลน์!


! น่ากลัว ภาษานี้ใช้กับ TIO มาตลอดหรือไม่?
geokavel

โดยการลบคำที่ซ้ำซ้อนออกคุณสามารถบันทึก 32 ไบต์
Jonathan Allan

@geokavel เพิ่มเมื่อวานนี้
Jonathan Allan

1
@geokavel คุณเทเนื้อหาของชามผสมลงในถาดอบก่อนเสิร์ฟหรือไม่?
NieDzejkob

1
@NieDzejkob Did you pour the contents of the mixing bowl into the baking dish before serving?ที่ฟังดูเหมือนความคิดเห็นที่สมบูรณ์แบบในการทำอาหาร SE และไม่ใช่ที่นี่: P lol (เป็นคำถามที่แปลกมากสำหรับการปรุงอาหารถ้าคุณถามฉัน: P)
HyperNeutrino

6

JavaScript, 61 ไบต์

a=>b=>b.map(v=>(p=a.indexOf(v))&&a.splice(p-1,2,a[p],a[p-1]))

รูปแบบอินพุต:

  • f (list_of_spices) (list_of_what_spices_got_used)
  • สองรายการคืออาร์เรย์ของสตริง

เอาท์พุท:

  • list_of_spices ได้รับการแก้ไขในสถานที่


5

Python 2 , 72 71 69 ไบต์

คำตอบใหม่ในจิตวิญญาณของความพยายามเดิมของฉัน

r,u=input()
for x in u:i=r.index(x);r.insert(i/~i+i,r.pop(i))
print r

ลองออนไลน์!

วิธีการแก้ปัญหาอื่น ๆ :

Python 2 , 69 ไบต์

r,u=input()
for x in u:i=r.index(x)-1;r[i:i+2]=r[i:i+2][::-1]
print r

ลองออนไลน์!


print(r)-> print r?
tsh


4

Python 2 , 80 ไบต์

def g(r,q):
 for s in q:
  i=r.index(s)
  if i:r[i-1],r[i]=r[i],r[i-1]
 return r

ลองออนไลน์!


1
บางส่วนเงินฝากออมทรัพย์: r[i-1:i+1]ใช้แท็บเพื่อเยื้องบล็อกภายในและกำหนดให้
Ørjan Johansen

การแทนที่returnด้วยprintสามารถบันทึกไบต์อื่นได้
Jonathan Frech

4

Java 8, 87 86 76 ไบต์

a->b->b.forEach(x->{int i=a.indexOf(x);a.set(i,a.set(i>0?i-1:i,a.get(i)));})

ใช้สองอินพุตเป็นArrayList<String>และแก้ไขรายการแรกแทนที่จะส่งคืนรายการใหม่เพื่อบันทึกไบต์

-10 ไบต์ขอบคุณที่@Nevay

คำอธิบาย:

ลองที่นี่

a->b->{                  // Method with two ArrayList<String> parameters and no return-type
  b.forEach(x->{         //  Loop over the second input-List
    int i=a.indexOf(x);  //   Get the index of the current item in the first input-List
    a.set(i,a.set(       //    Swap items:
      i>0?               //     If the current item is not the top item yet:
       i-1               //      Use the item above it
      :                  //     Else:
       i,                //      Use itself
         a.get(i)));     //     And swap it with the current item
  })                     //  End of loop
                         // End of method (implicit / single-line body)

1
77 ไบต์:a->b->b.forEach(x->{int i=a.indexOf(x);a.set(i,a.set(i>0?i-1:i,a.get(i)));});
2560

Java กำลังแก้แค้นให้กับทุกคนที่สนุกกับมัน
geokavel

2

05AB1E , 20 18 ไบต์

vDyk>Ig‚£`U`2(@)X«

ลองออนไลน์!

คำอธิบาย

v                    # for each used spice
 D                   # duplicate current spice order
  yk                 # get the index of the current spice
    >                # increment
     Ig‚             # pair with the number of unique spices
        £            # split the spice list into pieces of these sizes
         `           # split as 2 separate lists to stack
          U          # store the list of spices below the current one in X
           `         # push the current spice and all above separately to stack
            2(@      # swap the second item to the top of the stack
               )     # wrap in a list
                X«   # append the rest of the spices


2

C #, 125 117 81 79 ไบต์

(c,n)=>{foreach(var i in n){var j=c.IndexOf(i);if(j!=0){c[j]=c[--j];c[j]=i;}}};

ลองใช้. NET Fiddle

golfed off 36 ไบต์ด้วยraznagul


คำตอบมันเป็นจะเป็น 117 ไบต์เป็นคุณจะหายไป namespace Array.IndexOfสำหรับ แต่มีหลายวิธีที่จะทำให้คำตอบสั้นลง: 1. ใช้foreach-loop แทนfor-loop 2. ถ้าcเป็นList<string>แทนคุณโดยตรงสามารถใช้string[] c.IndexOf3. ตามที่cได้รับการแก้ไขคุณไม่จำเป็นต้องส่งคืน
raznagul


1

Mathematica, 52 ไบต์

แต่มันเป็นโพสต์แรกของฉันที่นี่ดังนั้นโปรดกรุณาถ้านับไม่ถูกต้อง :)

Keys@Sort@Merge[{PositionIndex@#,-Counts@#2},Total]&

และตัวอย่าง:

Keys@Sort@Merge[{PositionIndex@#, -Counts@#2}, Total] &[
    {pepper, paprika, salt, cumin, oregano}
  , {oregano, cumin, cumin, salt, salt, salt}
]

{เกลือ, พริกไทย, ปาปริก้า, ยี่หร่า, ออริกาโน}


ฉันไม่ใช่ผู้เชี่ยวชาญด้านคณิตศาสตร์ แต่คุณสามารถลบช่องว่างเพื่อบันทึกไบต์ได้
pajonk

@pajonk นับแล้วโดยไม่มีพวกเขา แต่ฉันควรจะลบพวกเขาที่นี่ด้วยขอบคุณ
Kuba

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