科学怪人的名字


15

介绍:

由于快到万圣节了,所以我只是想一些随机的以万圣节为主题的挑战。最终,我阅读了科学怪人的维基百科页面,并看到以下文本:

生物

科学怪人拒绝其创作的部分原因是他没有给它起名字,这导致缺乏身份。取而代之的是用诸如“ wretch”,“ monster”,“ creature”,“ demon”,“ devil”,“ fiend”和“ it”之类的词来指代。当科学怪人在第10章中与该生物交谈时,他称其为“邪恶的昆虫”,“憎恶的怪物”,“恶魔”,“恶魔”和“憎恶的恶魔”。

资源

挑战:

给定两个整数(年份和章节),根据以下规则输出字符串:

  1. 如果年份小于1818:输出一个空字符串
  2. 如果年份恰好是1818,并且该章低于10:输出a 从列表中随机字符串["wretch", "monster", "creature", "demon", "devil", "fiend", "it"]
  3. 如果年份恰好是1818,并且章节是10或更高:输出a 从列表中随机字符串["vile insect", "abhorred monster", "fiend", "wretched devil", "abhorred devil"]
  4. 是1818年(因此该书出版)以上的年份,归还"Frankenstein's Monster"

挑战规则:

  • 您可以选择以小写,大写还是组合输出。
  • 年份将始终在范围内 1500-2017
  • 本章将始终在范围内 1-24(如果我用正确的Google搜索,则该书有24章)
  • 相反输出用于选项1一个空字符串,你也允许输出nullundefinedfalse0,或者你选择的任何其他价值的其他三个选项不属于(请注明你在你的答案用什么)。
  • 输出中可能不包含任何前导或尾随空格/制表符,也不能包含任何前导新行。它可能包含一个可选的尾随换行符。

通用规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您使用非代码高尔夫球语言发布答案。尝试针对“任何”编程语言提出尽可能简短的答案。
  • 标准规则适用于您的答案,因此允许您使用STDIN / STDOUT,具有适当参数的函数/方法,完整程序。你的来电。
  • 默认漏洞是禁止的(尤其是“伪随机数”)。
  • 如果可能的话,请添加一个带有测试代码的链接。
  • 另外,如有必要,请添加说明。

测试用例:

year,chapter possible outputs:

1700,1       ""; null; undefined; false; 0; etc.
1700,12      ""; null; undefined; false; 0; etc.
1817,10      ""; null; undefined; false; 0; etc.
1818,1       "wretch"; "monster"; "creature"; "demon"; "devil"; "fiend"; "it"
1818,9       "wretch"; "monster"; "creature"; "demon"; "devil"; "fiend"; "it"
1818,10      "vile insect"; "abhorred monster"; "fiend"; "wretched devil"; "abhorred devil"
1818,11      "vile insect"; "abhorred monster"; "fiend"; "wretched devil"; "abhorred devil"
1818,18      "vile insect"; "abhorred monster"; "fiend"; "wretched devil"; "abhorred devil"
1819,1       "Frankenstein's Monster"; "frankenstein's monster"; "FRANKENSTEIN'S MONSTER"
1819,18      "Frankenstein's Monster"; "frankenstein's monster"; "FRANKENSTEIN'S MONSTER"
2017,24      "Frankenstein's Monster"; "frankenstein's monster"; "FRANKENSTEIN'S MONSTER"

Answers:


3

Perl 5中,156个 153 151字节

-3个字节,感谢DomHastings。

与运行 -p

$_=($d=devil,$m=monster,$w=wretch,<"{creature,demon,it,fiend,vile insect,{abhorr,$w}ed {$d,$d,$m},Frankenstein's $m}">)[/818/?6*/,../+rand 7:$_/606-5]

此输出分布不均匀,默认情况下接受。具体来说,如果日期是1818,并且章数≥10,则名称“憎恶的魔鬼”和“憎恶的怪物”的权重是其两倍。这让我用rand 7即使列表大小不同,用于两种情况。

1818年之前几年的产出是“怪兽”。尽管有一些相似之处,但该字符串不是其他三个选项中的任何一个,因此可以使用。

超出1212-2424范围的年份将失败,但是幸运的是,该规则保证年份在1500-2017范围内。

在线尝试!


