生存还是毁灭


28

在尽可能少的字节中,确定每个给定的两个值是否与以下之一匹配:

第一价值

2      string or integer - whichever you prefer
to     case insensitive
too    case insensitive
two    case insensitive
t0     case insensitive (t zero)

第二值

b      case insensitive
be     case insensitive
bee    case insensitive
b3     case insensitive

例子

2          'Bee'            true
'2'        'b'              true
'not to'   'be'             false
'that is'  'the question'   false

5
N0 l33t?“ t0 b3”
manatwork '02

7
为什么它必须是一个函数
丹尼斯

2
@ rybo111好!那时他们似乎还不理解这个概念;)
Draco18s

13
这是莎士比亚的解决方案。当然,这是非竞争的,因为字节数无疑是巨大的……
Darrel Hoffman

1
@LuisMendo好。都可以。不需要同时支持两者。
rybo111 '16

Answers:


34

莎士比亚,4778字节

注意:此答案并不意味着会成为竞争对手

To Be or Not To Be, This is the Answer.

Hamlet, the main player in our story.
Horatio, Hamlet's guide through his internal struggles.
The Ghost, a handsome honest bold fair gentle king.
Claudius, the worthless usurper of the throne.
Ophelia, who Hamlet always writes two.
Polonius, the unfortunate third man caught between Hamlet and Claudius.
Brabantio, the greater.
Banquo, the lesser.
Emilia, the greater.
Egeus, the lesser.
Othello, the greater.
Orsino, the lesser.
Tybalt, the greater.
Titania, the lesser.
Valentine, who doubled is greater.
Viola, who doubled is lesser.

Act I: A simple question in so many words.

Scene I: Hamlet passes judgment over the cast.

[Enter Hamlet and Horatio]
Hamlet:
    Thou art the sum of a good healthy sunny warrior and a lovely day.
[Exit Horatio]
[Enter Claudius]
Hamlet:
    Thou art the sum of The Ghost and warm cute brave trustworthy hero.
[Exit Claudius]
[Enter Ophelia]
Hamlet:
    Thou art the sum of Claudius and a smooth spaceman.
[Exit Ophelia]
[Enter Polonius]
Hamlet:
    Thou art the sum of Ophelia and a plum.
[Exit Polonius]
[Enter Brabantio]
Hamlet:
    Thou art the sum of The Ghost and the sum of The Ghost and a rich kingdom.
[Exit Brabantio]
[Enter Banquo]
Hamlet:
    Thou art the sum of Brabantio and The Ghost.
[Exit Banquo]
[Enter Emilia]
Hamlet:
    Thou art the sum of Brabantio and the sum of joy and a gentle girl.
[Exit Emilia]
[Enter Egeus]
Hamlet:
    Thou art the sum of Emilia and The Ghost.
[Exit Egeus]
[Enter Othello]
Hamlet:
    Thou art the sum of Emilia and the sum of a cunning lover and the sweetest golden embroidered rose.
[Exit Othello]
[Enter Orsino]
Hamlet:
    Thou art the sum of Othello and The Ghost.
[Exit Orsino]
[Enter Tybalt]
Hamlet:
    Thou art the sum of Othello and the sum of happiness and fair fine heaven.
[Exit Tybalt]
[Enter Titania]
Hamlet:
    Thou art the sum of Tybalt and The Ghost.
[Exit Titania]
[Enter Valentine]
Hamlet:
    Thou art the sum of Tybalt and the sum of a happy day and a pony.
[Exit Valentine]
[Enter Viola]
Hamlet:
    Thou art the sum of Valentine and The Ghost.
[Exeunt]

Scene II: The beginning of Horatio's interrogation.
[Enter Hamlet and Horatio]
Hamlet:
Horatio:
    Open your mind.
    Art thou as good as Tybalt?  If so, let us proceed to Scene IV.
    Art thou as good as Titania?  If so, let us proceed to Scene IV.
    Art thou as good as Ophelia?  If not, let us proceed to Scene XII.


Scene III: Are we to?
Horatio:
    Open your mind.
    Art thou as good as The Ghost?  If so, let us proceed to Scene VII.
    Let us proceed to Scene XII.

Scene IV: Can we go further than t?
Horatio:
    Open your mind.
    Art thou as good as Claudius?  If so, let us proceed to Scene III.
    Art thou as good as Valentine?  If so, let us proceed to Scene VI.
    Art thou as good as Viola?  If so, let us proceed to Scene VI.
    Art thou as good as Othello?  If so, let us proceed to Scene V.
    Art thou as good as Orsino?  If not, let us proceed to Scene XII.

