Numbers with Rotational Symmetry
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]))
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!
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
)