Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
5  
N0 l33t? “t0 b3” – manatwork Feb 10 at 20:18
7  
Why does it have to be a function? – Dennis Feb 10 at 20:19
2  
@rybo111 Well! That seems like they didn't understand the concept then ;) – Draco18s Feb 10 at 20:19
12  
This one begs for a Shakespeare solution. Non-competing of course, as the byte count would undoubtedly be huge... – Darrel Hoffman Feb 10 at 22:15
1  
@LuisMendo OK. Either is fine. Supporting both is not necessary. – rybo111 Feb 10 at 22:26

18 Answers 18

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.

share|improve this answer
2  
When I posted this question, I never envisaged I would get such a complex and yet very fitting answer! – rybo111 Feb 12 at 12:16

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.

share|improve this answer
    
I always love it when fixing bugs saves bytes! – Neil Feb 10 at 21:15
5  
@Neil yep - deleted code is debugged code :) – Digital Trauma Feb 10 at 21:21
1  
@DigitalTrauma Truly, only sufficiently penitent code can be allowed in our brave new world. – FryAmTheEggman Feb 10 at 21:24

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
share|improve this answer
    
Somehow if I remove the code before the packed string, the string becomes broken. – busukxuan Feb 10 at 21:53
    
@FryAmTheEggman I did. The output was "!$7C<Q/=/LF$VF4K:.-E" – busukxuan Feb 10 at 21:55
    
@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 Feb 10 at 22:01
    
@busukxuan Yeah I just noticed the same thing :P SE hides most unprintable characters unfortunately. – FryAmTheEggman Feb 10 at 22:02
1  
This doesn't seem to pass the condition (t zero) for the first value – Nic Robertson Feb 11 at 3:22

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.

share|improve this answer

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.

share|improve this answer
    
I have no Oracle, so only tested on sqlzoo.net, but looks like where is not necessary: SELECT:1 IN('2','to','too','two','t0')AND:2 IN('b','be','bee','b3')FROM DUAL; – manatwork Feb 10 at 20:54
    
It does not work with Oracle. The boolean datatype is strictly PL/SQL, so no logical evaluation in the SELECT. – Jeto Feb 10 at 21:07
    
Got it. ☹ Thank you. – manatwork Feb 10 at 21:09

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>*
share|improve this answer

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.

share|improve this answer
    
@user81655 Not bad, but I've further improved it. – Neil Feb 10 at 21:24
1  
if 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 Feb 10 at 22:33
    
@PatrickRoberts Huh, I had thought about that, but I must have miscalculated, because I didn't think I was saving anything. – Neil Feb 10 at 23:53
    
The i flag is missing. – Florent Feb 11 at 18:58
    
@Florent Thanks for spotting that. (Maybe the examples should not be all lower case...) – Neil Feb 11 at 19:31

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)}
share|improve this answer
1  
To 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 Feb 12 at 17:06
    
b2gills++, I didn't even think to mention the differences in Perl 6 regexes – LockOpeners Feb 12 at 18:30
    
I have had a downvote because someone didn't know that, so I always point it out. – Brad Gilbert b2gills Feb 12 at 18:33
    
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 Feb 13 at 19:27

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"
share|improve this answer
2  
Welcome 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 Feb 10 at 21:26
    
Thanks good to know! – TheLateOne Feb 10 at 21:29
    
Oh and most languages have a golfing-tips style question here. Python is no exception – Digital Trauma Feb 10 at 21:30
3  
This returns True also for e('oo', '3') and similar substrings. – manatwork Feb 11 at 8:46
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 Feb 11 at 14:47

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

share|improve this answer

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!

share|improve this answer

Pyth, 39 bytes

-3 bytes by @FryAmtheEggman

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

Try it here.

share|improve this answer
    
I'll fix it when I get to a computer. – lirtosiast Feb 10 at 21:30

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']
share|improve this answer
1  
For the first array: '2 to too two t0'.split() For the second array makes no difference. – manatwork Feb 11 at 11:54
    
Damn, yes, I tried '2,to,too,two,t0'.split(','). Silly me. – Ogaday Feb 11 at 11:54

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

share|improve this answer

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
share|improve this answer
    
What if x is t and y is 0b? – Neil Feb 10 at 20:52
    
What do you mean? – J Atkin Feb 10 at 20:53
    
I'd expect f('t', '0b') to be false, but I'm concerned that your code would return true. – Neil Feb 10 at 20:58
    
Ohhhh, Ok, I got it. Yep it would return the wrong thing. – J Atkin Feb 10 at 21:01

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
share|improve this answer

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());
share|improve this answer

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))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.