As Easy as One-Two-Three

35

2

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.

Calvin's Hobbies

Posted 2016-08-30T03:28:53.697

Reputation: 84 000

Oh, strings are allowed? What about digit arrays? – Dennis – 2016-08-30T03:30:27.763

@Dennis No. Let's keep it to plain strings or plain ints. – Calvin's Hobbies – 2016-08-30T03:30:54.753

6If I take string input, should I handle 012? – Lynn – 2016-08-30T04:30:08.440

Does it have to return the same truth value? same false value? – Brad Gilbert b2gills – 2016-08-30T04:43:31.990

1@Lynn No. 012 would be falsy but you can assume it is not input. – Calvin's Hobbies – 2016-08-30T04:45:30.433

1

@BradGilbertb2gills No. It should just satisfy the linked definition of truthy/falsy - http://meta.codegolf.stackexchange.com/questions/2190/interpretation-of-truthy-falsey/2194#2194

– Calvin's Hobbies – 2016-08-30T04:46:22.197

Answers

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

xnor

Posted 2016-08-30T03:28:53.697

Reputation: 115 687

Couldn't you remove a byte by making the start at 12 instead of 123? – var firstName – 2016-08-31T14:14:25.920

1It needs to not include 12. – xnor – 2016-08-31T16:02:57.093

But we can assume it wouldn't be input? I'm confused – var firstName – 2016-08-31T19:00:34.657

1If 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 – 2016-08-31T23:41:42.863

34

Python, 24 bytes

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

Just a lot of condition chaining.

Sp3000

Posted 2016-08-30T03:28:53.697

Reputation: 58 729

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.

– GuitarPicker – 2016-09-01T03:55:03.117

Wow, if it wasn't for the word lambda I wouldn't have even guessed that was Python. That's horrific. – Steve Bennett – 2017-05-09T10:54:10.577

25

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.

xnor

Posted 2016-08-30T03:28:53.697

Reputation: 115 687

1No way! That's magic! – YSC – 2016-08-30T13:43:38.587

12

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

Leaky Nun

Posted 2016-08-30T03:28:53.697

Reputation: 45 011

1This is brilliant! – datagod – 2016-09-02T16:54:06.260

11

Python 2, 25 bytes

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

Test it on Ideone.

Dennis

Posted 2016-08-30T03:28:53.697

Reputation: 196 637

8

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:

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

Post Rock Garf Hunter

Posted 2016-08-30T03:28:53.697

Reputation: 55 382

14"I don't actually know quite how my answer works" you win the internet – Leaky Nun – 2016-08-30T05:09:27.307

Nope. – Leaky Nun – 2016-08-30T09:20:05.723

@LeakyNun I don't believe Ascii mode works on try it online. You are going to have to get the github version. – Post Rock Garf Hunter – 2016-08-30T13:33:30.670

It works, I'm saying that the algorithm is wrong. – Leaky Nun – 2016-08-30T13:34:26.353

1

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

– James – 2016-08-30T17:24:21.053

1

@WheatWizard This answer does not appear to be valid. Try it online! (My original answer wasn't either)

– James – 2017-05-08T16:55:37.000

8

Brainfuck, 32 bytes

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

Try it online!

Credits to Lynn for the core of the algorithm.

Leaky Nun

Posted 2016-08-30T03:28:53.697

Reputation: 45 011

7

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.

Dennis

Posted 2016-08-30T03:28:53.697

Reputation: 196 637

012 doesn't return false, although it doesn't actually return anything... – Jamie Barker – 2016-08-30T16:23:32.593

The input has to be an integer. As far as ast.literal_eval is concerned, 012 doesn't represent an integer.

– Dennis – 2016-08-30T16:33:21.000

7

05AB1E, 5 bytes

¥XX‚Q

Explanation

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

Try it online

Emigna

Posted 2016-08-30T03:28:53.697

Reputation: 50 798

I used 2Å1 instead of XX,, just for the heck of less commands (4 instead of 5). – Erik the Outgolfer – 2016-12-03T11:59:11.387

@ErikGolferエリックゴルファー: and Å is writeable on my keyboard (as opposed to ) which is a benefit :) – Emigna – 2016-12-03T13:17:24.580

(not the , I used) doesn't have a compose-key sequence too, while Å is oA on an en-US keyboard. – Erik the Outgolfer – 2016-12-03T14:29:34.683

6

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

James

Posted 2016-08-30T03:28:53.697

Reputation: 54 537