我完全不知道括号扩展在Perl中是什么……使用这种方法,对于已知的挑战有很好的解决方案!在线尝试!
Dom Hastings

@DomHastings哦,我不知道您可以引用这样的空格。这是-3个字节!
Grimmy

很高兴我能够提供帮助!我一定要尝试记住这一点,我想这是因为<...>假设存在问题吗?
Dom Hastings

1
<>是readline或glob,具体取决于…东西(经典Perl:语法不明确,但解析器尝试DWYM)。当它是一个glob时,它会紧密模仿shell glob,包括在shell中常见的括号扩展。
Grimmy

您能否为您的答案添加一个TIO链接。:)目前,这是最短的答案,但是我只是想确保在我接受最短的答案之前一切正常。
凯文·克鲁伊森

9

Python 2中227个 220 214字节

lambda y,c:[[choice([["wretch",m,"creature","demon",d,F,"it"],["vile insect",a+m,F,"wretched "+d,a+d]][c>9]),"Frankenstein's "+m][y>1818],0][y<1818]
from random import*
m,d,F,a='monster','devil','fiend',"abhorred "

在线尝试!


1
您也可以通过'abhorred '在底部的列表中再保存6个字节,因为该列表已被使用两次。('wretch'也可以,但字节数保持不变。)
Kevin Cruijssen

6

Perl 5,172个字节

171字节代码+ 1 for -p

@a=<><10?($}=wretch,$M=monster,creature,demon,$D=devil,fiend,it):("vile insect",($A="abhorred ").$M,fiend,"$}ed $D",$A.$D);$_=($a[rand@a],"frankenstein's $M",0)[$_<=>1818]

在线尝试!

说明

相当标准,如果输入小于,等于或大于以返回源数据的最后一个,第一个或第二个索引,则使用'spaceship operator'(<=>1818来返回-10或。另外,我喜欢鱼操作员1$_1818


我本来想不使用$F=fiend,但是经过测试,这样做的长度是一样的。
卡米尔·德拉卡里

@KamilDrakari是的,由于标记的原因,长度必须为六个或更多$:(
Dom Hastings

$}什么是变量名...
Titus

@Titus是 几乎所有方法都可以使用,尽管许多方法具有特殊的意义。我只用它那里,好像我用$W我会需要使用${W},因为它的旁边另一封信。
Dom Hastings

1
我认为。还是要注意:)
Titus

4

C#(Visual C#中编译器)225个 209字节

y=>c=>y<1818?"":y>1818?"frankenstein's monster":"wretch,monster,creature,demon,devil,it,fiend,vile insect,abhorred monster,wretched devil,abhorred devil".Split(',')[new System.Random().Next(c>9?6:0,c>9?11:7)];

在线尝试!

-16感谢Kevin Cruijssen

并不是特别简洁,但是我希望C#可以像使用Python答案那样占用更多空间来声明和使用任何重复的字符串,而不是仅仅重复它们。包含的测试运行程序具有测试用例的Thread.Sleep(1)内部枚举,因为默认的种子是Random()使用当前时间,并且实际函数运行得足够快,从而可以在随后的运行中以相同的种子最终结束而没有延迟。

对该函数的调用使用currying语法f(year)(chapter)(或函数存储在其中的任何名称而不是f)


2
我看到您在发布挑战时应用了与我想到的相同的技巧,将其放置fiend在单个字符串数组的中间,因为它出现在两个列表中。您可以new[]{"wretch","monster","creature","demon","devil","it","fiend","vile insect","abhorred monster","wretched devil","abhorred devil"}"wretch,monster,creature,demon,devil,it,fiend,vile insect,abhorred monster,wretched devil,abhorred devil".Split(',')(-16字节)替换来节省一些字节
Kevin Cruijssen 17-10-24

@KevinCruijssen好的技巧,它不会立即显得有利的,但改变从分隔符",",只是,弥补了较长.Split(',')相比,new[]{}与以上几个项目。
卡米尔·德拉卡里

3

击,229206197,179个字节

m=monster
a=("Frankenstein's $m" wretch $m creature demon devil it fiend vile\ insect {abhorr,wretch}ed\ {devil,$m})
echo "${a[$1>1818?0:$1<1818?13:RANDOM%($2>9?5:7)+($2>9?7:1)]}"

