37
\$\begingroup\$

Given an integer, output a truthy value if it is the same upside-down (rotated 180°) or a falsy value otherwise.

0, 1, and 8 have rotational symmetry. 6 becomes 9 and vice versa.

Sequence of numbers producing truthy results: OEIS A000787

0, 1, 8, 11, 69, 88, 96, 101, 111, 181, 609, 619, 689, 808, 818, 888, 906, 916, 986, 1001, 1111, 1691, 1881, 1961, 6009, 6119, 6699, 6889, 6969, 8008, 8118, 8698, 8888, 8968, 9006, 9116, 9696, 9886, 9966, 10001, 10101, 10801, 11011, 11111, 11811, 16091, ...

This question is inspired by my own reputation at the time of posting: 6009.

\$\endgroup\$
4
  • \$\begingroup\$ May we take the input as a string? \$\endgroup\$
    – xnor
    Commented Apr 15, 2016 at 21:26
  • \$\begingroup\$ @xnor If the language has numeric types, it should use them, for function parameters, for example. But if, in Python for example, you take raw_input, the user entered an integer, which would become a string behind the scenes. That's fine. \$\endgroup\$
    – mbomb007
    Commented Apr 15, 2016 at 21:32
  • \$\begingroup\$ On seven-segment displays, 🯲 and 🯵 both rotate to themselves - that would make a worthwhile extension to this challenge. \$\endgroup\$ Commented Jul 6, 2022 at 7:39
  • \$\begingroup\$ Related: lol is an ambigram, dad isn't \$\endgroup\$
    – Deadcode
    Commented Jul 6, 2022 at 9:17

43 Answers 43

15
\$\begingroup\$

Python 2, 50 bytes

lambda n:`n`==`map('01xxxx9x86'.find,`n`)`[-2::-3]

The method '01xxxx9x86'.find takes a digit character to it's upside-down number, with any unflippable digit giving -1. This function is mapped to the reversed number string, producing a list of digits.

This is converted to a string with the [1::3] trick, except it's reversed instead by doing [-2::-3] (thanks to Dennis for this, saving 4 bytes), and compared to the original number string. Any -1's from unflippable digits will misalign the conversion, making it fail.


56 bytes:

lambda n:`n`[::-1]==`n`.translate('01xxxx9x86______'*16)

Checks if the number string reversed is the same as it with the upside-down replacements. Digits that can't be flipped are replaced with 'x' to always give the wrong answer.

The replacement is done with translate on a string of 256 chars, replacing the corresponding ASCII values. Only the 10 values 48 to 57 matter, but I padded to length 16 to make the total length be 256. I wonder if there's a shorter way.

Some other approaches (lengths 59, 60, 60):

lambda n:set(zip(`n`,`n`[::-1]))<=set(zip('01896','01869'))
r=lambda s:set(zip(s,s[::-1]));lambda n:r(`n`)<=r('9018106')
lambda n:all(a+b in'001188969'for a,b in zip(`n`,`n`[::-1]))
\$\endgroup\$
1
9
\$\begingroup\$

Regex (ECMAScript / Python), 366 323 254 201 180 148 135 bytes

^(x$|x{8}$|(?=.*?(?=(((x*)\4{8}(?=\4$))+x$))(x*)\5{99})(?=(\2x)(|\6{5}(\6\6|\2{3}|xxx))(?!\2)(x*)\9{9}$)(?=(x*(?=\5|$))(|\5x)\9$)\10)*$

Takes its input in unary, as the length of a string of xs.

Try it online! - ECMAScript
Try it online! - Python
Try it on replit.com! - RegexMathEngine (much faster), in ECMAScript mode

It's impossible for a ECMAScript regex to match the sequence in decimal, but as demonstrated here, it's possible in unary.