Maybe d1=Ep4= (I haven't tested thoroughly) – Luis Mendo – 2016-08-30T09:53:26.087

1Or dTTX= for 5 bytes – Luis Mendo – 2016-08-30T10:05:33.740

@luismendo whaaa? How does that even work? I can't find documentation on T – James – 2016-08-30T14:53:55.120

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 – Luis Mendo – 2016-08-30T14:59:26.017

6

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? 
}

Geobits

Posted 2016-08-30T03:28:53.697

Reputation: 19 061

6

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

Brad Gilbert b2gills

Posted 2016-08-30T03:28:53.697

Reputation: 12 713

6

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

Digital Trauma

Posted 2016-08-30T03:28:53.697

Reputation: 64 644

5

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

James

Posted 2016-08-30T03:28:53.697

Reputation: 54 537

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. – Post Rock Garf Hunter – 2016-08-30T04:20:20.310

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. – Post Rock Garf Hunter – 2016-08-30T04:43:31.803

Nope. – Leaky Nun – 2016-08-30T07:42:18.073

5

Ruby, 32 30 25 + 2 = 27 bytes

+2 bytes for -nl flags.

Takes input on STDIN and prints true or false.

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

See it on repl.it: https://repl.it/DBn2/2 (Click on ▶️ and then type input into the console below.)

Jordan

Posted 2016-08-30T03:28:53.697

Reputation: 5 001

Your tests show 12 going to true. – xnor – 2016-08-30T04:51:27.637

@xnor Oops. That'll teach me to golf after bedtime. Fixed! – Jordan – 2016-08-30T05:11:11.243

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 – 2016-08-30T08:26:27.420

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. – Jordan – 2016-08-30T12:29:33.537

@Jordan I'm not getting an error in 2.2. Maybe it's new in 2.3 too? – xsot – 2016-08-30T12:50:45.817

4

C# (Visual C# Interactive Compiler), 41 30 23 bytes

First code-golf submission, be gentle :)

a=>{return a&gt12&&a&lt790?a%111==12:false;};
a=>a&gt12&&a&lt790?a%111==12:false
a=>a&gt12&a&lt790&a%111==12

Try it online!

  • -11 bytes thanks to Kirill L.
  • Another -7 bytes thanks to ASCII-only.

Loxx

Posted 2016-08-30T03:28:53.697

Reputation: 41

1

Welcome to PPCG! You can save some bytes by dropping the curly braces and return keyword: 30 bytes

– Kirill L. – 2019-03-14T11:57:24.867

123, also 23 – ASCII-only – 2019-03-14T12:50:31.277

1Nice first submission! – Embodiment of Ignorance – 2019-03-14T15:36:56.103

4

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.

JDL

Posted 2016-08-30T03:28:53.697

Reputation: 1 135

4

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.

Leaky Nun

Posted 2016-08-30T03:28:53.697

Reputation: 45 011

I don't understand the code, but the algorithm is clever so have a +1. – Cyoce – 2016-09-04T03:40:30.610

3

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.

user62131

Posted 2016-08-30T03:28:53.697

Reputation:

3

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.

Business Cat

Posted 2016-08-30T03:28:53.697

Reputation: 8 927

5doesn't work if number is like 2345 – Maltysen – 2016-08-30T03:37:26.753

@Maltysen Fixed – Business Cat – 2016-08-30T12:57:50.850

3

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!

Lynn

Posted 2016-08-30T03:28:53.697

Reputation: 55 648

What's the point of [>>]<? Couldn't that just be >? – James – 2016-08-30T05:12:45.073

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 – 2016-08-30T05:21:38.187

42 bytes – Leaky Nun – 2016-08-30T05:51:21.343

@LeakyNun That looks completely different; feel free to post it as a separate answer – Lynn – 2016-08-30T06:02:00.973

@Lynn Done.

– Leaky Nun – 2016-08-30T06:13:30.373

You beat Java 7, yes, but not Java 8 :P – Olivier Grégoire – 2016-08-31T13:37:14.110

3

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)

dulrich

Posted 2016-08-30T03:28:53.697

Reputation: 31

Doesn't N<900 save you a byte, in which case you can also use N>99 instead of LEN(N)=3. – Neil – 2016-08-30T07:49:35.150

121 bytes: =REPT(LEFT(N),3)+12=N where N is the name of the reference cell. – Engineer Toast – 2017-05-08T19:02:03.933

3

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.

WallyWest

Posted 2016-08-30T03:28:53.697

Reputation: 6 949

1two bytes: (n-12) and n>99 – Titus – 2016-08-30T07:31:09.957

@Titus Oh, very nice, +1 to you! – WallyWest – 2016-08-30T07:44:01.470

1=> is ES6, not ES5. – Neil – 2016-08-30T07:50:32.850