关于括号/变量扩展的解释

m=monster
{abhorr,wretch}ed\ {devil,$m}

首先扩展到

abhorred\ {$m,devil} wretched\ {devil,$m}
abhorred\ $m abhorred\ devil wretched\ devil wretched\ $m

然后变量m被展开

abhorred\ monster abhorred\ devil wretched\ devil wretched\ monster

但是,下面的命令不起作用,因为$w将首先与ed和连接,并且wed未定义,并且将其扩展为空字符串。

m=monster
w=wretch
{abhorr,$w}ed\ {devil,$m}

可以添加带有大括号的显式花括号w,但是会更长

m=monster
w=wretch
{abhorr,${w}}ed\ {devil,$m}

蒂奥


通过为wretch和创建变量monster225个字节),可以节省4个字节
凯文·克鲁伊森

由于撑杆膨胀发生在变量膨胀之前而并非那么简单
Nahuel Fouilleul

我(几乎)从来没有用Bash编程过,所以您能澄清一下这是什么意思吗?它似乎可以在TIO中运行,但是由于编程语言的行为方式,也许我错过了一些东西。
凯文·克鲁伊森

1
@KevinCruijssen感激您,当牙套是最后的元件扩展工作时,我学到了一些知识
Nahuel Fouilleul

1
@DomHastings,是的,已解决
Nahuel Fouilleul

2

APL(Dyalog Unicode),189 字节

m'monster'
a'abhorred ',⊢
f'fiend'
d'devil'
{⊃⍵=1818:{⊃⌽⍵<10:(?7)⊃'wretch'm'creature' 'demon'd f'it'⋄(?5)⊃'vile insect'(a m)f('wretched ',⊢d)(a d)}⍵⋄{⊃⍵>1818:'frankenstein''s ',⊢m''}⍵}

在线尝试!

要进行测试,只需将测试用例附加到最后一个大括号即可,如下所示:

{⊃⍵=1818:{⊃⌽⍵<10:(?7)⊃'wretch'm'creature' 'demon'd f'it'⋄(?5)⊃'vile insect'(a m)f('wretched ',⊢d)(a d)}⍵⋄{⊃⍵>1818:'frankenstein''s ',⊢m''}⍵}1818 10

非常感谢@ngn帮助解决领先的空白问题。

怎么运行的:

值得注意的是,APL将字符串解释为字符的数组(向量),因此'wretch' 'creature'将在其他语言中解释为[[w,r,e,t,c,h],[c,r,e,a,t,u,r,e]]

这些是助手功能:

m'monster'      #These are pretty self explanatory, variables m, f and d are being set to 'monster', 'fiend' and 'devil respectively.
f'fiend'
d'devil'
a'abhorred ',⊢  # a is being set to 'abhorred ' concatenated with the argument to it's right.

这是主要功能,为了更好的理解,它将分为三个部分:

{⊃⍵=1818:{⊃⌽⍵<10:(?7)⊃'wretch'm'creature' 'demon'd f'it'⋄(?5)⊃'vile insect'(a m)f('wretched ',⊢d)(a d)}⍵⋄{⊃⍵>1818:'frankenstein''s ',⊢m''}⍵}

第1部分:

{⊃⍵=1818:{Part2}⍵⋄{Part3}⍵} # First conditional
        :                   # If
                           # the first element
                           # of the argument ⍵
   =1818                    # Equals the literal 1818
         {Part2}⍵           # Executes part 2 with argument ⍵
                           # Else
                  {Part3}⍵  # Executes part 3 with argument ⍵

第2部分:

{⊃⌽⍵<10:(?7)⊃'wretch'm'creature' 'demon'd f'it' # Second conditional
(?5)⊃'vile insect'(a m)f('wretched ',⊢d)(a d)}⍵                
       :                                         # If
                                                # the first element
                                                # of the inverse
                                                # of the argument ⍵
    <10                                          # is less than 10
                                                # pick the nth
       (?7)                                      # random element between [1,7]
            'wretch'm'creature' 'demon'd f'it'   # from this string array
                                                # Else
                                                # pick the nth
(?5)                                             # random element between [1,5]
     'vile insect'(a m)f('wretched ',⊢d)(a d)    # from this string array
                                             }⍵  # using argument ⍵

