To be or not to be

28

3

In the fewest bytes possible, determine whether the two values given each match one of the following:

First value

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

Second value

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

Examples

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

rybo111

Posted 2016-02-10T20:15:21.817

Reputation: 4 071

Generally return values are considered valid if truthy or falsy.

– Draco18s no longer trusts SE – 2016-02-10T20:17:37.783

5N0 l33t? “t0 b3” – manatwork – 2016-02-10T20:18:22.167

@Draco18s I had so many questions last time I posted a question I thought it best to make it very clear. – rybo111 – 2016-02-10T20:18:24.783

7Why does it have to be a function? – Dennis – 2016-02-10T20:19:32.017

2@rybo111 Well! That seems like they didn't understand the concept then ;) – Draco18s no longer trusts SE – 2016-02-10T20:19:34.503

@manatwork Good point, added. – rybo111 – 2016-02-10T20:19:53.170

@Dennis Edited. – rybo111 – 2016-02-10T20:29:43.717

Can we take one string as input, with one space between the args? – J Atkin – 2016-02-10T21:02:26.230

@FryAmTheEggman I think they whole thing should match one of those strings. – lirtosiast – 2016-02-10T21:05:19.340

@FryAmTheEggman The first and second values must both match one of the listed values. not to is not in the first list, so your example would be false. – rybo111 – 2016-02-10T21:12:14.210

Why is 2 not in quotes? – J Atkin – 2016-02-10T21:38:40.677

@JAtkin It's an integer example – rybo111 – 2016-02-10T21:50:58.133

Wait, we have to handle integers as input? (not a prob for my impl, but still a surprise) – J Atkin – 2016-02-10T21:52:51.197

13

This one begs for a Shakespeare solution. Non-competing of course, as the byte count would undoubtedly be huge...

– Darrel Hoffman – 2016-02-10T22:15:24.147

@DarrelHoffman I imagine one of the creators would like to attempt that! – rybo111 – 2016-02-10T22:24:26.057

@rybo111 It appears some of the answers are failing to permit the first input to be either a string '2' or an integer 2. Maybe you should make that clearer in the challenge spec – Luis Mendo – 2016-02-10T22:25:09.617

1@LuisMendo OK. Either is fine. Supporting both is not necessary. – rybo111 – 2016-02-10T22:26:43.933

That is the question indeed. – Zizouz212 – 2016-02-12T12:16:13.873

Answers

34

Shakespeare, 4778 Bytes

Note: this answer is not meant to be a serious competitor

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]

Outputs 0 if false, 1 if true.

This could easily be shorter (and if people really want me to, the word lengths could be cut down), but to do so would be a slap to the face of good ol' Will. I've always felt Horatio is the unsung hero of Hamlet, so I made sure that he was the one to deliver the intense monologue to Hamlet where Hamlet has to ultimately prove that he is as good as Horatio (who represents the newline).

The code itself is pretty simple. All of the characters sans Hamlet are ascii values (In order:newline,space,0,2,3,B,b,E,e,O,o,T,t,V,v) and then the code is a simple state machine (specifically, a DFA) that transitions to Scene XI as an accept state and Scene XII as a reject state.

This is the basic one I worked off of.

After making this, I just plugged it into Shakespeare, using the fact that I could drop down to the next state when they were numerically adjacent. I only tested it with the version of Shakespeare I linked in the title, but I believe we define a language by an implementation iirc.

Kevin W.

Posted 2016-02-10T20:15:21.817

Reputation: 763

2When I posted this question, I never envisaged I would get such a complex and yet very fitting answer! – rybo111 – 2016-02-12T12:16:03.287

18

Retina, 28

  • 1 byte saved thanks to @MartinBüttner.

Quite possibly my quickest ever code-golf answer - 9 minutes after OP.

Input parameters are comma-separated. Output is 1 for truthy and 0 for falsey.

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

Try it online.

Digital Trauma

Posted 2016-02-10T20:15:21.817

Reputation: 64 644

I always love it when fixing bugs saves bytes! – Neil – 2016-02-10T21:15:02.753

5@Neil yep - deleted code is debugged code :) – Digital Trauma – 2016-02-10T21:21:25.030

1

@DigitalTrauma Truly, only sufficiently penitent code can be allowed in our brave new world.

– FryAmTheEggman – 2016-02-10T21:24:25.803

7

Pyth, 34 bytes

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

Try it online: Demonstration

Explanation:

          ."...   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

Jakube

Posted 2016-02-10T20:15:21.817

Reputation: 21 462

Somehow if I remove the code before the packed string, the string becomes broken. – busukxuan – 2016-02-10T21:53:40.110

