用代码烹饪


24

您是厨师,并且喜欢用香料烹饪,但是最近您很喜欢根据香料的使用频率来组织香料。但是上次使用香料时,您没有时间写下来。只需简单地交换和移动香料,这似乎可以解决问题。

但是,您当然是厨师,这意味着您可以和一些厨师在一起。您决定告诉他们与香料接触的简单规则。

  1. 如果您最近使用过香料,则将其在香料架中向上移动

  2. 如果您根本不使用任何香料,例如[]空的移动列表,那么香料列表将不受影响。

  3. 您可以将任何香料放入我的香料容器中,但是如果使用它,请确保将其移动。

  4. 该列表可以包含任何内容。但是因为这些是我们正在使用的香料。最好使用香料名称。

  5. 香料应该是独特的。太多相同的香料破坏了汤的味道……或者俗话说

正常的代码高尔夫球规则适用。

牛至被反复使用的例子。

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 '17

31
我在厨师那里做得很远,但最终太累了!如果可以的话,我会给某人50英镑的赏金。
geokavel

5
这是将输入内容输入搅拌碗的要点。其余的将非常困难,但对合适的人而言是可行的!
geokavel

1
@geokavel 挑战接受
NieDzejkob

Answers:


4

外壳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比较)类型。

用作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从(香料架)列表开始,sm使用运算符将其与每个元素(香料)按顺序组合?
  • 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链接。

伪代码说明:

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它是昨天添加的。
乔纳森·艾伦

1
@geokavel服务之前,您是否将搅拌碗中的内容物倒入烤盘中?
NieDzejkob

1
@NieDzejkob Did you pour the contents of the mixing bowl into the baking dish before serving?听起来完全像是在烹饪SE上发表的评论,而不是在这里:P哈哈(如果您问我:P,这也是一个非常奇怪的烹饪问题
:)

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被就地修改。




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)));});
Nevay

Java正在为所有取笑Java的人报仇。
geokavel

2

05AB1E20 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上尝试

多亏了raznagul,打出了36个字节


答案是117个字节,因为您缺少的命名空间Array.IndexOf。但是有几种方法可以缩短答案:1.使用foreach-loop而不是for-loop。2.如果cList<string>,则string[]可以直接使用代替c.IndexOf。3. 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}
]

{盐,胡椒粉,辣椒粉,小茴香,牛至}


我不是Mathematica专家,但是您可以删除一些空间以节省一些字节。
pajonk

@pajonk已经没有它们了,但我也应该在这里删除它们,谢谢。
库巴岛

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.