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

71 Answers 71

2
\$\begingroup\$

Jelly, 5 bytes

9ṡ3Vċ

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Yay outgolfed the Dennis with a perfectly competing answer. \$\endgroup\$ – Erik the Outgolfer May 9 '17 at 11:52
2
\$\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≤k≤7.

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

Brain-Flak, 118 + 3 = 121 bytes

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

Try it online!

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

\$\endgroup\$
1
\$\begingroup\$

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] 
\$\endgroup\$
1
\$\begingroup\$

Batch, 49 bytes

@for /l %%a in (123,111,789)do @if %%a==%1 echo 1
\$\endgroup\$
  • \$\begingroup\$ You need @ before echo 1 \$\endgroup\$ – stevefestl May 15 '17 at 11:59
1
\$\begingroup\$

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

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

  }
}
\$\endgroup\$
1
\$\begingroup\$

Javascript (ES5), 47 39 bytes

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

Old solution with 47 did not work correctly.

\$\endgroup\$
  • \$\begingroup\$ Old one returns true for 1234 \$\endgroup\$ – Tejas Kale Sep 2 '16 at 8:51
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Callum Kerr Sep 2 '16 at 19:43
  • \$\begingroup\$ @CallumKerr You're right, I wrote this in a hurry. Let me fix it. \$\endgroup\$ – gowrath Sep 2 '16 at 19:46
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Link to 3d? (filler text) \$\endgroup\$ – user48538 Sep 6 '16 at 8:53
  • \$\begingroup\$ yeah, forgot it... Adding it now! \$\endgroup\$ – joH1 Sep 6 '16 at 18:33
1
\$\begingroup\$

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>

\$\endgroup\$
1
\$\begingroup\$

Japt, 6 5 bytes

%#o¥C

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I think you can change 12 to C to save a byte \$\endgroup\$ – ETHproductions May 12 '17 at 13:32
1
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ invalid, returns true for 12, 900 etc \$\endgroup\$ – ASCII-only Mar 14 at 13:32
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ This returns Truthy for input of 1234 or 4567, etc. tio.run/… \$\endgroup\$ – 640KB Mar 13 at 22:08
  • \$\begingroup\$ @gwaugh fixed. Thanks. \$\endgroup\$ – Benjamin Urquhart Mar 13 at 22:13
  • \$\begingroup\$ you could -1 byte by changing n<1000 to n<999 (or even n<790) since the highest Truthy number is 789 \$\endgroup\$ – 640KB Mar 13 at 22:15
  • \$\begingroup\$ @gwaugh done. Thanks again \$\endgroup\$ – Benjamin Urquhart Mar 13 at 22:18
  • \$\begingroup\$ 23, same as C# \$\endgroup\$ – ASCII-only Mar 14 at 12:55
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ It would be nice if 2. could be \$\endgroup\$ – Jo King Mar 15 at 10:12
  • \$\begingroup\$ @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. \$\endgroup\$ – maxb Mar 15 at 12:08
  • 1
    \$\begingroup\$ @JoKing I realized that I could fit both those things into existing operators, which made sense to have. Check the readme for more changes. \$\endgroup\$ – maxb Mar 18 at 14:15
0
\$\begingroup\$

PHP, 35 bytes

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

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

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

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

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

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.

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

JAVA 7, 57 bytes

boolean f(int x){return (int)Math.log(x)-1==3&x%111==12;}
\$\endgroup\$
  • \$\begingroup\$ You can remove the space in return (int) \$\endgroup\$ – Cyoce Dec 5 '16 at 15:45
0
\$\begingroup\$

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

C# 6 (36 bytes)

bool f(int x)=>x<790&0==(x+321)%111;
\$\endgroup\$
  • \$\begingroup\$ Is f necessary? \$\endgroup\$ – Cyoce Dec 4 '16 at 0:22
0
\$\begingroup\$

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));
}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Do you need those spaces? \$\endgroup\$ – Blue Sep 1 '16 at 12:46
  • \$\begingroup\$ 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 \$\endgroup\$ – Cyoce Dec 4 '16 at 0:09
0
\$\begingroup\$

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])
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Carcigenicate Feb 14 '17 at 20:43
0
\$\begingroup\$

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

><> (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!

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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