@Neil well spotted, thanks – WallyWest – 2016-08-30T07:51:48.750

1I believe it was decided in meta that you didn't have to count "f=" making this 26 bytes – Charlie Wynn – 2016-08-30T20:53:33.127

Thanks @CharlieWynn, is that because the function's declaration and assignment are arbitrary? – WallyWest – 2016-08-30T20:55:08.617

1@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 ;) – Charlie Wynn – 2016-08-30T20:58:01.673

Guess I'll be going through all my previous answers! – WallyWest – 2016-09-01T01:42:33.487

2

JavaScript (ES6), 34 bytes

And one more option in JS. Takes input as a string and outputs 0 for false and 1 for true.

n=>++n[0]==n[1]&++n[1]==n[2]&!n[3]

See my other solutions here and here


Try it

f=
n=>++n[0]==n[1]&++n[1]==n[2]&!n[3]
i.addEventListener("input",_=>o.innerText=f(i.value))
<input id=i type=number><pre id=o>

Shaggy

Posted 2016-08-30T03:28:53.697

Reputation: 24 623

2

Jelly, 5 bytes

9ṡ3Vċ

Try it online!

Erik the Outgolfer

Posted 2016-08-30T03:28:53.697

Reputation: 38 134

Yay outgolfed the Dennis with a perfectly competing answer. – Erik the Outgolfer – 2017-05-09T11:52:20.903

2

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≤k≤7.

Deadcode

Posted 2016-08-30T03:28:53.697

Reputation: 3 022

2

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.

Anastasiya-Romanova 秀

Posted 2016-08-30T03:28:53.697

Reputation: 1 673

21 bytes: =REPT(LEFT(N),3)+12=N where N is the name of the reference cell. – Engineer Toast – 2017-05-08T18:24:58.193

2

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

Adám

Posted 2016-08-30T03:28:53.697

Reputation: 37 779

2

Perl, 18 bytes

Includes +1 for -p

Run with the input on STDIN

123.pl <<< 123

123.pl:

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

Ton Hospel

Posted 2016-08-30T03:28:53.697

Reputation: 14 114

2

PHP, 31 bytes

<?=($n=$_GET[n])-12==$n[0]*111;

Check if first digit of (number minus 12) is multiple of 111

Florin Chis

Posted 2016-08-30T03:28:53.697

Reputation: 59

2

PowerShell v3+, 24 bytes

($args[0]-12)/111-in1..7

Uses the same "multiple of 111 plus 12" trick as some other answers, but goes the other direction. Takes input $args[0], subtracts 12, divides by 111, and checks whether that's -in the range 1..7. Outputs a Boolean true/false value. Requires v3+ for the -in operator.

Test Cases

PS C:\Tools\Scripts\golfing> 123,234,345,456,567,678,789|%{.\easy-as-one-two-three.ps1 $_}
True
True
True
True
True
True
True

PS C:\Tools\Scripts\golfing> 1,2,3,12,122,124,132,321,457,777,890,900,1011,1230,1234|%{.\easy-as-one-two-three.ps1 $_}
False
False
False
False
False
False
False
False
False
False
False
False
False
False
False

AdmBorkBork

Posted 2016-08-30T03:28:53.697

Reputation: 41 581

2

ARM Machine Code, 18 bytes

Hex dump (little endian):

3803 d105 6808 ebc0 2010 b280 f2a0 1001 4770

This is a function that takes in a length, pointer pair for the string. The output is bash-style, it outputs 0 for true and a non-zero value for false. In C the function would be declared int oneTwoThree(size_t length, char* string). The instruction encoding is thumb-2, which has 2 and 4 byte instructions. Tested on a Raspberry Pi 3.

Ungolfed assembly:

.syntax unified
.text
.global oneTwoThree
.thumb_func
oneTwoThree:
    @Input: r0 - the number of characters in the string
    @r1 - A pointer to the (not necessarily NUL-terminated)
    @string representation of the number (char*)
    @Output: r1 - 0 if the number is in 123,234,...,789, else non-zero (bash-style)
    subs r0,r0,#3
    bne end @Return non-zero if r0!=3
    ldr r0,[r1] @Remember that this is little endian
    @So the first digit is the most siginificant byte
    @I.e. if the input was 123 then r0 contains 0xXY010203 where XY is garbage

    rsb r0,r0,r0,lsr #8 @r0=(r0>>8)-r0 (rsb is reverse subtract)
    uxth r0,r0 @r0&=((1<<16)-1) (mask off top half)
    @Now r0 is 0x0101 iff we have a matching number
    sub r0,r0,#0x101
    @Now r0 is 0 iff the string fit the specification

    end:
    bx lr @return

