真正的香肠盛宴


35

由于十月的第一个周末临近,让我们一起庆祝慕尼黑啤酒节!

背景

您和其他一些程序员已被德国慕尼黑的当地香肠人雇用。香肠人为慕尼黑啤酒节提供巨型Volksfest需要的所有香肠。您设法偷听老板与其他员工谈论为何您和其他人被雇用而没有任何与香肠相关的经验。您意识到自己是因为无可挑剔的编程技能而被录用的-并且您的老板显然希望您为香肠分析仪编写代码。

今年左右,香肠人决定在慕尼黑啤酒节上增加香肠的种类-但他们不知道进口了多少香肠。

挑战

您需要帮助老板弄清楚他们实际进口了某种香肠。您将需要编写一个香肠分析仪,以输出每个香肠进口的香肠的种类和数量。您的老板为此购买了一个特殊的软盘驱动器,给了一根香肠,将其输送到stdin

输入值

上有许多香肠stdin,每个香肠用一个空格隔开。香肠的格式如下:

普林斯科夫(P)

 ¤
| |
| |
| |
 ¤

Salchichón(S)

 l
| |
| |
| |
| |
 l

兰贾格(L)

\ /
| |
| |
| |
| |
/ \

卡巴诺斯(K)

.
|
|
|
|
|
.

Cotechino摩德纳(C)

 ___
|   |
|   |
|   |
|___|

罗得岛(R)

 ^
| |
| |
| |
| |
 v

输出量

给定香肠的出现以及它是哪种香肠的标识符,以空格分隔。标识符是香肠名称中的第一个字母。顺序并不重要。

输出应写入stdout,允许尾随换行符和空格。

例子

输入值

 ^   ^   ^   ^  .
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
 v   v   v   v  |
                .

输出量

4R 1K

输入值

\ /  ___   l   ¤   ¤
| | |   | | | | | | |
| | |   | | | | | | |
| | |   | | | | | | |
| | |___| | |  ¤   ¤
/ \        l

输出量

1L 1C 1S 2P

输入值

 l   ¤   l
| | | | | |
| | | | | |
| | | | | |
| |  ¤  | |
 l       l

输出量

2S 1P

字节最短程序的程序员将由香肠小伙支付(胜利)!

香肠琐事

Prinskorv
Prinskorv是直接翻译为“王子香肠”的瑞典小香肠,通常通过链接出售。通常在煎锅中油炸,并配上大量芥末酱。

SalchichónSalchichón
是西班牙夏季香肠,通常用猪肉制成,尽管某些食谱使用其他肉类,包括牛,小牛肉或马肉。将肉和脂肪切成薄片,用盐,胡椒,肉豆蔻,牛至和大蒜调味,然后放入厚实的天然猪肠中。

LandjägerLandjäger
是传统上在德国南部,奥地利,瑞士和阿尔萨斯制造的半干香肠。在远足等活动中作为休闲食品而广受欢迎。它也有作为士兵食品的历史,因为它不冷藏也可以单餐食用。

Kabanos
Kabanos是波兰长而细的干香肠,由猪肉或犹太火鸡制成。它们有烟熏味,根据新鲜程度,质地可以柔软或非常干燥。Kabanosy通常只用胡椒调味。与其他肉类不同,这些香肠通常作为开胃菜单独食用,并且除非经过犹太洁食,否则通常与奶酪一起食用。

Cotechino摩德纳
Cotechino摩德纳或Cotechino di Modena是由猪肉,肥臀和猪皮制成的新鲜香肠,来自意大利摩德纳,在意大利具有PGI地位。Cotechino经常搭配小扁豆或芥末酱配土豆泥和土豆泥一起调味,尤其是在新年前后。

罗德·珀斯(Rødpølse)
Rødpølse(红色香肠)是一种鲜红色的煮猪肉香肠,在丹麦很常见。由于热狗摊在丹麦无处不在,因此有人将rødepølser视为国家美食之一。

从Wikipedia中无耻地复制了所有香肠信息


32
您正在写有关啤酒节的慕尼黑啤酒节主题挑战,但其中不包括Weißwurst吗?-1
马丁·恩德

3
我责怪那些卑鄙的香肠家伙!
sweerpotato

31
为什么将香肠信息放在扰流标签中?那是为了防止它变坏吗?
Alex A.

10
@MartinBüttner好吧,我猜你可以说...那是有史以来最糟糕的想法!
DankMemes

2
允许尾随空格。我想我的意思是写尾随空格而不是尾随换行符。现在都允许!
sweerpotato

Answers:


8

Pyth,30个字节

jdm+hd@"SKLCRP"%Ced45rS-czd\/8

在线尝试:演示测试套件

说明:

与所有其他参与者一样,我只看输入的第一行。假设输入的第一行是\ / ___ l ¤ ¤ ____

首先,我用空格分开,这给了我列表

['\\', '/', '', '___', '', '', 'l', '', '', '¤', '', '', '¤', '', '___']

现在,我们想乘搭'/'s和''s并对剩余的东西进行排序。

['\\', '___', '___', 'l', '¤', '¤']

现在,我可以对其进行长度编码。