@FryAmTheEggman I did. The output was "!$7C<Q/=/LF$VF4K:.-E" – busukxuan – 2016-02-10T21:55:48.257

@FryAmTheEggman I tried a few times and compared the permalink, there was something missing. It seems it's because I copied and pasted, rather than using the link in the answer. I didn't know you'll miss some bytes when pasting packed strings into SE. – busukxuan – 2016-02-10T22:01:52.773

@busukxuan Yeah I just noticed the same thing :P SE hides most unprintable characters unfortunately. – FryAmTheEggman – 2016-02-10T22:02:47.930

1This doesn't seem to pass the condition (t zero) for the first value – Nic Robertson – 2016-02-11T03:22:57.773

@NicRobertson Your right. No idea how I forgot about this case. Fixed it for a cost of 2 bytes. – Jakube – 2016-02-11T08:52:23.580

4

Pyth, 41 bytes

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

Try it here!

Straightforward list lookup. Prints an empty list as falsy value and a non-empty list as truthy value.

Looking or a better way tho, I don't really like this one.

Denker

Posted 2016-02-10T20:15:21.817

Reputation: 6 639

4

Oracle SQL 11.2, 86 bytes

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

Returns one row for truthy and no row for falsey.

Jeto

Posted 2016-02-10T20:15:21.817

Reputation: 1 601

3

ES6, 56 48 45 bytes

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

Saved 5 bytes thanks to @user81655. Saved 3 bytes from some further optimisation. Saved another 3 bytes thanks to @Patrick Roberts.

t[wo]?o is the shortest regex I could think of to match all three homophones.

If it's permitted to pass the two values as a single parameter array, then the rest parameter can become a normal parameter, saving another 5 bytes.

Neil

Posted 2016-02-10T20:15:21.817

Reputation: 95 035

@user81655 Not bad, but I've further improved it. – Neil – 2016-02-10T21:24:52.223

1if you use the rest parameter for input, it will save you 3 bytes: (...a)=>/^(2|t0|t[wo]?o),b(ee?|3)?$/.test(a) – Patrick Roberts – 2016-02-10T22:33:12.813

@PatrickRoberts Huh, I had thought about that, but I must have miscalculated, because I didn't think I was saving anything. – Neil – 2016-02-10T23:53:05.330

The i flag is missing. – Florent – 2016-02-11T18:58:57.383

@Florent Thanks for spotting that. (Maybe the examples should not be all lower case...) – Neil – 2016-02-11T19:31:16.367

3

05AB1E, 39 45 bytes

Code:

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

Try it online!

Uses CP-1252 encoding. Truthy is when a number is outputted and falsy is when nothing is outputted.

Non-competing version (39 bytes), works with the newest version:

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

Adnan

Posted 2016-02-10T20:15:21.817

Reputation: 41 965

3

Python 2.7, 133 bytes

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

Not sure if we're supposed to post solutions if there's a smaller version in the comments but here's my version in Python.

Edit: Without the function it's only 73 bytes (and that's not even near the best answers. Forgive me I'm new

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

TheLateOne

Posted 2016-02-10T20:15:21.817

Reputation: 31

2Welcome to PPCG! If you spent time writing and golfing an answer and didn't plagiarise (without attribution), then you're welcome to post your answer! – Digital Trauma – 2016-02-10T21:26:56.040

Oh and most languages have a golfing-tips style question here. Python is no exception

– Digital Trauma – 2016-02-10T21:30:28.547

Note that this does work with input of 2 (integer) – wnnmaw – 2016-02-10T21:36:42.607

Also, by taking out unneeded spaces you can get this down to 98 bytes (you dont need to include print e('2', 'bee') in your count either) – wnnmaw – 2016-02-10T21:38:55.987

Thanks digital trauma that looks very helpful.

And thank you wnnmaw, my text editor hates me but golf was never supposed to be pretty – TheLateOne – 2016-02-10T21:39:09.833

3This returns True also for e('oo', '3') and similar substrings. – manatwork – 2016-02-11T08:46:21.237

1@TheLateOne, Python isn't very golfable, so you will almost never beat the golfing languages like pyth or cjam, but getting in the 50, 60, or 70 range is pretty good for us on most challenges. Don't get discouraged! – wnnmaw – 2016-02-11T14:47:28.677

The first line can be a,b="to","bee" ... – wb9688 – 2016-02-11T16:09:48.367

@wnmaw Ok I'll just try to minimise my output then - pyth looks fun though so I might branch out once I'm more confident with optimising Python solutions – TheLateOne – 2016-02-11T18:42:41.447