Testing script (also assembly):

.syntax unified
.text
.global main
.thumb_func
main:
    push {r4,lr}
    ldr r4,[r1,#4] @r0=argv[1]
    mov r0,r4
    bl strlen
    @Now r0 is the length of the string argv[1]
    mov r1,r4
    bl oneTwoThree @oneTwoThree(strlen(argv[1]),argv[1])
    cmp r0,#0
    it ne
    movne r0,#1 @Output through return code, 1 if false
    pop {r4,pc}

Ian Chew

Posted 2016-08-30T03:28:53.697

Reputation: 171

1

JavaScript (ES6), 41 39 38 37 bytes

Just another JS option. Takes input as a string and outputs 0 for false and 1 for true.

n=>+n[2]&!n[3]&~"123456789".search(n)

See my other solutions here and here


Try it

f=
n=>+n[2]&!n[3]&~"123456789".search(n)
i.addEventListener("input",_=>o.innerText=f(i.value))
<input id=i type=number><pre id=o>

Shaggy

Posted 2016-08-30T03:28:53.697

Reputation: 24 623

1

JavaScript (ES6), 17 12 bytes

Takes input as an integer and outputs true or false.

n=>n%111==12

See my other solutions here and here


Try it

f=
n=>n%111==12
o.innerText=f(i.value=123)
i.addEventListener("input",_=>o.innerText=f(+i.value))
<input id=i type=number><pre id=o>

Original, 17 bytes

n=>n==n[0]*111+12

Shaggy

Posted 2016-08-30T03:28:53.697

Reputation: 24 623

1invalid, returns true for 12, 900 etc – ASCII-only – 2019-03-14T13:32:29.190

1

Japt, 6 5 bytes

%#o¥C

Try it online!

Luke

Posted 2016-08-30T03:28:53.697

Reputation: 4 675

I think you can change 12 to C to save a byte – ETHproductions – 2017-05-12T13:32:00.587

1

8086 Machine Code, 14 bytes

3C 03   CMP  AL, 3      ; check if input length is 3 chars exactly 
75 0A   JNZ  FALSY      ; if not, falsy  
AD      LODSW           ; load first char into AL, second into AH, advance SI 
40      INC  AX         ; increment first char 
3A E0   CMP  AH, AL     ; are chars now equal? 
75 04   JNZ  FALSY      ; if not, falsy
AC      LODSB           ; load third char into AL 
48      DEC  AX         ; decrement third char 
3A E0   CMP  AH, AL     ; are chars now equal?
FALSY:

Input string is SI, length in AL. Return is Zero Flag where (ZF=1) if Truthy, (ZF=0) if Falsy.

640KB

Posted 2016-08-30T03:28:53.697

Reputation: 7 149

1

Java 8, 23 bytes (thanks ASCII-only!)

n->n>12&n<790&n%111==12


Old version, 40 bytes

n->n>99&n<999&"123456789".contains(n+"")

-1 bytes thanks to gwaugh
Try it online!

Benjamin Urquhart

Posted 2016-08-30T03:28:53.697

Reputation: 1 262

@gwaugh fixed. Thanks. – Benjamin Urquhart – 2019-03-13T22:13:14.677

you could -1 byte by changing n<1000 to n<999 (or even n<790) since the highest Truthy number is 789 – 640KB – 2019-03-13T22:15:07.073

@gwaugh done. Thanks again – Benjamin Urquhart – 2019-03-13T22:18:23.810

23, same as C# – ASCII-only – 2019-03-14T12:55:12.360

1

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)

maxb

Posted 2016-08-30T03:28:53.697

Reputation: 5 754

It would be nice if 2. could be – Jo King – 2019-03-15T10:12:59.530

@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 – 2019-03-15T12:08:43.140

1@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 – 2019-03-18T14:15:40.670

1

Brachylog (v2), 4 bytes

~s₃Ị

Try it online!

Certainly not as creative as user62131's answer, but quite a bit shorter. Takes input as a string through the input variable and assumes the string has no leading zeroes (i.e. "012" is a false positive), outputting through success or failure, where success of a predicate run as a program prints true. and failure prints false.

        The input is a
~s₃     length-3 substring of
   Ị    "0123456789".

If input is allowed to be a bit less orthodox...

Brachylog, 3 bytes

Ịs₃

Try it online!

Outputs the same way and takes input in the same format, but takes it through the output variable. To run this as a program, the input is given as the first command-line argument on TIO, rather than through the Input box (which actually has to either be empty or contain "0123456789").

Unrelated String