第3部分:

{⊃⍵>1818:'frankenstein''s ',⊢m''}⍵ # Third conditional
        :                           # If
                                   # the first element
                                   # of the argument ⍵
   >1818                            # is greater than 1818
         'frankenstein''s ',⊢m      # print the string "frankenstein's" concatenated with m (which prints monster)
                                   # else
                               ''   # print an empty string
                                 }⍵ # with argument ⍵

2

的Java(OpenJDK的8) 275个 269 255 207 203字节

y->c->y<1818?0:y>1818?"Frankenstein's monster":"wretch;monster;creature;demon;devil;it;fiend;vile insect;abhorred monster;wretched devil;abhorred devil".split(";")[c+=Math.random()*(c>9?5:7)+(c>9?6:0)-c]

在线尝试!




1
203个字节。使用IntFunctions代替Function<Integer>使其工作。
奥利维尔·格雷戈尔(OlivierGrégoire),

2

PHP,184176字节

<?=strtr([0,1,2,Creature,Demon,It,Fiend,30,31,"2ed 1","Vile Insect","Frankenstein´s 0"][[rand(6*$x=$argv[2]>9,6+4*$x),11][$argv[1]<=>1818]],[Monster,Devil,Wretch,"Abhorred "]);

运行-n并提供Year和Chapter作为命令行参数,或者在线尝试

指数,因此结果评估NULL为<1818年,导致空输出。


怎么$argv[1]<=>1818办?我从未见过。
YU NO WORK


1

Javascript(ES6),205个字节

a="abhorred ",d="devil",f="fiend",m="monster"
y=>c=>y<1818?0:y>1818?"frankenstein's "+m:(l=c<10?[f,"wretch",m,"creature","demon",d,f,"it"]:["vile insect",a+m,f,"wretched "+d,a+d])[0|Math.random()*l.length]

0如果年份小于1818,则返回


1

Javascript 199字节

M="monster"
A=(y,c)=>y<1818?"":y^1818?"Frankenstein's "+M:[W="wretch",M,"creature","demon",D="devil",F="fiend","it","vile insect",(H="abhorred ")+M,F,W+"ed "+F,H+D][Math.random()*(c>9?5:7)+7*(c>9)|0]

console.log(A(1700,1))
console.log(A(1700,12))
console.log(A(1817,10))
console.log(A(1818,1))
console.log(A(1818,9))
console.log(A(1818,10))
console.log(A(1818,11))
console.log(A(1819,1))
console.log(A(1819,18))
console.log(A(2017,24))


1

C,380字节

void ok(int *year, int* chapter){char*below_10[] = {"wretch", "monster", "creature", "demon", "devil", "fiend", "it"}; char*at_10[] = {"vile insect", "abhorred monster", "fiend", "wretched devil", "abhorred devil"}; year < 1818?printf("0"):(year == 1818?(chapter<10 printf("%s",below_10[rand()%8]):printf("%s",at_10[rand()%7])):printf("Frankenstein's Monster"));}

4
嗨,欢迎来到PPCG!在代码高尔夫球挑战中,目标是拥有尽可能少的字节。因此,方法/变量名称最好是单个字符。此外,您可以删除所有不必要的空格和换行符,并"Frankenstein's Monster"直接使用direct而不是为其创建变量。此外,这里是yearchapter未来从现在开始?我不认为它是该方法的参数。<所有语言> 中打高尔夫球的技巧C中打高尔夫球的技巧可能会有用。入住愉快!:)
Kevin Cruijssen

没问题。你可以高尔夫多一点:void o(int y,int c){char*b[]={"wretch","monster","creature","demon","devil","fiend","it"},*a[]={"vile insect","abhorred monster","fiend","wretched devil","abhorred devil"};printf("%s",y<1818?0:y==1818?c>9?a[rand()%6]:b[rand()%7]:"Frankenstein's Monster");} 256个字节 这里试试吧!(我删除了所有不必要的空格,将方法/变量/参数重命名为单个字母;在三进制-if上删除了一些不必要的括号;单printf;更改<10>9;等。)不确定为什么rand()在TIO中不起作用。:S
Kevin Cruijssen


1

MBASIC,288字节