Scene V: Oone oor twoo?
Horatio:
    Open your mind.
    Art thou as good as The Ghost?  If so, let us proceed to Scene VII.
    Art thou as good as Othello?  If so, let us proceed to Scene III.
    Art thou as good as Orsino?  If so, let us proceed to Scene III.
    Let us proceed to Scene XII.

Scene VI: Hamlet desperately searches for whOo?.
Horatio:
    Open your mind.
    Art thou as good as Othello?  If so, let us proceed to Scene III.
    Art thou as good as Orsino?  If so, let us proceed to Scene III.
    Let us proceed to Scene XII.

Scene VII: Knowing to, what to do?
Horatio:
    Open your mind.
    Art thou as good as Brabantio?  If so, let us proceed to Scene VIII.
    Art thou as good as Banquo?  If not, let us proceed to Scene XII.

Scene VIII: Learning what to Bleive.
Horatio:
    Open your mind.
    Art thou as good as me?  If so, let us proceed to Scene XI.
    Art thou as good as Emilia?  If so, let us proceed to Scene X.
    Art thou as good as Egeus?  If so, let us proceed to Scene X.
    Art thou as good as Polonius?  If not, let us proceed to Scene XII.

Scene IX: The Eend is nigh?
Horatio:
    Open your mind.
    Art thou as good as me?  If so, let us proceed to Scene XI.
    Let us proceed to Scene XII.

Scene X: Wee may havee succeeeedeed.
Horatio:
    Open your mind.
    Art thou as good as Emilia?  If so, let us proceed to Scene IX.
    Art thou as good as Egeus?  If so, let us proceed to Scene IX.
    Art thou as good as me?  If not, let us proceed to Scene XII.

Scene XI: Hamlet is at peace.
Horatio:
    Thou art a beacon of happiness. 
    Let us proceed to Scene XIII

Scene XII: The demons have won.
Horatio:
    Thou art nothing.

Scene XIII: Hamlet opens up.
Horatio:
    Hamlet! Open your heart.
[Exeunt]

如果为false,则输出0;如果为true,则输出1。

这可以很容易地缩短(如果人们真的希望我这样做,可以缩短单词的长度),但是这样做对好心人是一记耳光。我一直感觉到Horatio是Hamlet的无名英雄,因此我确保他是向Hamlet传达强烈的独白的人,Hamlet必须最终证明他和Horatio一样好(他代表换行)。

代码本身非常简单。没有Hamlet的所有字符都是ascii值(顺序:)newline,space,0,2,3,B,b,E,e,O,o,T,t,V,v,然后代码是一个简单的状态机(特别是DFA),它转换Scene XI为接受状态和Scene XII拒绝状态。

This is the basic one I worked off of

完成此操作后,我将其插入莎士比亚,使用了这样的事实:当它们在数字上相邻时,我可以下降到下一个状态。我只用标题中链接的莎士比亚版本进行了测试,但是我相信我们通过实现iirc定义了一种语言。


2
当我发布这个问题时,我从未想到过我会得到如此复杂而又非常合适的答案!
rybo111 '16

18

视网膜 28

  • 感谢@MartinBüttner,节省了1个字节。

很可能是我有史以来最快的代码高尔夫球答案-OP后9分钟。

输入参数以逗号分隔。输出为真时为1,为假时为0。