3

Perl 6, 45 44 bytes

Thanks to the folks in IRC for helping me golf this down

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

usage

> 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

Non-competing alternative, 54 bytes

This is a nice alternative to the above if you think regexes are gross, but it is a tad longer. It could be golfed down a couple more bytes but since it's non-competing I'll leave it as is.

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

Hotkeys

Posted 2016-02-10T20:15:21.817

Reputation: 1 015

1To anyone looking at this who doesn't know Perl 6. [ and ] denote a non-capturing group in a regex, which would be written as (?: and ) in Perl 5. To get a character class you have to place them in angle brackets <[ and ]>. – Brad Gilbert b2gills – 2016-02-12T17:06:53.547

b2gills++, I didn't even think to mention the differences in Perl 6 regexes – Hotkeys – 2016-02-12T18:30:14.743

I have had a downvote because someone didn't know that, so I always point it out. – Brad Gilbert b2gills – 2016-02-12T18:33:30.953

Is there a PP&CG rule stopping you from posting the more elegant golf as its own answer? I think a slightly longer* version would be worthy. *I hereby decree that elegant golf idioms include not caring about (not counting) whitespace. – raiph – 2016-02-13T19:27:33.537

2

Ruby, 53 55 52 bytes

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

I'll be honest, this is my first attempt at trying to golf a problem.

Function call in the form of f.call(firstValue, secondValue)

0 is Truthy, nil is Falsy.

Test it Here

Mr Public

Posted 2016-02-10T20:15:21.817

Reputation: 669

1

Pyth, 39 bytes

-3 bytes by @FryAmtheEggman

.A.b}rN1cY\@Q,."0Wѳ5YYÅJB"."3EW´l¢ï

Try it here.

lirtosiast

Posted 2016-02-10T20:15:21.817

Reputation: 20 331

I'll fix it when I get to a computer. – lirtosiast – 2016-02-10T21:30:52.783

1

PowerShell v3+, 74 70 bytes

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

Doesn't use regex. Takes two input, checks if the first one is -in the array that's been dynamically created by the -split operator, converts that Boolean to an int with +, then multiplies that * with checking whether the second is -in the second array (which will automatically cast the Boolean as an int). This works because x*y == x&y if x and y can only be 1 or 0.

PowerShell by default is case-insensitive, so we get that for free. Will output 0 or 1 for falsey/truthy, respectively. Requires v3 or newer for the -in operator.

Edit -- Saved 4 bytes by using unary -split

AdmBorkBork

Posted 2016-02-10T20:15:21.817

Reputation: 41 581

1

Japt, 36 bytes

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

Maybe I missed something, but this should work completely. Test it online!

ETHproductions

Posted 2016-02-10T20:15:21.817

Reputation: 47 880

1

Python, 85 83 bytes

@Manatwork saved me two bytes.

This is pretty brute force, I'll look into regex solutions next.

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

Ogaday

Posted 2016-02-10T20:15:21.817

Reputation: 471

1For the first array: '2 to too two t0'.split() For the second array makes no difference. – manatwork – 2016-02-11T11:54:01.770

Damn, yes, I tried '2,to,too,two,t0'.split(','). Silly me. – Ogaday – 2016-02-11T11:54:49.110

0

Groovy, 52 bytes

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

==~ is a cool regex operator in groovy that checks for equality.

Tests:

Regex101 test.

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

J Atkin

Posted 2016-02-10T20:15:21.817

Reputation: 4 846

What if x is t and y is 0b? – Neil – 2016-02-10T20:52:50.760

What do you mean? – J Atkin – 2016-02-10T20:53:41.287

I'd expect f('t', '0b') to be false, but I'm concerned that your code would return true. – Neil – 2016-02-10T20:58:19.460

Ohhhh, Ok, I got it. Yep it would return the wrong thing. – J Atkin – 2016-02-10T21:01:50.150

0

MATL, 32 41 43 bytes

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

Same approach as @DigitalTrauma's Retina answer. Inputs are separated by a comma. Truthy output is a string with the two inputs lowercased; falsy is no output.

Try it online!

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

Luis Mendo

Posted 2016-02-10T20:15:21.817

Reputation: 87 464

0

C# 6, 132 bytes

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

Ungolfed version (Only slightly more readable):

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

Farhan Anam

Posted 2016-02-10T20:15:21.817

Reputation: 189

0

Python 2, 67 bytes

Uses Digital Trauma's regex. Input is a single string separated by a comma. Not sure if that format is allowed for input...

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

mbomb007

Posted 2016-02-10T20:15:21.817

Reputation: 21 944