# As Easy as One-Two-Three

Write a program or function that takes in a positive integer. You can assume the input is valid and may take it as a string. If the number is any of

123
234
345
456
567
678
789


then output a truthy value. Otherwise, output a falsy value. For example, the inputs

1
2
3
12
122
124
132
321
457
777
890
900
1011
1230
1234


must all result in falsy output. (The input will not have leading zeroes so you needn't worry about things like 012.)

The shortest code in bytes wins.

• Oh, strings are allowed? What about digit arrays? Aug 30, 2016 at 3:30
• @Dennis No. Let's keep it to plain strings or plain ints. Aug 30, 2016 at 3:30
• If I take string input, should I handle 012?
– Lynn
Aug 30, 2016 at 4:30
• @Lynn No. 012 would be falsy but you can assume it is not input. Aug 30, 2016 at 4:45
• @BradGilbertb2gills No. It should just satisfy the linked definition of truthy/falsy - meta.codegolf.stackexchange.com/questions/2190/… Aug 30, 2016 at 4:46

## Python, 24 bytes

range(123,790,111).count


An anonymous function that outputs 0 or 1. It creates the list [123, 234, 345, 456, 567, 678, 789] and counts how many times the input appears.

f=range(123,790,111).count

f(123)
=> 1
f(258)
=> 0

• Couldn't you remove a byte by making the start at 12 instead of 123? Aug 31, 2016 at 14:14
• It needs to not include 12.
– xnor
Aug 31, 2016 at 16:02
• But we can assume it wouldn't be input? I'm confused Aug 31, 2016 at 19:00
• If you're talking about the comments, they're saying that if you take input as a string (which this is not), you can expect numbers to not have leading zeroes, so 12 will be given as "12" and not "012".
– xnor
Aug 31, 2016 at 23:41

## Python, 24 bytes

lambda n:n%111==12<n<900


Just a lot of condition chaining.

• Being able to compare a range that easily beats any language I already know. I had to look it up to see how it worked. Sep 1, 2016 at 3:55
• Wow, if it wasn't for the word lambda I wouldn't have even guessed that was Python. That's horrific. May 9, 2017 at 10:54

(elem[123,234..789])


An anonymous function. Generates the evenly-spaced list [123, 234, 345, 456, 567, 678, 789] and checks if the input is an element.

• No way! That's magic!
– YSC
Aug 30, 2016 at 13:43

# Python 2, 25 bytes

lambda n:n-12==n[0]*3


Test it on Ideone.

# Brachylog, 9 bytes

h:2j:12+?


In English, "(prove that) the Input's first digit, concatenated to itself twice, add 12, is still the Input."

• This is brilliant! Sep 2, 2016 at 16:54

# Brain-Flak 76 + 3 = 79 bytes

This answer is a golf of this answer. I don't actually know quite how my answer works but DJMcMayhem gives a good explanation in his original answer and my answer is a modification of his.

([]<>)<>({}[({})]<>)<>(({}[({})()()()()()]<>{}{}<(())>)){{}{}(((<{}>)))}{}{}


It is run with the -a ascii flag adding 3 bytes.

## Explanation (of sorts)

Starting with the original working solution:

([]<>)<>({}[({})]<>)<>({}[({})]<>)({}{}[()()])({}<({}[()()()])>)(({}{}<(())>)){{}{}(((<{}>)))}{}{}


I run this through a simple golfing algorithm I wrote and get:

([]<>)<>({}[({})]<>)<>(({}[({})]<>{}[()()]<({}[()()()])>{}<(())>)){{}{}(((<{}>)))}{}{}


From here I see the section <({}[()()()])>{} this essentially multiplies by one which makes it equal to {}[()()()] reduce the whole code to:

([]<>)<>({}[({})]<>)<>(({}[({})]<>{}[()()]{}[()()()]<(())>)){{}{}(((<{}>)))}{}{}


Lastly negatives can be combined:

([]<>)<>({}[({})]<>)<>(({}[({})()()()()()]<>{}{}<(())>)){{}{}(((<{}>)))}{}{}

• "I don't actually know quite how my answer works" you win the internet Aug 30, 2016 at 5:09
• Nope. Aug 30, 2016 at 9:20
• @LeakyNun I don't believe Ascii mode works on try it online. You are going to have to get the github version. Aug 30, 2016 at 13:33
• @WheatWizard ASCII mode definitely works on TIO. You can verify this by adding 48 ('0') to the top of the stack. Leaky nun is right, the algorithm (my algorithm) is wrong, because it just checks if the sum of the differences is 2 (which works if the difference is +3 and -1). Unfortunately, both of our answers are wrong. Aug 30, 2016 at 17:24
• @WheatWizard This answer does not appear to be valid. Try it online! (My original answer wasn't either) May 8, 2017 at 16:55

# 05AB1E, 5 bytes

¥XX‚Q


Explanation

¥      # deltas
Q  # are equal to
XX‚   # [1,1]


Try it online

• I used 2Å1 instead of XX,, just for the heck of less commands (4 instead of 5). Dec 3, 2016 at 11:59
• @ErikGolferエリックゴルファー: and Å is writeable on my keyboard (as opposed to ‚) which is a benefit :) Dec 3, 2016 at 13:17
• ‚ (not the , I used) doesn't have a compose-key sequence too, while Å is oA on an en-US keyboard. Dec 3, 2016 at 14:29

# Brainfuck, 32 bytes

+>,+>,>,-[-<-<->>],[<]<[<]<[<]<.


Try it online!

Credits to Lynn for the core of the algorithm.

# Jelly, 6 bytes

DI⁼1,1


### How it works

DI⁼1,1  Main link. Argument: n (integer)

D       Decimal; convert n to base 10.
I      Increments; compute the differences of all pairs of adjacent digits.
1,1  Yield [1, 1].
⁼     Test the results to both sides for equality.

• 012 doesn't return false, although it doesn't actually return anything... Aug 30, 2016 at 16:23
• The input has to be an integer. As far as ast.literal_eval is concerned, 012 doesn't represent an integer. Aug 30, 2016 at 16:33

# MATL, 8 bytes

d1=tn2=*


Try it online!

This will print 1 1 for a truthy input, and an array with a 0 in it for a falsy value, since that is falsy in MATL.

Explanation:

d           % Calculate the difference between consecutive digits
1=         % Push an array of which elements equal one
t        % Duplicate this array
n       % Push the length of this array
2=     % Push a one if the length is 2, and a zero otherwise
% Now, if we have a truthy input, the stack looks like:
%   [1 1]
%   1
% And if we have a falsy input, the stack looks something like this:
%   [1 0]
%   1
% Or this:
%   [1 1]
%   0
*    % Multiply the top two elements

• Maybe d1=Ep4= (I haven't tested thoroughly) Aug 30, 2016 at 9:53
• Or dTTX= for 5 bytes Aug 30, 2016 at 10:05
• @luismendo whaaa? How does that even work? I can't find documentation on T Aug 30, 2016 at 14:53
• T is the literal true, and F is false. Neighbouring T and F stick together, so TT is [true true], which for these purposes is equivalent to [1 1]. See section 4.3 of the spec Aug 30, 2016 at 14:59

## Java 7, 46 bytes

boolean f(int a){return a>12&a<790&a%111==12;}


After trying several things with Leaky Nun in chat, this seems to be the shortest. Sometimes you just have to do things the straightforward way :/

Explanation:

boolean f(int a){
return a>12         Is it more than 12? (stupid edge case)
&
a<790        Is it in range the other way?
&
a%111==12;   Is it 12 more than a multiple of 111?
}


# Perl 6,  35 29 24 21  19 bytes

{.chars==3&&'0123456789'.index: $_} {$_ (elem) (123,*+111...789)}
^(:+ ):\1::\1$ Outputs 1 for truthy and 0 for falsey. Try it online (First line added to allow multiple testcases to be run). # R, 30 22 bytes scan()%in%(12+1:7*111)  Not particularly exciting; check if input is in the sequence given by 12 + 111k, where k is each of 1 to 7. Note that : precedes * so the multiplication happens after the sequence is generated. # Brain-Flak, 99 bytes ([{}]({})<>)<>([{}]{}<>)(({})<([{}]{})((){[()](<{}>)}{})>)([{}]{})((){[()](<{}>)}{})<>{{{}}<>{}}<>  Try it online! This is 98 bytes of code +1 for the -a flag. This prints 1 for truthy, and either 0 or nothing (which is equivalent to 0) for falsy • Try to get rid of push pop inefficiencies. I can see a bunch in your code. They look like ...)({} but vary. If you push and pop without using the value you can condense it into one thing. I can link you to a version of your code with all of these golfed out if you want. Aug 30, 2016 at 4:20 • Here is my 76 byte golf of your program. I pretty much ran my brain-flak optimizer over your code with a few custom settings. Aug 30, 2016 at 4:43 • Nope. Aug 30, 2016 at 7:42 # C# (Visual C# Interactive Compiler), 41 30 23 bytes First code-golf submission, be gentle :) a=>{return a>12&&a<790?a%111==12:false;}; a=>a>12&&a<790?a%111==12:false a=>a>12&a<790&a%111==12  Try it online! • -11 bytes thanks to Kirill L. • Another -7 bytes thanks to ASCII-only. • Welcome to PPCG! You can save some bytes by dropping the curly braces and return keyword: 30 bytes Mar 14, 2019 at 11:57 • Mar 14, 2019 at 12:50 • Nice first submission! Mar 14, 2019 at 15:36 # Ruby -nl, 3230 25 bytes p"123456789"[$_]&.size==3


Attempt This Online!

• Your tests show 12 going to true.
– xnor
Aug 30, 2016 at 4:51
• @xnor Oops. That'll teach me to golf after bedtime. Fixed! Aug 30, 2016 at 5:11
• I thought -a does a split, not chop? Also, what does the & do? I'm using an older Ruby which throws an error. Anyway, it works perfectly at 26 bytes without it.
– xsot
Aug 30, 2016 at 8:26
• Oops, I meant -l, not -a. &. is the "safe navigation" operator, added in Ruby 2.3. Without it inputs like 19, which aren't substrings if "123456789", will raise a NoMethodError. Aug 30, 2016 at 12:29
• @Jordan I'm not getting an error in 2.2. Maybe it's new in 2.3 too?
– xsot
Aug 30, 2016 at 12:50

# Brain-Flak, 114 bytes

([((()()()){}){}]{})(((()()()){}())<>)<>{({}<(({}[(((((()()()){}()){}){}){}){}]())){(<{}>)<>({}[()])<>}{}>[()])}<>


Try it online!

Correct version (in the spirit of the question): takes the integer as input, output 0 for falsey and 1 for truthy.

This is not stack clean.

### Algorithm

Let the input be n.

The output is truthy iff (n-123)(n-234)(n-345)(n-456)(n-567)(n-678)(n-789)=0.

I computed those seven numbers by first subtracting 12 and then subtract 111 7 times, and then computed the logical double-NOT of those seven numbers and added them up.

For truthy results, the sum is 6; for falsey results, the sum is 7.

Then, I subtract the sum from 7 and output the answer.

• I don't understand the code, but the algorithm is clever so have a +1. Sep 4, 2016 at 3:40

# JavaScript ES6, 26 bytes

n=>1>(n-12)%111&n>99&n<790

This takes advantage of the fact that I'm using bit-wise logic operators on what are essentially booleans (which are bit-based!)

Thanks to Titus for saving 2.

• two bytes: (n-12) and n>99 Aug 30, 2016 at 7:31
• @Titus Oh, very nice, +1 to you! Aug 30, 2016 at 7:44
• => is ES6, not ES5.
– Neil
Aug 30, 2016 at 7:50
• I believe it was decided in meta that you didn't have to count "f=" making this 26 bytes Aug 30, 2016 at 20:53
• @WallyWest I think it's because it's not necessary to have "f=" to use the function in every case, so why assume you need it for this case? People smarted than me decided it's fine so I just go with it ;) Aug 30, 2016 at 20:58

# Vyxal, 4 bytes

¯k₁⁼


Try it Online!

### Explained

¯k₁⁼
¯    # Deltas
k₁  # [1,1]
⁼ # compare lists


# K (ngn/k), 15 bytes

{($x-12)=3#*$x}


Try it online!

Port of Dennis' algorithm.

Explanation:

{($x-12)=3#*$x}  Main function. Takes x as input
$x Convert x to string * Then get the first character 3# And duplicate it thrice = Is it equal to ( x-12) x - 12$              Converted to string?


# Brainfuck, 43 bytes

,>,>,>,[>>]<[[-<-<->>]<+[>>]<++[>>->]<+<]>.


Bah, I'm no good at this. Outputs \x01 if the output is one of the strings 123, 234, …, 789; outputs \x00 otherwise.

(I beat Java 7, though…)

Try it online!

• What's the point of [>>]<? Couldn't that just be >? Aug 30, 2016 at 5:12
• I want to veer the program into failure (by throwing it off-track) if the cell under the pointer isn't zero at that point.
– Lynn
Aug 30, 2016 at 5:21
• 42 bytes Aug 30, 2016 at 5:51
• @LeakyNun That looks completely different; feel free to post it as a separate answer
– Lynn
Aug 30, 2016 at 6:02
• @Lynn Done. Aug 30, 2016 at 6:13

# Excel - 625735 31 bytes

Based on Anastasiya-Romanova's answer, but returning Excel's TRUE/FALSE values.

=AND(LEN(N)=3,MID(N,2,1)-MID(N,1,1)=1,MID(N,3,1)-MID(N,2,1)=1)


Further, we can get to

=AND(LEN(N)=3,MID(N,2,1)-LEFT(N)=1,RIGHT(N)-MID(N,2,1)=1)


since both RIGHT and LEFT return a single character by default.

And, inspired by some of the Python solutions:

=AND(LEN(N)=3,MOD(N,111)=12,N<>900)


Thanks to Neil for 4 more bytes...

=AND(N>99,MOD(N,111)=12,N<900)

• Doesn't N<900 save you a byte, in which case you can also use N>99 instead of LEN(N)=3.
– Neil
Aug 30, 2016 at 7:49
• 21 bytes: =REPT(LEFT(N),3)+12=N where N is the name of the reference cell. May 8, 2017 at 19:02

# Brachylog (2), 7 bytes

ẹ~⟦₂-_2


Try it online!

## Explanation

ẹ~⟦₂-_2
ẹ        Split into digits
~⟦₂     Assert that this is an increasing range; take its endpoints
-_2  Assert that the starting minus ending endpoint is -2


As a full program, we get a truthy return if all assertions hold, a falsey return if any fail.

# CJam, 13 9 bytes

A,s3ewqe=


Try it online!

Explanation

A,s        e# Push "0123456789".
3ew     e# Split it into contiguous length-3 chunks: ["012" "123" "234" ... "789"].
q    e# Push the input.
e=  e# Count the number of times the input appears in the array.


# MathGolf, 6 4 bytes

│ª▒=


Try it online!

## Explanation

│       get differences of digits in integer
ª      push [1]
▒     duplicate items in list
=    pop(a, b), push(a==b)

• It would be nice if 2. could be ∞
– Jo King
Mar 15, 2019 at 10:12
• @JoKing yes, that would be the dream. Right now, ∞ multiplies each element of the list by 2 rather than multiplying the list by 2. I haven't checked if I use ∞ with lists to any extent, but I can't have both implementations at once. I also want to make │ work with integers, to get digit differences which could be useful for some challenges.
– maxb
Mar 15, 2019 at 12:08
• @JoKing I realized that I could fit both those things into existing operators, which made sense to have. Check the readme for more changes.
– maxb
Mar 18, 2019 at 14:15

# Regex (ECMAScript), 20 bytes

The input n is in unary, as the length of a string of xs.

^x{12}(x{111}){1,7}\$


Try it online!

This works by asserting that $$\n-12\$$ is of the form $$\111k\$$ where $$\1\le k\le 7\$$.

# BRASCA, 15 bytes

Ci,:::SSL{+,SS=


Try it online!

## Explanation

Ci                - Set implicit output to output numbers, and turn "0-9" into 0-9
,               - Reverse stack
:::SSL{+       - First digit of input, concatenated to itself twice, + 12
,SS=   - Concatenate the digits of the original, then check if both numbers are equal
<implicit output> - Output 1 if truthy, 0 if falsy


# Thunno 2, 4 bytes

Ṣk&⁼


Attempt This Online!

#### Explanation

Ṣk&⁼  # Implicit input
Ṣ     # deltas
⁼  #        ==
k&   #           [1, 1]
# Implicit output


# Excel - 104 bytes

=IF(LEN(N)<3,"Falsy",IF(AND(LEN(N)=3,MID(N,2,1)-MID(N,1,1)=1,MID(N,3,1)-MID(N,2,1)=1),"Truthy","Falsy"))


Explanation:

The syntax for the IF formula in Excel is:

IF( condition, [value_if_true], [value_if_false] )


If the length of input N, where it's a name of the reference cell, is less than 3, then it will return Falsy. Else, if the length of input N is 3 and both of the difference of second digit and first digit and the difference of third digit and second digit are equal to 1, then it will return Truthy.

• 21 bytes: =REPT(LEFT(N),3)+12=N where N is the name of the reference cell. May 8, 2017 at 18:24