40
\$\begingroup\$

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.

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

78 Answers 78

49
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ Couldn't you remove a byte by making the start at 12 instead of 123? \$\endgroup\$ Aug 31, 2016 at 14:14
  • 1
    \$\begingroup\$ It needs to not include 12. \$\endgroup\$
    – xnor
    Aug 31, 2016 at 16:02
  • \$\begingroup\$ But we can assume it wouldn't be input? I'm confused \$\endgroup\$ Aug 31, 2016 at 19:00
  • 1
    \$\begingroup\$ 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". \$\endgroup\$
    – xnor
    Aug 31, 2016 at 23:41
35
\$\begingroup\$

Python, 24 bytes

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

Just a lot of condition chaining.

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Sep 1, 2016 at 3:55
  • \$\begingroup\$ Wow, if it wasn't for the word lambda I wouldn't have even guessed that was Python. That's horrific. \$\endgroup\$ May 9, 2017 at 10:54
25
\$\begingroup\$

Haskell, 22 bytes

(`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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ No way! That's magic! \$\endgroup\$
    – YSC
    Aug 30, 2016 at 13:43
13
\$\begingroup\$

Python 2, 25 bytes

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

Test it on Ideone.

\$\endgroup\$
13
\$\begingroup\$

Brachylog, 9 bytes

h:2j:12+?

Try it online! or Verify all test-cases.

Credits to Dennis for the algorithm.

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This is brilliant! \$\endgroup\$
    – datagod
    Sep 2, 2016 at 16:54
8
\$\begingroup\$

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:

([]<>)<>({}[({})]<>)<>(({}[({})()()()()()]<>{}{}<(())>)){{}{}(((<{}>)))}{}{}
\$\endgroup\$
6
  • 14
    \$\begingroup\$ "I don't actually know quite how my answer works" you win the internet \$\endgroup\$
    – Leaky Nun
    Aug 30, 2016 at 5:09
  • \$\begingroup\$ Nope. \$\endgroup\$
    – Leaky Nun
    Aug 30, 2016 at 9:20
  • \$\begingroup\$ @LeakyNun I don't believe Ascii mode works on try it online. You are going to have to get the github version. \$\endgroup\$
    – Wheat Wizard
    Aug 30, 2016 at 13:33
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – DJMcMayhem
    Aug 30, 2016 at 17:24
  • 1
    \$\begingroup\$ @WheatWizard This answer does not appear to be valid. Try it online! (My original answer wasn't either) \$\endgroup\$
    – DJMcMayhem
    May 8, 2017 at 16:55
8
\$\begingroup\$

05AB1E, 5 bytes

¥XX‚Q

Explanation

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

Try it online

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

Brainfuck, 32 bytes

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

Try it online!

Credits to Lynn for the core of the algorithm.

\$\endgroup\$
0
7
\$\begingroup\$

Jelly, 6 bytes

DI⁼1,1

Try it online! or verify all test cases.

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.
\$\endgroup\$
2
  • \$\begingroup\$ 012 doesn't return false, although it doesn't actually return anything... \$\endgroup\$ Aug 30, 2016 at 16:23
  • \$\begingroup\$ The input has to be an integer. As far as ast.literal_eval is concerned, 012 doesn't represent an integer. \$\endgroup\$
    – Dennis
    Aug 30, 2016 at 16:33
6
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ Maybe d1=Ep4= (I haven't tested thoroughly) \$\endgroup\$
    – Luis Mendo
    Aug 30, 2016 at 9:53
  • 1
    \$\begingroup\$ Or dTTX= for 5 bytes \$\endgroup\$
    – Luis Mendo
    Aug 30, 2016 at 10:05
  • \$\begingroup\$ @luismendo whaaa? How does that even work? I can't find documentation on T \$\endgroup\$
    – DJMcMayhem
    Aug 30, 2016 at 14:53
  • \$\begingroup\$ 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 \$\endgroup\$
    – Luis Mendo
    Aug 30, 2016 at 14:59
6
\$\begingroup\$

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? 
}
\$\endgroup\$
0
6
\$\begingroup\$

Perl 6,  35 29 24 21  19 bytes

{.chars==3&&'0123456789'.index: $_}
{$_ (elem) (123,*+111...789)}
{$_∈(123,*+111...789)}
*∈(123,*+111...789)
*∈(123,234...789)

Explanation:

# Whatever lambda ( the parameter is 「*」 )
*

∈ # is it an element of:

# this sequence
(
  123,
  234,

  # deduce rest of sequence
  ...

  # stop when you generate this value
  789
)

Usage:

my &code = *∈(123,234...789);

say code 123; # True
say code 472; # False

say (  *∈(123,234...789)  )( 789 ); # True
\$\endgroup\$
6
\$\begingroup\$

Retina, 26

.
$*: 
^(:+ ):\1::\1$

Outputs 1 for truthy and 0 for falsey.

Try it online (First line added to allow multiple testcases to be run).

\$\endgroup\$
5
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Aug 30, 2016 at 4:20
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Aug 30, 2016 at 4:43
  • \$\begingroup\$ Nope. \$\endgroup\$
    – Leaky Nun
    Aug 30, 2016 at 7:42
5
\$\begingroup\$

Ruby -nl, 32 30 25 bytes

p"123456789"[$_]&.size==3

Attempt This Online!

\$\endgroup\$
5
  • \$\begingroup\$ Your tests show 12 going to true. \$\endgroup\$
    – xnor
    Aug 30, 2016 at 4:51
  • \$\begingroup\$ @xnor Oops. That'll teach me to golf after bedtime. Fixed! \$\endgroup\$
    – Jordan
    Aug 30, 2016 at 5:11
  • \$\begingroup\$ 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. \$\endgroup\$
    – xsot
    Aug 30, 2016 at 8:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jordan
    Aug 30, 2016 at 12:29
  • \$\begingroup\$ @Jordan I'm not getting an error in 2.2. Maybe it's new in 2.3 too? \$\endgroup\$
    – xsot
    Aug 30, 2016 at 12:50
4
\$\begingroup\$

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.

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

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.

\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG! You can save some bytes by dropping the curly braces and return keyword: 30 bytes \$\endgroup\$
    – Kirill L.
    Mar 14, 2019 at 11:57
  • 1
    \$\begingroup\$ 23, also 23 \$\endgroup\$
    – ASCII-only
    Mar 14, 2019 at 12:50
  • 1
    \$\begingroup\$ Nice first submission! \$\endgroup\$
    – Gymhgy
    Mar 14, 2019 at 15:36
3
\$\begingroup\$

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!

\$\endgroup\$
6
  • \$\begingroup\$ What's the point of [>>]<? Couldn't that just be >? \$\endgroup\$
    – DJMcMayhem
    Aug 30, 2016 at 5:12
  • \$\begingroup\$ 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. \$\endgroup\$
    – Lynn
    Aug 30, 2016 at 5:21
  • \$\begingroup\$ 42 bytes \$\endgroup\$
    – Leaky Nun
    Aug 30, 2016 at 5:51
  • \$\begingroup\$ @LeakyNun That looks completely different; feel free to post it as a separate answer \$\endgroup\$
    – Lynn
    Aug 30, 2016 at 6:02
  • \$\begingroup\$ @Lynn Done. \$\endgroup\$
    – Leaky Nun
    Aug 30, 2016 at 6:13
3
\$\begingroup\$

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.

\$\endgroup\$
8
  • 1
    \$\begingroup\$ two bytes: (n-12) and n>99 \$\endgroup\$
    – Titus
    Aug 30, 2016 at 7:31
  • \$\begingroup\$ @Titus Oh, very nice, +1 to you! \$\endgroup\$ Aug 30, 2016 at 7:44
  • 1
    \$\begingroup\$ => is ES6, not ES5. \$\endgroup\$
    – Neil
    Aug 30, 2016 at 7:50
  • 1
    \$\begingroup\$ I believe it was decided in meta that you didn't have to count "f=" making this 26 bytes \$\endgroup\$ Aug 30, 2016 at 20:53
  • 1
    \$\begingroup\$ @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 ;) \$\endgroup\$ Aug 30, 2016 at 20:58
3
\$\begingroup\$

Excel - 62 57 35 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)
\$\endgroup\$
2
  • \$\begingroup\$ Doesn't N<900 save you a byte, in which case you can also use N>99 instead of LEN(N)=3. \$\endgroup\$
    – Neil
    Aug 30, 2016 at 7:49
  • 1
    \$\begingroup\$ 21 bytes: =REPT(LEFT(N),3)+12=N where N is the name of the reference cell. \$\endgroup\$ May 8, 2017 at 19:02
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.
\$\endgroup\$
0
3
\$\begingroup\$

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\$.

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 4 bytes

¯k₁⁼

Try it Online!

Explained

¯k₁⁼
¯    # Deltas
 k₁  # [1,1]
   ⁼ # compare lists
\$\endgroup\$
3
\$\begingroup\$

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?
\$\endgroup\$
2
\$\begingroup\$

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.

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

Pyth, 8 Bytes

qi>3SeQT

Try online!

Explanation:

q       Q  Is the input equal to:
  <3S       The last three digits of the range from 1 to
     eQ      The last digit of the input    
 i     T      Concatenated together into an integer
\$\endgroup\$
2
\$\begingroup\$

Dyalog APL, 10 bytes

Takes string argument.

1 1≡¯2-/⍎¨

1 1≡ Is {1, 1} identical to

¯2-/ the reversed pair-wise difference of

⍎¨ each character taken as a number?

TryAPL online! ( has been emulated with e for security reasons.)

\$\endgroup\$
2
\$\begingroup\$

Perl, 18 bytes

Includes +1 for -p

Run with the input on STDIN

123.pl <<< 123

123.pl:

#!/usr/bin/perl -p
$_=$_=/./.2==$_-$&x3
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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