The regex works by looping the following logic:

  1. Carve away both the leading and trailing digit if they are an allowed pair, or (for single-digit values) an allowed single digit.
  2. Prevent the value sent to the next iteration from having any leading zeroes, since the regex operates on numbers, not strings, so any leading zeroes would be stripped. If it has a leading zero, add \$1\$ to both the leading and trailing digit. If both were zero, both will be changed to \$1\$, preserving the trait of symmetry. If the trailing digit was nonzero (meaning the input is not a rotationally symmetric number), adding one of \$11, 101, 1001, ...\$ is guaranteed not to change it to one that is symmetric, because for example \$1001\$ could only be added to any number in the range \$0001\$ to \$0999\$ with a nonzero last digit, which can only yield a number in the range \$1002\$ to \$2000\$ with a last digit other than \$1\$, none of which are rotationally symmetric. This allows us to golf off 13 bytes, due to not needing the trailing zero check (?=(x{10})*$).

The 135 byte version has been tested from 0 to 1000000 using RegexMathEngine without finding any false positives or negatives. The test took 29.342 hours (single-threaded) on my i7-4930K @ 4.1 GHz.

(This was originally part of this answer and was split due to being very different in nature. See that post's edit history for changes made prior to the 254 byte length.)

^                        # tail = input number
(
    x$                   # If tail==1, let tail=0 and end the loop
|
    x{8}$                # If tail==8, let tail=0 and end the loop
|
    # Assert tail >= 10; find \2 = the largest power of 10 that is <= tail
    (?=
        .*?              # Decrease tail by the minimum necessary to satisfy
                         # the following assertion:
        (?=
            # Assert tail is a power of 10 >= 10; \2 = tail
            (((x*)\4{8}(?=\4$))+x$)
        )
        (x*)\5{99}       # \5 = floor(tail / 100)
    )

    # Require tail is of the form 1.*1, 8.*8, 6.*9, or 9.*6
    (?=
        (\2x)            # \6 = \2 + 1
        (
                         # 1.*1
        |
            \6{5}
            (
                \6\6     # 8.*8
            |
                \2{3}    # 9.*6
            |
                xxx      # 6.*9
            )
        )
        (?!\2)
        (x*)\9{9}$       # Assert tail is divisible by 10;
                         # \9 = tail / 10 == floor((N % \2) / 10)
    )

    # Set tail = \9, but if \9 is of the form 0.*0, the next iteration would
    # not be able to see its leading zero, so change it to the form 1.*1 in
    # that case, otherwise leave it unchanged. If \9 is of the form 0.*[1-9],
    # don't go to the next iteration.
    (?=
        (                # \10 = tool to make tail = \9 or \9 + \5 + 1
            x*
            # Assert that the leading digit of tail is not 0, unless tail==0
            (?=
                \5       # Assert that the leading digit of tail is not 0
            |            # or
                $        # Assert that tail==0
            )
        )
        # Iff necessary to satisfy the above, tail += \5 + 1
        (
        |
            \5x
        )
        \9$              # tail = \9
    )
    \10                  # tail = \9 or \9 + \5 + 1
)*                       # Execute the above loop as many times as possible,
                         # with a minimum of zero iterations.
$                        # Assert tail==0
\$\endgroup\$
8
\$\begingroup\$

05AB1E, 22 16 15 14 bytes

Code:

Â23457ð«-69‡Q

Try it online!


Previous code:

Â69‡Q¹¹„vd•ÃQ*

To figure out if the string is rotational symmetric, we just need to transliterate 69 with 96, reverse the string and check if they are equal. The other thing we need to know is if the number only contains the digits 0, 1, 8, 6 and 9. So that's exactly what we are going to do:

                     # Bifurcate the input, which pushes input and input[::-1]
 69Â                  # Bifurcate 69, which pushes 69 and 96.
    ‡                 # Transliterate 69 with 96 in the input, e.g. 1299 becomes 1266.
     Q                # Check for equality.
      ¹¹              # Push input again twice.
        „vd•          # Compressed version for 10869.

The „vd• part actually converts the string vd from base 190 to base 10. You can try this out here.

            Ã        # Keep the characters of the second string in the first string.
                       667788 would become 6688 (since 7 is not in 10869).
             Q       # Check for equality.
              *      # Multiply the top two numbers, which actually does an AND function.

Uses CP-1252 encoding. Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Your code doesn't seem to work on TIO anymore. Why is that? \$\endgroup\$
    – mbomb007
    Commented Dec 23, 2020 at 17:10
7
\$\begingroup\$

05AB1E, 12 bytes

žhÀ•yΘOñ•‡RQ

Try it online!

I somehow managed to beat Adnan's 05AB1E answer lol. This

Explained

žhÀ             # Push "1234567890"
   •yΘOñ•       # Push "1000090860"
         ‡      # Transliterate the input based upon the previous two strings
          RQ    # Check if the reverse of that equals the input
\$\endgroup\$
6
\$\begingroup\$

Ruby, 54 46 bytes

 ->a{(b=a.to_s).tr('1-9','1w-z9x86').reverse==b}

I don't know, is anonymous functions like that allowed or not

Basically same idea as Python2 answer. If input is not integer, act bad (i.e. aba gives true)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! Nice first answer :D Anonymous functions are allowed in any context where normal named functions are allowed. \$\endgroup\$
    – cat
    Commented Apr 16, 2016 at 0:08
  • \$\begingroup\$ What even is a “function” by our standards? \$\endgroup\$
    – cat
    Commented Apr 16, 2016 at 0:10
6
\$\begingroup\$

Regex (Perl / PCRE2 / Boost / Pythonregex), 35 bytes

^(([018])((?1)\2|)|6(?1)9|9(?1)6|)$

Takes its input in decimal.

Try it online! - Perl
Try it online! - PCRE2
Try it online! - Boost
Attempt This Online! - Python import regex

^              # Assert this is the start of the string
(              # Define (?1) recursive subroutine call
    ([018])    # \2 = match a 0, 1, or 8
    ((?1)\2|)  # Optionally match (?1) followed by \2
|              # or
    6(?1)9
|              # or
    9(?1)6
|              # or
    # Match an empty string
)
$              # Assert this is the end of the string

Regex (Ruby), 43 bytes

^(([018])(\g<1>\k<2+0>|)|6\g<1>9|9\g<1>6|)$

Try it online!

This is a direct port of the Perl/PCRE2/Boost version to Ruby's subroutine call syntax and behavior; (?1) has been changed to \g<1>, and \2 to \k<2+0>.

In Perl, PCRE, and Boost, the contents of capture groups are passed to deeper recursion levels, but upon return from a subroutine call, all captures are returned to the state they were in before the subroutine was called, i.e. there is no way to pass captures done within a subroutine call back to its caller. But in Ruby, captures are passed to the caller. So \2 will refer to whatever it captured at the deepest recursion level, and so would \k<2>. It is possible, however, to refer to a capture's value at a specific recursion level; that is why \k<2+0> is used here to refer to its value at the current level.

Regex (.NET), 94 77 66 53 bytes

^((?=([018]|6.*9|9.*6)(?<=(.))).?)+(?<-3>\3)*(?(3)^)$

Try it online!

Implementing this using .NET's Balancing Groups feature turns out to provide an edge over the 57 byte .NET-compatible version that doesn't use any .NET-specific features.

^                 # Assert we're at the beginning of the string
(
    (?=
        # Depending on what the next left-edge digit is, push its corresponding
        # right-edge digit onto the Group 3 stack.
        (
            [018]
        |
            6.*9
        |
            9.*6
        )
        (?<=(.))  # \3 = the right-edge digit (push it onto the Group 3 stack)
    )
    .?
)+
(?<-3>\3)*        # Match all right-edge digits in reverse order as they're
                  # popped from the stack.
(?(3)^)           # Assert that the Group 3 stack is now empty (literally: if
                  # its stack is not empty, assert something impossible, that
                  # we're at the beginning of the string).
$                 # Assert we've reached the end of the string, having matched
                  # all of its characters.

Regex (Perl / PCRE / .NET), 106 62 57 bytes

^((?=([018]|6.*9|9.*6)(?<=(.))).?(?=.*(\3(?(4)\4))))*\4?$

Try it online! - Perl
Try it online! - PCRE1
Try it online! - PCRE2
Try it online! - .NET

In PCRE1, subroutine calls are atomic, which prevents the Perl/PCRE2/Boost version (which requires backtracking to extend into nested subroutine calls) from working. As for .NET (and Java), subroutine calls are not supported.

In this version, instead of using recursion, we read inward from the left and right edges, one pair of characters at a time, remembering the right-edge position using capture group \4. A set of alternatives followed by a lookbehind is used to set the required right-edge digit based on whether the left-edge digit was [018], 6, or 9.

^
(
    # Depending on the next left digit, capture the required right digit in \3
    (?=
        (
            [018]
        |
            6.*9
        |
            9.*6
        )
        (?<=(.))       # Capture the previous character (the right digit) in \3
    )
    .?
    (?=
        .*
        (              # Capture \4 as the following:
            \3
            # Match \4 iff it has been captured (i.e., iff this is not the
            # first iteration). As such, \4 will be prepended with the current
            # right digit.
            (?(4)\4)
        )
    )
)*
\4?
$

Regex (Perl / PCRE / Java / .NET), 113 76 69 64 bytes

^((?=([018]|6.*9|9.*6)(?<=(.))).?(?=.*(\3(\6\4|(?!\6))())))*\4?$

Try it online! - Perl
Try it online! - PCRE1
Try it online! - PCRE2
Try it online! - Java
Try it online! - .NET

This is a port of the Perl/PCRE/.NET version, removing the use of the conditional due to their lack in Java, and emulating it with an empty capture whose set/unset status can be detected. Only the modified portion is shown below:

            # Match \4 iff it has been captured (i.e., iff this is not the
            # first iteration). As such, \4 will be prepended with the current
            # right digit.
            (
                \6\4
            |
                (?!\6)
            )
            ()         # \6 = set and empty, to signal \4 is captured
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 56 bytes

n=>n==[...''+n].reverse().map(c=>'0100009086'[c]).join``
\$\endgroup\$
4
  • \$\begingroup\$ What does the ... in the array do? \$\endgroup\$
    – ericw31415
    Commented Apr 21, 2016 at 20:35
  • \$\begingroup\$ @ericw31415 In this case it turns the string into an array of characters. More generally, you can provide anything iterable and it will iterate it and include the elements in the array. \$\endgroup\$
    – Neil
    Commented Apr 21, 2016 at 22:44
  • \$\begingroup\$ So [...''+n] is essentially the same thing as n.split("")? Where can I read more about this? \$\endgroup\$
    – ericw31415
    Commented Apr 21, 2016 at 22:47
  • \$\begingroup\$ @ericw31415 Yes, in this case I use it as a shorthand for split``. See A more powerful array literal \$\endgroup\$
    – Neil
    Commented Apr 21, 2016 at 22:52
4
\$\begingroup\$

sh, 40 33 bytes

[ `rev<<<$1|tr 6923457 96` = $1 ]

Input via command line argument, output via exit code. Generate all testcases:

for i in `seq 0 10000`
do
  if [ `rev<<<$i|tr 6923457 96` = $i ]
  then
    echo $i
  fi
done
\$\endgroup\$
1
  • \$\begingroup\$ I'm guessing this is Bash, rather than standard shell, given the <<< redirection? \$\endgroup\$ Commented Aug 22, 2022 at 7:15
4
\$\begingroup\$

Retina, 40 38 33 bytes

$
;$_
T`92-7`69`;.+
+`(.);\1
;
^;

Try it online!

Explanation

This uses a completely different approach from the other Retina answer. Instead of removing all symmetric parts, we simply perform the transformation of reversing the string and swapping 6 and 9s and then compare for equality. To make sure that no non-symmetric digits appear we also turn them into 9s in one half.

$
;$_

We duplicate the input by matching the end of the string and inserting ; followed by the entire input.

T`92-7`69`;.+

This performs a character transliteration only on the second half by matching it with ;.+. The two transliteration sets expand to:

9234567
6999999

Because 2-7 denotes a range and the target set is padded with the last character to match the length of the source set. Hence, the stage swaps 6 and 9 and also turns all of 23457 into 9s.

+`(.);\1
;

Repeatedly (+) remove a pair of identical characters around the ;. This will either continue until only the ; is left or until the two characters around the ; are no longer identical, which would mean that the strings aren't the reverse of each other.

^;

Check whether the first character is ; and print 0 or 1 accordingly.

\$\endgroup\$
1
  • \$\begingroup\$ Lever, very lever. \$\endgroup\$ Commented Apr 17, 2016 at 1:29
4
\$\begingroup\$

Knight, 70 bytes

;=p=sP;=a'';Ws;=a+I|?54=bAs?57bA-111bI||?48b?49b?56bAb''a=sSsF1''O?a p

Working with strings is a pain in Knight

Try It Online!

Test Suite

\$\endgroup\$
19
  • 1
    \$\begingroup\$ Thanks. I did so. Also, it's cool that people still answer my questions from years ago even though I don't really codegolf anymore. \$\endgroup\$
    – mbomb007
    Commented Aug 15, 2022 at 20:06
  • 1
    \$\begingroup\$ @mbomb007 In this case, your question was a dupe target for another recently asked question. I've already created an answer for that question but it got closed, so I decided to post the answer here instead. \$\endgroup\$
    – Aiden Chow
    Commented Aug 15, 2022 at 20:11
  • 1
    \$\begingroup\$ @mbomb007 Also, if I may ask, how come you don't code golf anymore? Is it because you got bored of it? \$\endgroup\$
    – Aiden Chow
    Commented Aug 15, 2022 at 20:13
  • 1
    \$\begingroup\$ Kind of. It's still interesting, but it had little reward. The main reason is that programming is my job, so I don't want to code outside of work. Also, most of my posts were during work at a job that sometimes left me without much to do. However, I now work at a job that's more demanding. Yet, I still get to have some hacky fun, as I make use of Retina at work for manipulating data when coding in another language (e.g. expanding a list of member variables into a full class w/ constructor) \$\endgroup\$
    – mbomb007
    Commented Aug 15, 2022 at 20:34
  • 1
    \$\begingroup\$ For what it's worth AidenChow and @doug, I'm in the process of revamping the Rust version, which will have some flags which you can toggle to make it only accept 100% spec-compliant programs \$\endgroup\$
    – Sampersand
    Commented Aug 18, 2022 at 17:24
4
\$\begingroup\$

C, 73 82 bytes

f(x){int y=0,z=x;for(;x;x/=10)y=y*10+"0100009086"[x%10]-'0';return z==y;}

Expanded

static const char s[] = "0100009086";
int f(int x)
{
    int y=0, z=x;
    for(; x; x/=10)
        y = y*10 + s[x%10]-'0';
    return z==y;
}

Explanation

We reverse the digits of x using modulo-10 arithmetic, replacing 6 and 9 by their reflections as we go. We replace the rotationally-asymmetric digits by zeros (note that we can handle symmetric fives and/or twos by simply changing the replacement table s). If the new number is equal to the original (saved in 'z'), then it is rotationally symmetric.

Test program

#include <stdio.h>
int main()
{
    int i;
    for(i=0;  i <= 16091; ++i)
        if (f(i))
            printf("%d\n", i);
}

This prints the list of symmetric numbers given in the question.

\$\endgroup\$
1
  • \$\begingroup\$ 68 bytes by using unprintable characters in the string literal, and moving the declaration of temporary variables into the function's argument list (nicer than the global variables offered by @c--). \$\endgroup\$
    – Deadcode
    Commented Aug 22, 2022 at 7:45
3
\$\begingroup\$

Jelly, 16 15 bytes

,ȷ9+90860¤Dị/⁼Ṛ

Try it online!

How it works

,ȷ9+90860¤Dị/⁼Ṛ  Main link. Argument: n (integer)

         ¤       Evaluate the three links to the left as a niladic chain:
 ȷ9              Yield 1000000000.
    90860        Yield 90860.
   +             Add to yield 1000090860.
,                Pair; yield [n, 1000090860].
          D      Convert both integers to base 10.
           ị/    Reduce by index, i.e., for each digit of n, retrieve the element
                 of [1, 0, 0, 0, 0, 9, 0, 8, 6, 0] at that index (1-based).
              Ṛ  Yield the digits of n in reversed order.
             ⁼   Test for equality.
\$\endgroup\$
3
  • \$\begingroup\$ This answer is tied with another at 15 bytes, but the other answer was posted first. Can you shave off 1 byte? \$\endgroup\$
    – mbomb007
    Commented Apr 18, 2016 at 14:50
  • 1
    \$\begingroup\$ I've tried quite hard; I don't think I can. However, our default tie breaker is first answer to achieve the winning score, and I think I got to 15 first. \$\endgroup\$
    – Dennis
    Commented Apr 18, 2016 at 15:06
  • \$\begingroup\$ In a newer version of Jelly, ȷ9+90860¤ can be “?¡pm’ for 12 bytes \$\endgroup\$
    – naffetS
    Commented Aug 17, 2022 at 17:28
3
\$\begingroup\$

Retina, 57 49 bytes

8 bytes saved thanks to @Martin Büttner.

+`^(([018])(.*)\2|9(.*)6|6(.*)9)$
$3$4$5
^[018]?$
  • Apply reduction as such: 1610880191 -> 61088019 -> 108801 -> 0880 -> 88 -> (empty).
  • Returns 1 if only 0, 1, 8, or (empty) is left.
  • Returns 0 otherwise.

Try it online!

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

TSQL, 122 bytes

I am new to code golf, so not quite sure how to count the characters. Counting it as 1 here since the number used in this case is 8

This will return 1 when the reversed value match and nothing when it doesn't match:

SELECT 1FROM(values(8))x(x)WHERE(x)NOT like'%[23457]%'and REPLACE(REPLACE(x,6,4),9,6)=REVERSE(REPLACE(REPLACE(x,9,4),9,6))

Human readable:

SELECT 1
FROM
  (values(808))x(x)
WHERE(x)
  NOT like'%[23457]%'
  and REPLACE(REPLACE(x,6,4),9,6)=REVERSE(REPLACE(REPLACE(x,9,4),9,6))
\$\endgroup\$
3
\$\begingroup\$

GNU sed, 84 bytes

(including +1 for -r flag)

:
s/^6(.*)9$/\1/
s/^9(.*)6$/\1/
s/^([081])(.*)\1$/\2/
t
s/^[081]$//
s/.+/0/
s/^$/1/

If the line ends with a rotation of its beginning character, trim both ends by one. Repeat until there's no match. Account for a single symmetric character, then if anything remains, the input was not symmetric, and we return false; otherwise return true.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can get -20B by merging first 4 substitutions together s/^(([081])((.*)\2)?|6(.*)9|9(.*)6)$/\4\5\6/. \$\endgroup\$
    – Jiří
    Commented Aug 14, 2022 at 15:18
3
\$\begingroup\$

Wolfram Language (Mathematica), 56 bytes

#~ContainsNone~{2,3,4,5,7}&&Reverse@#==(#/.{6->9,9->6})&

Try it online!

Takes input as a list of digits.

Explanation:


#~ContainsNone~{2,3,4,5,7} Check if the input contains no unflippable digits

&&Reverse@# and that the reverse of the input

==(# is equal to the input

/.{6->9,9->6})& where all 6s are replaced with 9s and 9s with 6s.

\$\endgroup\$
1
  • \$\begingroup\$ Try it online! - shows the values that return True; better than a raw True/False printout. But your function should take an integer (71 bytes), not a digit list. \$\endgroup\$
    – Deadcode
    Commented Jun 25, 2022 at 7:11
3
\$\begingroup\$

Julia

47 bytes

The first attack.

!x=10(d=digits(x))+reverse(d)⊆[0,11,88,69,96]

Attempt This Online!

39 bytes

Using @b_str it can be golfed further as proposed by @MarcMush and @Deadcode:

Attempt It Online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ I love this approach! you can destroy my solution by using a @b_str 41 bytes \$\endgroup\$
    – MarcMush
    Commented Jun 26, 2022 at 8:47
  • 1
    \$\begingroup\$ and using 9 instead of 10 seems to work too 40 bytes \$\endgroup\$
    – MarcMush
    Commented Jun 26, 2022 at 8:58
  • 1
    \$\begingroup\$ @MarcMush You can just use a raw NUL character instead of escaping it for 39 bytes \$\endgroup\$
    – Deadcode
    Commented Jun 26, 2022 at 21:07
3
\$\begingroup\$

Pure bash, 99 bytes

Using shopt -s extglob:

o=;for i in ${1//*()/ };do o=${i//6/_}$o;done; o=${o//9/6} o=${o//_/9}
[[ ${o//[23457]} == "$1" ]]

There are 98 bytes +1 for extglob -> 99 bytes.

Without extglob, 101 bytes

o=;for((i=${#1};i--;)){ o+=${1:i:1};};o=${o//6/_} o=${o//9/6} o=${o//_/9}
[[ $1 == "${o//[23457]}" ]]

Sample:

rotatable() {
    o=;for i in ${1//*()/ };do o=${i//6/_}$o;done; o=${o//9/6} o=${o//_/9}
    [[ ${o//[23457]} == "$1" ]]
}
for((x=0;x<10000;x++)){ rotatable $x&&echo $x;}|column 
0       69      111     689     906     1111    6009    6969    8888    9696
1       88      181     808     916     1691    6119    8008    8968    9886
8       96      609     818     986     1881    6699    8118    9006    9966
11      101     619     888     1001    1961    6889    8698    9116
\$\endgroup\$
2
\$\begingroup\$

Perl, 29 26 bytes

Includes +1 for -p

Run with the input on STDIN:

rotation.pl <<< 69

rotation.pl:

#!/usr/bin/perl -p
$_=reverse==y/962-7/69a/r
\$\endgroup\$
0
2
\$\begingroup\$

MATL, 25 21 22 bytes

j69801VmAGtP69VtPXE=vA

Try it Online!

Explanation

j       % Explicitly grab input as string
69801   % Number literal
V       % Convert to a string
mA      % Check if all input chars are members of this list
G       % Explicitly grab the input
tP      % Duplicate and flip it
69      % Number literal
V       % Convert to string ('69')
tP      % Duplicate and flip it ('96')
XE      % Replace all '6' with '9', and '9' with '6'
=       % Check that this substituted string is equal to the original
v       % Vertically concatenate all items on the stack
A       % Ensure everything is true
        % Implicitly print boolean result
\$\endgroup\$
2
\$\begingroup\$

Pyth, 17 bytes

!-FmC_B`d,QC\􄽥

Test it in the Pyth Compiler.

How it works

!-FmC_B`d,QC\􄽥  (implicit) Store the input in Q.

            \􄽥  Yield the Unicode character with code point 1068901.
           C    Compute its code point.
         ,Q     Pair the input and 1068901.
   m            Map; for each d in [Q, 1068901]:
       `d         Yield the string representation of d.
     _B           Yield the pair of `d` and `d` reversed.
    C             Zip. For the second string, this gives
                  ['11', '00', '69', '88', '96', '00', '11'].
 -F             Fold by difference, i.e., removes all pairs in the second list
                from the first list.
!               Logically negate the result, returning True iff the list is empty.
\$\endgroup\$
1
  • \$\begingroup\$ This doesn't work in current version of Pyth, but it can be shortened to 16 bytes anyway using a 3-byte ASCII string instead of a single Unicode character: Try it online! \$\endgroup\$
    – Deadcode
    Commented Jun 26, 2022 at 20:44
2
\$\begingroup\$

Visual Basic for Applications, 150 111 bytes

Usable in console or as an UDF.

Function c(b)
c=(Not b Like"*[!01869]*")And b=Replace(Replace(Replace(StrReverse(b),9,7),6,9),7,6)
End Function

Improved by taking advantage of implicit type conversions and by doing three steps swapping instead of two steps on each side of the equation. Count includes Function and End Function statements.

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

Stax, 16 bytes

ê±°";╙ñ♀C¼EE^☼ùΣ

Run and debug it

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

AWK, 85 84 82 80 bytes

{n=split($1,a,s="");for(;n;){b=a[n--];s=s (b~/[2-57]/?9:b~6?9:b~9?6:b)}$1=s~$1}1

This feels like brute-forcing, but awk has very limited tools, mostly aimed to text processing. This code allows multiple inputs.

{
n=split($1,a,s="");    # this splits $1 (the input) into the array _a_
                       # using an empty string as token. each element of the
                       # array is a digit of the input number.
                       # also, sets the number of elements to _n_,
                       # and sets the empty string to _s_.
    
for(;
    n;          # loops until n==0 (evaluates to false)
   ){
      b=a[n--]; # sets the _n_ element of the array to _b_. this will save bytes later
                # immediately after, decrements _n_ by 1 (one less byte here)

                        # here the magic happens:
      s=s (             # this appends a chaining conditional to the variable _s_
          b~/[2-57]/?9: # if _b_ equals 2, 3, 4, 5 or 7, returns 9 (dummy number)
          b~6?9:        # otherwise, if _b_ equals 6, returns 9
          b~9?6:        # otherwise, if _b_ equals 9, returns 6
          b             # otherwise, returns its value
          )
    }
$1=s~$1      # evaluates if _s_ matches $1, and sets this boolean to $1
}
1            # prints $1.

Try it online!


AWK, 78 bytes

This is almost the same as above, but without the s variable. Two less bytes, with the effect of allowing only one input.

{n=split($1,a,"");for(;n;){b=a[n--];s=s (b~/[2-57]/?9:b~6?9:b~9?6:b)}$1=s~$1}1

Try it online!

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

Julia 1.0, 45 bytes

!x=prod(d->"01....9.86"[1+d],digits(x))=="$x"

Try it online!

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

Vyxal, 12 bytes

»\Π»F∑69S*Ṙ=

Try it Online!

    F∑       # Remove elements of...
»\Π»         # 23457
         *   # Ring translate by...
      69S    # "69"
          Ṙ= # Does it, reversed, equal the input?
\$\endgroup\$
1
  • \$\begingroup\$ Haha 69 funny number \$\endgroup\$
    – lyxal
    Commented Jun 25, 2022 at 6:43
2
\$\begingroup\$

JavaScript (Node.js), 55 bytes

n=>!(e=[...''+n]).some(c=>'0x'+'01aaaa9a86'[c]-e.pop())

Try it online!

Half test of n=>![...''+n].some((c,i,e)=>'0100009086'[c]-e.slice(~i)[0])

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

C# (Visual C# Interactive Compiler), 58 bytes

n=>$"{n}".Aggregate("",(z,c)=>"01----9-86"[c-48]+z)==n+"";

Try it online!

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

PHP, 49 bytes (anonymous function)

fn($s)=>$s==strrev(strtr($s,"6923457","9600000"))

Try it online!

The strtr translates 6 to 9, 9 to 6, and all non-symmetric digits to 0 (any character other than themselves would work). The digits 0, 1, and 8 are left unchanged. (The order I put them in is purely a stylistic choice; I don't think it even makes a difference in speed.)

Then strrev reverses this result, which is then compared with the original string; if they're equal, it has rotational symmetry.

PHP -F, 51 bytes (full program)

<?=$argn==strrev(strtr($argn,"6923457","9600000"));

Try it online!

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

Pyth - 21 bytes

&!-z+`180K`69qzX_z_KK

Test Suite.

\$\endgroup\$
1
  • \$\begingroup\$ X_zK does the same job as X_z_KK. 3rd parameter is optional. \$\endgroup\$
    – Jakube
    Commented Apr 15, 2016 at 21:04

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.