Posted 2016-08-30T03:28:53.697

Reputation: 5 300

1

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

Steven H.

Posted 2016-08-30T03:28:53.697

Reputation: 2 841

1

Brain-Flak, 118 + 3 = 121 bytes

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

Try it online!

ASCII version: take code-points as input, output \x00 for falsey or \x01 for truthy.

Leaky Nun

Posted 2016-08-30T03:28:53.697

Reputation: 45 011

1

Pyke, 9 bytes

~ut{3Qlq&

Try it here!

~ut{      -  input in "123456789" 
        & - ^ and V
    3Qlq  -  len(input) == 3

Or 5 bytes if allowed sequence input

$1D]q

Try it here!

$     - delta(input)
 1D]q - ^ == [1,1] 

Blue

Posted 2016-08-30T03:28:53.697

Reputation: 26 661

1

Fish (><>), 14 bytes

With input from the command line/initial stack.

l3=?!;$:@---0=l1==n;

Explanation:

$:@              Swaps top two values on stack then duplicates topmost one, then swaps top three values
   ---             Each '-' subtracts the second most value on the stack from the topmost and pushes their result
      0=l1=          Checks if topmost value is 0, pushes 1 if true and 0 if false, then checks if length of stack is 1 (pushes 1 is true, 0 if false) 
         =n;           Does the logical && of the top two truthy/falsy values pushed in previous step and outputs the result.

Try it here

gowrath

Posted 2016-08-30T03:28:53.697

Reputation: 885

This actually returns false-positives for a lot of inputs, including any three digit number where all three digits are the same like 111. It also errors out without delivering a falsey if the initial input is less than three characters. – Callum Kerr – 2016-09-02T19:43:13.950

@CallumKerr You're right, I wrote this in a hurry. Let me fix it. – gowrath – 2016-09-02T19:46:51.003

1

PHP, 32 34 bytes

<?=(800>$x=$argv[1]-12?:1)>$x%111;

Run like this:

echo '<?=(800>$x=$argv[1]-12?:1)>$x%111;' | php -- 124

Updates:

  • I made a little mistake where 12 was considered valid.

aross

Posted 2016-08-30T03:28:53.697

Reputation: 1 583

1

Batch, 49 bytes

@for /l %%a in (123,111,789)do @if %%a==%1 echo 1

Neil

Posted 2016-08-30T03:28:53.697

Reputation: 95 035

You need @ before echo 1 – stevefestl – 2017-05-15T11:59:29.077

1

3d, 61 bytes

>:&&#a&×∕$#a∕#a%.--v
  v--.%a#$∕a#&%×&a#—'F!,;
v —'F!;
>'T!;

Will output:

T

if input is a three consecutive increasing digits,

F

if not.

Explanation:

:&&  push imput and duplicate twice
#a&× push 0xa (10), duplicate it, multiply top two of the stack: get 100
∕    floor divide top two elements of the stack (we get the first digit)
$    invert top two elements
#a∕  floor divide by 10 (get rid of last digit)
#a%  modulo 10 (get middle digit)
.-   push 1 and difference
-    difference (hence we get 0 if two initial digits have a difference of 1)
—    is top of stack null?
if not:
'F!  print 'F'
,;   pops the stack and exits
if yes:
#a&× get 100
%    modulo (get rid of first digit)
&    duplicate
#a∕  floor divide by 10 (get middle digit)
$    invert
#a%  modulo 10 (get last digit)
.- push 1 and difference
-  difference
— is top of stack null?
yes:
'T!; print 'T' and exit
not:
'F!; print F and exit

Phew, this really ain't golfy.

joH1

Posted 2016-08-30T03:28:53.697

Reputation: 391

Link to 3d? (filler text) – user48538 – 2016-09-06T08:53:58.560

yeah, forgot it... Adding it now! – joH1 – 2016-09-06T18:33:53.293

1

Java 8, 23 bytes

n->n<900&(n-122)%111==1

Ungolfed:

interface N { boolean f(int n); }

public static void main(String[] args) {
  N f = n ->
    n < 900 // 900 is not good
    &
    (n - 122) % 111 == 1 // force 12 to go negative and fail the comparison (can't use 123 because -111 % 111 == 0, while -110 % 111 == -1).
}

Test class:

public class Main {
  private interface N {

    boolean f(int n);
  }

  static void test(N f, int n, boolean expected) {
    boolean result = f.f(n);
    System.out.printf("%s -> %b (%b) -> %s%n", n, result, expected, result == expected ? "OK" : "NOK");
  }