[[1, '\\'], [2, '___'], [1, 'l'], [2, '¤']]

事实证明,这些字符或字符串的顺序(ascii值)'___' 可以很好地映射到数字[0, 1, 2, 3, 4, 5]

char/string |  l   .   \    ___    ^   ¤  
-------------------------------------------
value       | 108  46  92 6250335  94 164  
value%45    |  18   1   2      15   4  29    
(value%45)%6|   0   1   2       3   4   5     

这可以用来将它们直接映射到字母SKLCRP

jdm+hd@"SKLCRP"%Ced45rS-czd\/8
                        czd     split the input string at spaces
                       -   \/   remove "/"s (and also ""s)
                      S         sort
                     r       8  run-length-encode
  m                             map each pair d of ^ to:
   +hd                            d[0] + 
                Ced               convert d[1] to a number
               %   45             mod 45
      @"SKLCRP"                   take the ^th element in the string (mod 6)
jd                              join by spaces

19

Pyth,36 34 32 30字节

XjdsMrShM-czd\\8"¤_l/^.KRLSCP

保存的又 2个字节感谢......猜猜我是谁?:D

忽略除第一行以外的所有输入,删除所有/s和空格,将其转换为目标标识符,对其进行排序,使用游程长度编码,然后输出结果。

现场演示。

32字节版本

XjdsMrS-hMfTczd\\8"¤_l/^.KRLSCP

现场演示。

感谢@Jakube,又节省了2个字节!

34字节版本

jdsMrSX-hMfTczd\\"¤_l/^.KRLSCP")8

现场演示。

感谢@Jakube,节省了2个字节!

36字节版本

jdsMrSX-hMfTczd\/"¤_l\\^.""PCSLRK"8

现场演示。


6
我给了+1,因为我爱柯比
纳赫特-恢复莫妮卡2015年

6
我没有给你+1,因为你正好是4k代表:)
ETHproductions'October

我给了您-1,让您回到正好4k的状态:D
Beta Decay

1
另外两个字节;-) hM-czd\\-hMfTczd\\
Jakube

1
@sweerpotato看到这里。该¤是两个字节。
kirbyfan64sos

8

Javascript(ES6),105

a=>[...'¤l/._^'].map((g,h)=>(f=(a.split(g).length-1)/'222261'[h],f?f+'PSLKCR'[h]:0)).filter(b=>b).join` `

这很简单,但还是有一个解释:

input=>
  // list of all unique characters of the sausages
  [...'¤l/._^'].map((sausage_char, index)=>(
    // find all occurrences of the character in the string
    occurrences = (input.split(sausage_char).length - 1)
      / '222261'[index], // divide by the number of that character in its sausage
      // when dividing and multiplying by numbers in strings, JS automatically casts them
    occurrences ? // is there anything for this sausage?
      occurrences + 'PSLKCR'[index] : // add the sausage's letter and count
      0 // return 0 so it can be filtered out
  ))
  // filter out the 0s
  .filter(b=>b)
  // return a space-separated string
  .join` `

3
非常聪明!使用ES7的阵列内涵高尔夫这个版本,我得到了91:a=>(h=-1,[for(g of'¤l/._^')if(f=(a.split(g).length-1)/'222261'[++h])f+'PSLKCR'[h]].join` `)如果我能简化的声明h...
ETHproductions

@ETHproductions很酷!我应该学习ES7
DankMemes,2015年

8

CJam,38 35 33字节

l'_%'C*"l¤\^./""SPLRK "erS-$e`S*

在这里测试。

说明

每种类型的香肠的第一行都是唯一的,并且由于香肠在顶部对齐,因此在第一行中计算相关字符就足够了。有两种类型需要特殊处理:

  • Landjäger(L)同时具有\/。我们想要摆脱其中一个,然后我们可以像其他所有字符一样计算另一个。
  • Cotechino Modena(C)具有三个下划线,因此我们需要将下划线除以3。但是,通过简单地将输入中的下划线(始终仅属于一个香肠)替换为单独的下划线,实际上要短一些。目标角色C

现在查看实际代码:

l         e# Read one line from STDIN.
'_%       e# Split on runs of underscores.
'C*       e# Join back together by C's.
"l¤\^./"  e# Push a string with characters corresponding to each type, and a slash.
"SPLRK "  e# Push a string with the corresponding letters and a space.
er        e# Transliterate, turning each identifying character into the correct
          e# letter and all slashes into spaces.
S-        e# Remove spaces (which also gets rid of what used to be slashes).
$         e# Sort the string to group each letter.
e`        e# Run-length encode.
S*        e# Join by spaces.

无论您做什么,都不要忘记Splurk。
泰勒·洛佩兹

6

Mathematica 116

某些字节可能会被删掉,但是没有什么方法可以接近高尔夫球语言。

