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

1 2
3
0
\$\begingroup\$

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

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

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

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!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Stax, 5 bytes

╡e╦ù┌

Run and debug it

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Julia 1.0, 18 bytes

n->n∈123:111:789

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

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.

| improve this answer | |
\$\endgroup\$
1 2
3

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.