  public static void main(String[] args) {
    N f = n->n<900&(n-122)%111==1;

    test(f,  123, true);
    test(f,  234, true);
    test(f,  345, true);
    test(f,  456, true);
    test(f,  678, true);
    test(f,  789, true);

    test(f,    1, false);
    test(f,    2, false);
    test(f,    3, false);
    test(f,   12, false);
    test(f,  122, false);
    test(f,  124, false);
    test(f,  132, false);
    test(f,  321, false);
    test(f,  457, false);
    test(f,  777, false);
    test(f,  890, false);
    test(f,  900, false);
    test(f, 1011, false);
    test(f, 1230, false);
    test(f, 1234, false);

  }
}

Olivier Grégoire

Posted 2016-08-30T03:28:53.697

Reputation: 10 647

1

Javascript (ES5), 47 39 bytes

function(n){return(n-12)%111==0&&i<1e3}

Old solution with 47 did not work correctly.

Paul Schmitz

Posted 2016-08-30T03:28:53.697

Reputation: 1 089

Old one returns true for 1234 – Tejas Kale – 2016-09-02T08:51:15.857

0

Jelly, 8 bytes

Not as short as Dennis's answer (duh), but I'm quite proud of myself for getting the results I wanted out of Jelly.

9Rṡ3
De¢

Try it online!

Explanation:

9R    Push 1-9 to the stack
ṡ3    Slice up the stack in parts of 3, overlapping
      This gives us [1,2,3], [2,3,4], ..., [7,8,9]
D     Convert input to list ( '123' -> [1, 2, 3])
e¢    This checks to see if the decimal list is in the list of our previous link.

steenbergh

Posted 2016-08-30T03:28:53.697

Reputation: 7 772

0

Clojure, 25 bytes

(set (range 123 790 111))

Basically the Haskell answer. Creates a range which includes all the truthy answers, then converts it into a set. Sets are functions in Clojure (they implement the function interface and can be called).

Returns the number as the truthy return (every number is truthy in Clojure), and nil for the falsey value.

Use:

(let [s (set (range 123 790 111))]

    (s 123)
    123

    (s 234)
    234

    (s 993)
    nil

Carcigenicate

Posted 2016-08-30T03:28:53.697

Reputation: 3 295

0

SmileBASIC, 27 bytes

INPUT N?N-12==N DIV&H64*111

Using Dennis's algorithm.

If forcing the user to add &H to their input is OK, this works for 24 bytes:

INPUT N?N-18==(N>>8)*273

12Me21

Posted 2016-08-30T03:28:53.697

Reputation: 6 110

0

QBIC, 21 bytes

;[7|~!12+111*a$=A|_Xq

This prints 1 if a match is found, nothing otherwise.

Explanation

;              Get A$ from the command line
[7|            FOR a=1; a <=7 ; a++ (for each of the 7 sequences)
  12+111*a     Generate the number 123, 234, 345 by mult. 111 x index, plus 12
 !        $    Cast that to string B$
~          =A  And IF that matches A$
|_Xq           THEN quit, printing 1
               IF and FOR loop implicitly closed

steenbergh

Posted 2016-08-30T03:28:53.697

Reputation: 7 772

0

Befunge-93, 57 53 bytes

#@ #.0 &# ::57*2+3*%34*-#<_# 34*`!#<_# 98*34**`#<_1.@

Try it online!

Explanation

#@ #.0        skip the terminator and output command. Adds a meaningless zero to 
                the stack.
&# ::         take in an input and add two more copies to the stack
57*2+3*%      adds input mod 111 ((5*7+2)*3) to the stack
34*-#<_#      compares 12 to the result, if they are the same, continue to move 
                right, else move left
34*`!#<_#     checks to see that the input is greater (`) than 12. If it is, 
                continue to move right, else move left
98*34**`#<_   check that 864 (9*8*3*4) is greater than the input. (the reason for 
                this is that 864 was an easy number to create that was greater than 
                the last possible number (789) and was less than the next number 
                divisible by 111 (900)
1.@           if you pass all the checks, output 1, otherwise...
@.0># &#      you will move back through the code heading left. (you will do 
                the operations, but that will not matter.) Skip the input 
                command. Output zero and end the code.

bwillsh

Posted 2016-08-30T03:28:53.697

Reputation: 11

0

Thue, 84 bytes

123z::=~1
234z::=~1
345z::=~1
456z::=~1
567z::=~1
678z::=~1
789z::=~1
a::=:::
::=
az

Outputs 1 when given one of the target values, and no output when given something else. The z at the end is needed to prevent false positives with numbers like 1230

pppery

Posted 2016-08-30T03:28:53.697

Reputation: 3 987

0

Stax, 5 bytes

╡e╦ù┌

Run and debug it

recursive

Posted 2016-08-30T03:28:53.697

Reputation: 8 616

0

Julia 1.0, 18 bytes

n->n∈123:111:789

Try it online!

Kirill L.

Posted 2016-08-30T03:28:53.697

Reputation: 6 693

0

Unexpanded Sinclair ZX-81 and Timex TS1000/1500 ~99 97 tokenised BASIC bytes

 1 LET A$="123456789"
 2 INPUT B$
 3 LET P=NOT PI
 4 FOR F=VAL "1" TO VAL "7"
 5 LET P=P+(A$(F TO F+VAL "2")=B$)
 6 NEXT F
 7 PRINT P

This is using a sub-string method to scan the string literal A$ for the match as per the test cases; if a match is found then the variable 1 is added to the P, otherwise zero is added to P.

Donkeysoft - Easy as one-two-three

Shaun Bebbers

Posted 2016-08-30T03:28:53.697

Reputation: 1 814

0

APL(NARS), 40 chars, 80 bytes

{(∼⍵⊆⎕D)∨3≠≢⍵:0⋄(⊂⍎¨⍕⍵)∊{⍵+0..2}¨⍳7:1⋄0}

This is a little long, but would return the 0 if type is not string, or if string is different "123","234",..,"789"; test:

  f←{(∼⍵⊆⎕D)∨3≠≢⍵:0⋄(⊂⍎¨⍕⍵)∊{⍵+0..2}¨⍳7:1⋄0} 
  f 'aaa'
0
  f ''
0
  f¨('12')(,'3')('3')('122')('124')('132')('123456789')
0 0 0 0 0 0 0 
  f¨('123')('456')('789')
1 1 1 

RosLuP

Posted 2016-08-30T03:28:53.697

Reputation: 3 036

0

Befunge-98 (PyFunge), 36 bytes

0c75*2+3*&# :b9*8*`#0_# \%# -#0_1.@.

Try it online!

Alternate version that uses the 2-D aspect of Befunge:

c75*2+3*&:98*b*`#v_\%-#v_1.@
                 >     >0. ^

I actually prefer the second, however the first is shorter. Also, the first version leaves some stack pollution, whereas the second leaves a clean stack. Either could easily be made for Befunge-93 with a small 4-byte addition. I did not include it because there is already a 93 answer.

JPeroutek

Posted 2016-08-30T03:28:53.697

Reputation: 734

0

PHP, 35 bytes

<?=($n=$argv[1]-99)<790&$n%111==24;

exploiting that the modulo of a negative number is never positive in PHP

Titus

Posted 2016-08-30T03:28:53.697

Reputation: 13 814

0

C 61 Bytes

f(char*v){puts(v[3]==0&v[0]+1==v[1]&v[1]+1==v[2]?"T":"F");}
g(v){puts(v<790&v%111==12?"T":"F");}
main(c, v)char**v;{
    f(v[1]);
    g(atoi(v[1]));
}

61 Bytes only includes the function. f(...) 36 Bytes using the trick from Geobits in g(...)

cleblanc

Posted 2016-08-30T03:28:53.697

Reputation: 3 360

0

GNU Octave : 74 bytes

golfed:

function r=g(a)t=conv(a-'0',[1,-1],'valid')==1;r=(sum(t)==2).*prod(t);end;

ungolfed:

function r=golf_123(a)
  t=conv(a-'0',[1,-1],'valid')==1;
  r=(sum(t)==2).*prod(t);
endfunction

Explanation: Treat the string as an integer vector, any two consecutive digits will then have derivative given the finite difference filter [1,-1] equal to 1 at each valid convolution output. The prod command calculates product, so it will short circuit to 0 if at least one entry is 0. The sum(t)==2 disqualifies any run of increasing digits longer or shorter than 3.

mathreadler

Posted 2016-08-30T03:28:53.697

Reputation: 141

0

JAVA 7, 57 bytes

boolean f(int x){return (int)Math.log(x)-1==3&x%111==12;}

Numberknot

Posted 2016-08-30T03:28:53.697

Reputation: 885

You can remove the space in return (int) – Cyoce – 2016-12-05T15:45:26.843

0

AWK, 33 28 27 Bytes

{$0=($1-12)/111~/^[1-7]$/}1

Prints 1 for corresponds to one of the desired values, 0 otherwise.

Updated answer is based on TimmyD's powershell answer.

Example usage:

awk '{$0=($1-12)/111~/^[1-7]$/}1' <<< 789

Prints 1

awk '{$0=($1-12)/111~/^[1-7]$/}1' <<< 12

Prints 0

Since this is using division rather than modulus, the result will be an integer for the desired values, but that integer needs to be between 1 and 7.

Try it online!

Robert Benson

Posted 2016-08-30T03:28:53.697

Reputation: 1 339

0

SpecBAS - 35 bytes

1 INPUT n: ?(n-12)/111 in [1 TO 7]

Uses the same formula as the Powershell answer, returns 0 for False and 1 for True.

My original version was 59 bytes (and seems more "honest" for being my own work). Input as number and converted to string to avoid "012" showing as True.

1 INPUT n: n$=STR$ n: ?LEN n$=3 AND POS(n$,"0123456789")>0

Brian

Posted 2016-08-30T03:28:53.697

Reputation: 1 209

0

C# 6 (36 bytes)

bool f(int x)=>x<790&0==(x+321)%111;

Lorentz Vedeler

Posted 2016-08-30T03:28:53.697

Reputation: 101

Is f necessary? – Cyoce – 2016-12-04T00:22:59.987

0

C, 71 bytes

Taking the input as int:

f,d,c;F(n){for(f=c=0;n;)d=n%10,c++,f|=d-(n/=10)%10-1&&n;return!f&c==3;}

Test main:

int main() {
  printf("%d\n", F(123));
  printf("%d\n", F(234));
  printf("%d\n", F(345));
  printf("%d\n", F(456));
  printf("%d\n", F(567));
  printf("%d\n", F(678));
  printf("%d\n", F(789));
  printf("%d\n", F(1));
  printf("%d\n", F(2));
  printf("%d\n", F(3));
  printf("%d\n", F(12));
  printf("%d\n", F(122));
  printf("%d\n", F(124));
  printf("%d\n", F(321));
  printf("%d\n", F(457));
  printf("%d\n", F(777));
  printf("%d\n", F(890));
  printf("%d\n", F(900));
  printf("%d\n", F(1011));
  printf("%d\n", F(1230));
  printf("%d\n", F(1234));
}

Stefano Sanfilippo

Posted 2016-08-30T03:28:53.697

Reputation: 1 059

0

Ruby, 114 bytes

i = gets.chomp.to_i
l = [123, 234, 345, 456, 567, 678, 789]
y=false
l.each {|x| if i==x then y=true end;}
puts y

I don't know how many bytes this answer is, but I'm guessing it's not that small.

user59211

Posted 2016-08-30T03:28:53.697

Reputation: 1

Do you need those spaces? – Blue – 2016-09-01T12:46:11.993

You can remove all but one space (puts y)..each can be .map. false can be p. if i==x then y=true end; can be i==x&&y=1 – Cyoce – 2016-12-04T00:09:21.250

0

Clojure, 48 bytes

(defn f [n] (= n ((set (range 123 900 111)) n)))

Test:

user=> (map (fn [n] [n (f n)]) [123 234 345 456 567 678 789])
([123 true]
 [234 true]
 [345 true]
 [456 true]
 [567 true]
 [678 true]
 [789 true])

user=> (map (fn [n] [n (f n)]) [1 2 3 12 122 124 132 321 457 777 890 900 1011 1230 1234])
([1 false]
 [2 false]
 [3 false]
 [12 false]
 [122 false]
 [124 false]
 [132 false]
 [321 false]
 [457 false]
 [777 false]
 [890 false]
 [900 false]
 [1011 false]
 [1230 false]
 [1234 false])

ntalbs

Posted 2016-08-30T03:28:53.697

Reputation: 131

I swear I didn't copy your answer! This can be significantly reduced. See my answer which is the same idea, but without all of the extra bits. – Carcigenicate – 2017-02-14T20:43:30.973

0

PHP 5.6, 48 bytes

<?=($a=$argv[1])==join("",range($a[0],$a[0]+2));

Wanted to try something a little different from the other PHP answers (and a different approach to most of the posted answers). Take the first number of the input string, add two to it and create a range array, then join those together to create a new string and then return if they're equal or not.

Run with:

echo '<?=($a=$argv[1])==join("",range($a[0],$a[0]+2));' | php -- 123

Samsquanch

Posted 2016-08-30T03:28:53.697

Reputation: 271

0

><> (Fish), 28 Bytes

l3-0=?v0n;>
?+1-@:<;n1^?+2-!

Input is via an execution argument to add each digit to stack, left to right.

Checks if the length of the stack (number) is three, then that there's a difference of one between the third and second digits, then that there's a difference of two between the first and third digits.

Try it online!

Callum Kerr

Posted 2016-08-30T03:28:53.697

Reputation: 131