Row[Row/@Reverse/@Tally@ImportString[#,"Table"][[1]]/.{"¤"->"P","l"->"S","/"->"L","___"->"C","."->"K","^"->"R"}," "] &

ImportString[#,"Table"][[1]]返回出现在输入顶行的以空格分隔的字符串列表。该字符串可以包括列表中的任何元素{"¤","l","/","___",".","^"},包括重复。每个元素都与独特的香肠类型相关联。

Tally 计算每个这样的字符串出现的次数。

/.{"¤"->"P","l"->"S",...替换¤PlS等。

Reverse 将每个提示放在与之关联的项目之前。

两者Row格式化输出。


6

MATLAB 113

假设允许使用尾随空格(是的),这是一个MATLAB匿名函数:

@(a)arrayfun(@(p,m)fprintf([(m>32&&p)*'%d%c ' ''],p,m),histc(strtok(strrep(a,'___','_'),10),'./\^_l¤'),'K LRCSP')

并给出一个解释:

@(a) %Anonymous function, with an input a
    arrayfun(@(p,m) %Loop through the two input arrays (see later)
                   fprintf( %Print to console
                           [(m>32&&p)*'%d%c ' ''] %Essentially this means if p>0 and m>' ', print an integer followed by a char, then a space
                                                 ,p,m) %The values from the array input is what is getting printed
                                                      ,histc( %Make an array of how many times 
                                                                       strtok(strrep(a,'___','_'),10), %Keep only the first line (as that is where the interesting bit is) and also replace ___ with _ for the 'C'
                                                             './\^_l¤'), %these inputs appear (which will be passed in turn as the 'p' variable to cellfun)
                                                                        'K LRCSP' %These are the characters to be printed with the count representing each sausage (it will be placed in the 'm' input of arrayfun)
             )

似乎可以正常工作。仍有尾随空间,但现在可以正确处理所有香肠。


3

Perl,84 77字节

有人可能会刮掉一点...

84字节

($s=<>)=~y|_^\.l\\¤|CRKSLP|d;$$_++for split//,$s;$C/=3;for(A..Z){print"$$_$_ "if$$_}

77字节

$$_++for split//,<>=~y|_^\.l\\¤|CRKSLP|dr;$C/=3;for(A..Z){print"$$_$_ "if$$_}

分解:

取STDIN的第一行,将值音译为字母代码,删除多余的垃圾。该d修改真的不应该是必要的,但我遇到了奇怪的Unicode问题上¤没有它的性格。

使用符号引用为找到的每个字符创建和/或递增变量。

$$_++ for split //, <> =~ y|_^\.l\\¤|CRKSLP|dr;

将C变量除以3,原因是三位下划线

$C /= 3;

循环遍历字母,并打印单字母大写变量和字母(如果它们的值大于零)

for (A..Z) {
    print "$$_$_ " if $$_;
}

测试结果:http : //ideone.com/alpUlI

编辑:通过音译直接将匿名返回值传递到中,将7个字节砍掉split


2

Perl,172个字节

还可以从香肠上切出更多的Daresay,但是这里有10个开胃菜。

$a=<>;$a=~s/¤/h/g;$a=~s/_+/_/g;$a=~s/(\/| |\n)//g;$a=~s/\\/x/g;$h{$_}++ for split(//,$a);foreach (sort keys %h){print $h{$_};$_=~tr/^.hlx_/RKPSLC/;print "$_ ";}print "\n"

非高尔夫版本

$a=<>;              # get 1st line
$a=~s/¤/h/g;        # convert ¤ to h, avoid unicode hassles
$a=~s/_+/_/g;       # multiple consecutive _ to single _
$a=~s/(\/| |\n)//g; # strip / and spaces
$a=~s/\\/x/g;       # convert \\ to x to avoid regexp hassles

# build hash that counts occurences of character
$h{$_}++ for split(//,$a);

# print the answers
foreach (sort keys %h) {
 print $h{$_};
 $_=~tr/^.hlx_/RKPSLC/;
 print "$_ ";
}
print "\n";

检测结果

$ perl meaty.pl <test1.txt
1K 4R
$ perl meaty.pl <test2.txt
1C 2P 1S 1L
$ perl meaty.pl <test3.txt
1P 2S
$

2

Python 3,120个字节

我敢肯定,您可以缩短此时间,但是还没有Python解决方案,因此我们开始:

x=input()
a={z:x.count(y)for y,z in zip('¤l/.^_','PSLKRC')}
a['C']//=3
print(' '.join(str(a[x])+x for x in a if a[x]))

说明

这很简单,有人甚至说可读性,但是无论如何这是一个简短的解释:

读取输入的第一行,因为可以从第一行确定每个香肠。

现在a={z:x.count(y)for y,z in zip('¤l/.^_','PSLKRC')}是一个字典解析,它将每种类型的香肠的标识符(z)映射到每种类型的香肠的计数(x.count(y),这y是香肠定义字符)。

然后,由于三重下划线,我们将Cotechino Modena(C)香肠的数量除以3。

最后,我们打印出结果:print(' '.join(str(a[x])+x for x in a if a[x]))。这只会一次创建每个香肠的输出计数,但前提是该香肠至少出现一次(a[x]不为零=> Truthy)。每个计数字符串都由空格连接并打印出来。


打印'a [x]'+''+ a [x] for ...应该起作用(未经测试),并节省5个字节。倒退。
2015年
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.