i`^(2|t[ow]?o|t0),b(e?e?|3)$

在线尝试。


修复错误可以节省字节时,我总是喜欢它!
尼尔

5
@Neil是的-删除的代码是已调试的代码:)
Digital Trauma

1
@DigitalTrauma的确,在我们这个勇敢的新世界中,只有足够的 pen悔代码才能被允许。
FryAmTheEggman '16

7

Pyth,34个字节

.A}Lrw1c2c." Wô-WûÄæ­§Òé
}

在线尝试:演示

说明:

          ."...   packed string, gets decoded to: "2 TO TOO TWO T0 B BE BEE B3"
         c        split by spaces
       c2         split into 2 lists: [['2', 'TO', 'TOO', 'TWO', 'T0'], 
                                       ['B', 'BE', 'BEE', 'B3']]
   L              for each list:
     w               read a line
    r 1              convert it to uppercase
  }                  and test if it is part of this list list
.A                test if both return true

如果我以某种方式删除打包字符串之前的代码,该字符串将损坏。
busukxuan 2016年

@FryAmTheEggman我做到了。输出为“!$ 7C <Q / = / LF $ VF4K:.- E”
busukxuan

@FryAmTheEggman我尝试了几次,并比较了固定链接,但缺少了一些东西。似乎是因为我复制并粘贴了,而不是使用答案中的链接。我不知道将打包的字符串粘贴到SE中时会丢失一些字节。
busukxuan 2016年

@busukxuan是的,我只是注意到同一件事:不幸的是,P SE隐藏了大多数无法打印的字符。
FryAmTheEggman '16

1
这似乎没有通过第一个值的条件(t 0)
Nic Robertson

4

Pyth,41个字节

&xrw0c"2 to too two t0"dxrw0c"b be bee b3

在这里尝试!

直接列表查找。将空列表打印为虚假值,将非空列表打印为真值。

看起来还是更好的方式,我真的不喜欢这种方式。


4

Oracle SQL 11.2,86字节

SELECT 1 FROM DUAL WHERE:1 IN('2','to','too','two','t0')AND:2 IN('b','be','bee','b3');

对于真值,返回一行,对于假值,不返回一行。


3

05AB1E39 45字节

码:

“2€„…«Œ† t0“' ¡)Ilrk\U“b€ïÍÝ b3“' ¡)Ilrk\>X>*

在线尝试!

使用CP-1252编码。真是当数字输出时,虚假是什么都不输出。

非竞争版本(39字节),适用于最新版本:

“2€„…«Œ† t0“ð¡)IlkU“b€ïÍÝ b3“ð¡)Ilk>X>*

3

ES6,56个 48 45字节

(...a)=>/^(2|t0|t[wo]?o),b(ee?|3)?$/i.test(a)

@ user81655,节省了5个字节。通过进一步优化节省了3个字节。感谢@Patrick Roberts,又节省了3个字节。

t[wo]?o 是我想到的最短的正则表达式,可以匹配所有三个同音字。

如果允许将两个值作为单个参数数组传递,则rest参数可以成为普通参数,另外节省5个字节。


@ user81655不错,但是我进一步改进了它。
尼尔

1
如果您使用rest参数作为输入,它将为您节省3个字节:(...a)=>/^(2|t0|t[wo]?o),b(ee?|3)?$/.test(a)
Patrick Roberts

@PatrickRoberts Huh,我曾考虑过这一点,但我一定算错了,因为我不认为自己在保存任何东西。
尼尔

i标志丢失。
佛罗伦萨(Florent)

@佛罗伦萨感谢您发现这一点。(也许示例不应该全部都是小写……)
Neil

3

Perl 6,45 44字节

感谢IRC中的人帮助我打高尔夫球

{@_~~(~2|/:i^t[0|oo?|wo]$/,/:i^b[ee?|3]?$/)}

用法

> my &f = {@_~~(~2|/:i^t[0|oo?|wo]$/,/:i^b[ee?|3]?$/)}
-> *@_ { #`(Block|309960640) ... }
> f("2", "Bee")
True
> f("2", "b")
True
> f("not to", "be")
False
> f("that is", "the question")
False

非竞争替代,54字节

如果您认为正则表达式很繁琐,那么这是上述方法的一个不错的选择,但是要稍长一些。它可以向下打几个字节,但是由于它没有竞争性,所以我将其保留。

{@_».lc~~(qw<to too two t0 2>.any,<b be bee b3>.any)}

1
对于任何不了解Perl 6 []在正则表达式中表示非捕获组的人,该组都将用Perl 5 (?:和编写)。要获得字符类,必须将其放在尖括号<[和中]>
布拉德·吉尔伯特b2gills '16

b2gills ++,我什至没想到要提到Perl 6正则表达式中的差异
热键

我投票失败,因为有人不知道,所以我总是指出这一点。
布拉德·吉尔伯特b2gills '16

是否有PP&CG规则阻止您发布更为优雅的高尔夫作为自己的答案?我认为稍长一点的版本是值得的。*我据此颁布法令,优雅的高尔夫习惯用法包括不计较(不计入)空白。
雷夫

3

Python 2.7,133个字节

def e(a, b):
        c, d = """2,too,to,t0,two""","""be,b,bee,b3"""
        return a.lower() in c and b.lower() in d

print e('2', 'bee')