1 DIM R$(12):FOR I=1 TO 12:READ R$(I):NEXT:INPUT Y,C:IF Y<1818 THEN 5
2 IF Y>1818 THEN O$="Frankenstein's Monster":GOTO 5
3 IF C>9 THEN O=7
4 O$=R$(O+INT(RND(1)*N+1))
5 PRINT O$:DATA wretch,monster,creature,demon,devil,fiend,it,vile insect,abhorred monster,fiend,wretched devil,abhorred devil

0

05AB1E114个 110 字节

0…뢼–±¹.•ʒ¦Ë"ìËM₂G¶тá≠•ì#.•1ÏiT¥Ƶʒ“T¡Äg… 5ÀΘO¼™Ãí?ιË%4₄‡‡λYιi¾н••Jtm•17в£‚…'s 'ì³R7£R’¡¬ÿÿ±¹’¸¸)sŽ7X.S>èsT@èΩ

0年份低于时的输出1818

在线尝试验证所有测试用例

说明:

0                    # Push a 0
…뢼–±¹              # Push dictionary string "creature devil monster"
 .•ʒ¦Ë"ìËM₂G¶тá≠•   "# Push compressed string "demon fiend it wretch "
  ì                  # Prepend it: "demon fiend it wretch creature devil monster"
   #                 # Split by spaces: ["demon","fiend","it","wretch","creature","devil","monster"]
.•1ÏiT¥Ƶʒ“T¡Äg 5ÀΘO¼™Ãí?ιË%4₄‡‡λYιi¾н•
                     # Push compressed string "abhorred devilabhorred monsterfiendvile insectwretched devil"
 Jtm               # Push compressed integer 1249548
      17в            # Converted to Base-17 as list: [14,16,5,11,14]
         £           # Split the string into parts of that size: ["abhorred devil","abhorred monster","fiend","vile insect","wretched devil"]
                    # Pair both string-lists together
's                 '# Push string "'s "
   'ì³              '# Push dictionary string "liechtenstein"
      R7£R           # Leave the last 7 characters: "enstein"
          ’¡¬ÿÿ±¹’   # Push dictionary string "frankÿÿmonster", where the "ÿ" are automatically
                     # replaced with the strings at the top of the stack: "frankenstein's monster"
                  ¸¸ # Wrap it twice into a list: [["frankenstein's monster"]]
)                    # Wrap everything on the stack into a list
                     # (We now have: [0,[["demon","fiend","it","wretch","creature","devil","monster"],["abhorred devil","abhorred monster","fiend","vile insect","wretched devil"]],[["frankenstein's monster"]]])
 s                   # Swap to get the first (implicit) input
  Ž7X                # Push compressed integer 1818
     .S              # Compare it with the input (-1 if below; 0 if equal; 1 if above)
       >             # Increase it by 1 (0 if below; 1 if equal; 2 if above)
        è            # Use it to index into the list
         s           # Swap to get the second (implicit) input
          T@         # Check if it's larger than or equal to 10 (results in 0 for falsey; 1 for truthy)
            è        # Use it to index into the inner list
             Ω       # And finally get a random value from that list (which is output implicitly)

请参阅我的05AB1E技巧(各节如何使用字典?如何压缩不属于字典的字符串?如何压缩大整数?;以及如何压缩整数列表?了解原因:

  • …뢼–±¹"creature devil monster";
  • .•ʒ¦Ë"ìËM₂G¶тá≠•"demon fiend it wretch ";
  • .•1ÏiT¥Ƶʒ“T¡Äg… 5ÀΘO¼™Ãí?ιË%4₄‡‡λYιi¾н•"abhorred devilabhorred monsterfiendvile insectwretched devil";
  • •Jtm•1249548•Jtm•17в[14,16,5,11,14];
  • 'ì³"liechtenstein ";
  • ’¡¬ÿÿ±¹’"frankÿÿmonster";
  • Ž7X1818

-1

JavaScript,194 B

e=Math.random,f=Math.floor;x=(a,b,c=`insect,monster,wretch,creature,demon,devil,fiend,it`.split`,`,d=[`vile `,`abhorred `])=>a<1818||a-1818?`Frankenstein's `+c[1]:(b<10||c[f(e()*2)])+c[f(e()*8)]

与测试用例不符
DanielIndie
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.