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
    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
    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\$ Jul 6, 2022 at 7:39
  • \$\begingroup\$ Related: lol is an ambigram, dad isn't \$\endgroup\$
    – Deadcode
    Jul 6, 2022 at 9:17

43 Answers 43

1
2
1
\$\begingroup\$

Seriously, 23 bytes

,$;`"01xxxx9x86"í`MRεj=

Try it online!

This is essentially a port of xnor's Python 2 solution.

Explanation:

,$;`"01xxxx9x86"í`MRεj=
,$;                      push two copies of str(input)
   `"01xxxx9x86"í`M      map: get each digit's rotated digit (or x if not rotatable)
                   Rεj   reverse and join on empty string to make a string
                      =  compare equality with original input
\$\endgroup\$
1
\$\begingroup\$

R, 111 bytes

With integer input and 0 output corresponding to truthy

f=sum
p=1:nchar(a<-scan())
b=a%%(l<-10^p)%/%(l/10)
d=(b-rev(b))^2
f(b>2&b<8&b!=6)+f(d[b<2|b==8])+f(d[!b%%3]!=9) 

Try it online!

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

Kotlin, 69 bytes

This takes the number, converts it to a string, rotates it, and then compares it with the original as a string for equality. Non-rotatable digits are simply converted to 0

{i->"$i".map{"0100009086"[it-'0']}.joinToString("").reversed()=="$i"}

Test it Here!

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

Nobody had posted a Python 3 solution so I'll go with my rubbish one

Python 3, 190 95 bytes

lambda x:not str(x).replace('0','').replace('1','').replace('6','').replace('8','').replace('9','').replace('0','')and str(x)[::-1].replace('6','z').replace('9','6').replace('z','9')==str(x)

EDIT:

lambda x:(x:=str(x))==[x:=x[::-1].replace(*p)for p in['6z','96','z9']][2]and not{*x}-{*'01689'}
\$\endgroup\$
1
1
\$\begingroup\$

K (ngn/k), 25 24 bytes

{d~|"01986""01689"?d:$x}

Try it online!

-1 byte thanks to @ngn

My first version was 74 bytes but then I read @xnor's solution and decided to translate that.

\$\endgroup\$
5
  • \$\begingroup\$ Using a Bubbler train doesn’t save any bytes here. f:~/1(|"01986"@"01689"?)\$: \$\endgroup\$
    – doug
    Aug 16, 2022 at 21:29
  • 1
    \$\begingroup\$ you can remove the @ \$\endgroup\$
    – ngn
    Aug 18, 2022 at 7:31
  • 1
    \$\begingroup\$ shorter: {d~|x[<x]x?d:$y}"01986" \$\endgroup\$
    – ngn
    Aug 18, 2022 at 7:39
  • \$\begingroup\$ Clever! It’s enough of a leap that I’ll leave mine as is though. This feels like a fresh idea. \$\endgroup\$
    – doug
    Aug 18, 2022 at 7:47
  • \$\begingroup\$ ok, i'll post a separate answer. i have yet another idea. \$\endgroup\$
    – ngn
    Aug 18, 2022 at 7:59
1
\$\begingroup\$

K (ngn/k), 21 bytes

{y~|x[>x]x?y}/$68910,

Try it online!

68910, prepend 68910 to the argument

$ format both as strings

{ }/ apply a function to x="68910" and y= the original argument as string

x?y find the positions of the elements of y among x. use "null" (0N) for "nout found".

x[>x] down-sorted x, i.e. "98610"

x[>x]x?y index x[<x] with x?y. out-of-bounds indexing produces spaces

| reverse

y~ test if y matches that

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

Zsh, 65 56 bytes

Thanks to pxeger for saving a whopping 9 bytes!

>${(j..)${(Oas..)1}}<${${${${1//6/.}//<2-7>}//9/6}//./9}

Try it online! Try it online!

Outputs via return code. The > construct creates and opens a file whose name is the reversed number for writing. The program will only exit truthy when the < construct attempts to read from a file with 6/9 swapped, and 2/3/4/5/7 removed.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ -2 bytes using the <-> glob syntax to match numbers: Try it online! \$\endgroup\$
    – pxeger
    Sep 12, 2022 at 12:23
  • \$\begingroup\$ Save another 7 by abusing the filesystem for the string comparison: Try it online! \$\endgroup\$
    – pxeger
    Sep 12, 2022 at 12:33
0
\$\begingroup\$

PHP, 83 bytes

<?=$argn==strrev(str_replace([6,9,x],[x,6,9],preg_replace("/[^01689]/","",$argn)));

Save the code in a file (let's say symmetry.php) and run it as:

php -d error_reporting=0 -F symmetry.php

Run it online on TIO.

The algorithm is simple: remove the non-symmetric characters, replace 6 with 9 and 9 with 6 then reverse the result. If it equals the input string then it is a rotationally symmetrical number.

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

Brev, 80 bytes

(define(r n)(= n((as-list reverse(over(vector-ref #(0 1 3 4 5 6 9 8 8 6)x)))n)))

Non-minified:

(define (rotsym? n)
  (= n ((as-list reverse (over (vector-ref #(0 1 3 4 5 6 9 8 8 6) x))) n)))
\$\endgroup\$
0
\$\begingroup\$

Swift, 148 bytes

var a=[0,1,6,8,9],b=[0,1,9,8,6],c=readLine()!.map {Int(String($0))!},f = false,t = c.count;for i in 0..<t{f = c[t-i-1] == b[a.index(of: c[i]) ?? 0]}
\$\endgroup\$
0
\$\begingroup\$

Scala, 184 bytes

Golfed version. Try it online!

def f(n:Int)=n.toString.map{case '1'=>'1';case '2'=>'w';case '3'=>'z';case '4'=>'y';case '5'=>'x';case '6'=>'9';case '7'=>'x';case '8'=>'8';case '9'=>'6';case c=>c}==n.toString.reverse

Ungolfed version. Try it online!

object Main {
  def f(n: Int): Boolean = {
    val original = n.toString
    val transformed = original.map {
      case '1' => '1'
      case '2' => 'w'
      case '3' => 'z'
      case '4' => 'y'
      case '5' => 'x'
      case '6' => '9'
      case '7' => 'x'
      case '8' => '8'
      case '9' => '6'
      case c => c
    }
    original == transformed.reverse.mkString
  }

  def main(args: Array[String]): Unit = {
    for (n <- 1 until 1005 if f(n)) {
      println(s"$n satisfies the condition")
    }
  }
}
\$\endgroup\$
0
\$\begingroup\$

Lua, 73 bytes

r=''s=...s:gsub('.',load'r=({0,1,0,0,0,0,9,0,8,6})[...+1]..r')print(r==s)

Try it online!

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

Thunno 2, 12 bytes

kDḣ»©®£~»Ṇr=

Try it online!

Thunno 2, Vyxal, and 05AB1E all at 12 bytes!

Explanation

kDḣ»©®£~»Ṇr=  # Implicit input
kDḣ           # Push "123456789"
   »©®£~»     # Push "100009086"
         Ṇ    # Transliterate
          r   # Reverse it
           =  # Equals the input?
\$\endgroup\$
1
2

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.