不确定注释中是否有较小的版本,是否应该发布解决方案,但这是我的Python版本。

编辑:没有该功能,它只有73个字节(而且甚至还没有达到最佳答案。请原谅我是新手

a, b = "to", "bee"
print a in "2 too to t0 two" and b in "be b bee b3"

2
欢迎来到PPCG!如果您花时间写和打高尔夫球的答案,并且没有窃(没有署名),那么欢迎您发布答案!
Digital Trauma

哦,大多数语言在这里都有关于打高尔夫球技巧的问题。Python 也不例外
Digital Trauma

请注意,这确实适用于2(整数)的输入
wnnmaw

3
对于e('oo', '3')类似的子字符串,也返回True 。
manatwork '16

1
@ TheLateOne,Python不是很容易打高尔夫球,因此您几乎永远不会击败pyth或cjam等高尔夫语言,但是在大多数挑战中进入50、60或70的范围对我们来说是非常好的。不要气aged!
wnnmaw '16

2

Ruby,53 55 52字节

f=->(a,b){/^(2|t[wo]?o|t0)$/i=~a&&/^b(e?e?|3)$/i=~b}

老实说,这是我第一次尝试打高尔夫球。

函数调用的形式 f.call(firstValue, secondValue)

0 是真实的 nil是虚假的。

在这里测试


1

Japt,36个字节

!Uv r"2|t(0|wo|oo?)" «Vv r"b(e?e?|3)

也许我错过了一些东西,但这应该可以正常工作。在线测试!



1

Python,85 83字节

@Manatwork为我节省了两个字节。

这是蛮力的,接下来我将研究正则表达式解决方案。

lambda a,b:a.lower()in'2 to too two t0'.split()and b.lower()in['b','be','bee','b3']

1
对于第一个数组:'2 to too two t0'.split()对于第二个数组没有区别。
manatwork '16

该死,是的,我尝试过'2,to,too,two,t0'.split(',')。傻我
Ogaday'2

1

PowerShell v3 +,74个 70字节

param($a,$b)+($a-in-split'2 to too two t0')*($b-in-split'b be bee b3')

不使用正则表达式。接受两个输入,检查第一个是否是-in-split操作员动态创建的数组,将Boolean转换为int +,然后乘以*检查第二个是否为第二-in个数组(将自动将Boolean转换为int) )。之所以有效,是因为x*y == x&yif xy只能是1or 0

默认情况下,PowerShell不区分大小写,因此我们免费提供。分别输出01伪造/真实。需要v3或更高版本的-in运营商。

编辑-使用一元拆分保存4个字节


0

Groovy,52个字节

f={x,y->"$x $y"==~"(?i)(2|t([wo]o?|0)) (b(ee?|3)?)"}

==~ groovy中的一个很酷的正则表达式运算符,用于检查相等性。

测试:

Regex101测试。

assert f('2', 'Bee') == true
assert f('2', 'b') == true
assert f('not to', 'be') == false
assert f('that is', 'the question') == false

如果xty0b什么?
Neil 2016年

你什么意思?
J Atkin

我希望f('t', '0b')它是错误的,但是我担心您的代码将返回true。
Neil

哦,好,我明白了。是的,它将返回错误的内容。
J Atkin

0

MATL,32 41 43字节

jk'^(2|t[ow]?o|t0),b(e?e?|3)$'XX

@DigitalTrauma的Retina答案相同的方法。输入用逗号分隔。Truthy输出是一个字符串,其中两个输入均小写;falsy没有输出。

在线尝试!

j                                % input as a string
k                                % convert to lowercase
'^(2|t[ow]?o|t0),b(e?e?|3)$'     % regular expression to match the two inputs
XX                               % match regular expression

0

C#6,132字节

bool t(string x,string y)=>new[]{"2","to","too","two","t0"}.Contains(x.ToLower())&&new[]{"b","be","bee","b3"}.Contains(y.ToLower());

非高尔夫版本(仅可读性更高):

bool t(string x, string y) => new[] { "2", "to", "too", "two", "t0" }.Contains(x.ToLower()) && new[] { "b", "be", "bee", "b3" }.Contains(y.ToLower());

0

Python 2,67个字节

使用Digital Trauma的正则表达式。输入是由逗号分隔的单个字符串。不知道该格式是否允许输入...

import re
f=lambda x:bool(re.match('^(2|t[ow]?o|t0),b(e?e?|3)$',x))
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.