# Return the flipped version of a number

When a number is shown on a calculator, it's possible to consider what various transformations of that number would look like. For example, on a seven-segment display, 2 is shown like this:

And when flipped horizontally it looks like this:

As such, the mirror image of 2 is 5.

The task in this challenge is to take a single-digit number, and return the number that's its mirror image (if possible). If its mirror image does not look like a number, return the number rotated 180 degrees (if possible). If neither of these are the case, return -1.

Here's the full list of inputs and outputs your program needs to handle:

Input   Output
0       0
1       -1
2       5
3       -1
4       -1
5       2
6       9
7       -1
8       8
9       6


As a challenge, the shortest code wins!

• I disagree with your last point -- a 1 on a 7 segment display would simply be flipped to the other side, so 1 should nap to 1. Jun 17, 2014 at 17:40
• I am confused about how to flip each digit. If 2 becomes 5, then 6 should become backwards 9, not 9. But if 6 becomes 9, then the flip is just a rotation, so 2 becomes another 2, not 5. Jun 17, 2014 at 18:41
• 6, 9 rotated 180 deg, 2, 5 flipped horizontally, and 1, 3 in fact are reflections of themselves across the vertical axis. Jun 17, 2014 at 19:24
• The translations defined in the question are not consistent at all. Why do 2 and 5 flip, but 3 doesn't? Jun 17, 2014 at 19:29
• I noticed a curious fact about the switchable numbers: they form opposite binary patterns, i.e. 2=010, 5=101. 6=0110, 9=1001. Can anyone use this fact in their solution? Jun 18, 2014 at 12:01

# Turtlèd, 40 bytes (noncompeting)

!.(3'1)(4'1)(1l'-)(2'5l)(5'2)(6'9l)(9'6)


Try it online!

Explanation

!                                          take input
.                                         write first char of input to cell
(3'1)                                    if cell is 3, write 1
(4'1)                               if cell is 4, write 1
(1l'-)                         If cell is 1, move left and write -
(2'5l)                   If cell is 2, write 5 and move left
(5'2)              If cell is 5, write 2
(6'9l)        If cell is 6, write 9 and move left
(9'6)   If cell is 9, write 6


# PHP, 48 39 bytes

Thanks to Jörg Hülsermann

<?=[0,-1,5,-1,-1,2,9,-1,8,6][$argv[1]];  Try it online! Older versions $m=-1;echo [0,$m,5,$m,$m,2,9,$m,8,6][$argv[1]];  Try it online! • Hey, thanks! Even better, should I post? – ʰᵈˑ Mar 31, 2017 at 14:58 # Jelly, 14 bytes (non-competing) ị“-5--29-860”V  Try it online! Explanation: ị“-5--29-860”V Takes argument as an integer. “-5--29-860” "-5--29-860" (1-indexed string). ị Return the xth char of the string above (y) (x=argument). V Eval. - is the same as -1.  • ịV = žh‡ in 05AB1E Mar 31, 2017 at 17:58 # Vyxal, 16 bytes kd0u5uu29u86⇿ₑ  Try it Online! Simply push the digits 0 to 9, push the string 0u5uu29u86 (the mapping of each number - u pushes -1 in Vyxal), transliterate and evaluate as Vyxal. # Excel, 31 bytes =MID(1060039097,A1+1,1)-(A1<>6)  # Python - 92 f=lambda x:x if x in(0,8)else(5if x==2 else(2if x==5 else(6if x==9 else(9if x==6 else -1))))  Usage: >>> f(2) 5 >>> f(3) -1  • def f(x): will make it a byte shorter. Also "[a,b][x==5]" is shorter then and equivalent to "a if x==5 else b" Jun 17, 2014 at 21:42 • Also, a small change would be to put the if x==9 last and say x>8 instead, saving a character. Jun 17, 2014 at 21:49 • @Pinna_be: You’re forgetting the return. – Ry- Jun 18, 2014 at 5:09 ## Python - 73 bytes r=[2,5,2,9,6,9,8,8,0,0] f=lambda x:-1 if x not in r else r[r.index(x)+1]  Or, for one digit numbers only : r=[0,-1,5,-1,-1,2,9,-1,8,6] f=lambda x:r[x]  • why not altogether f=(lambda x:[-1,-1,5,-1,-1,2,9,-1,8,6][x])(x) Jun 17, 2014 at 18:24 • 43 def f(x):print[-1,-1,5,-1,-1,2,9,-1,8,6][x] Jun 17, 2014 at 18:56 # PowerShell 32 (0,-1,5,-1,-1,2,9,-1,8,6)[$args]


# F# - 37 bytes

let f n=[0;-1;5;-1;-1;2;9;-1;8;6].[n]


Usage:

> f 2
val it : int = 5
> f 9
val it : int = 6
> f 4
val it : int = -1

• Fails for 0 and 8 Jun 17, 2014 at 22:30
• I missed that in the rules, thanks! Jun 17, 2014 at 22:31

## Java, 75

int f(int i){return i>8?6:i>7?8:i>6?-1:i>5?9:i>4?2:i>2?-1:i>1?5:i>0?-1:0;}

• i>8:-1 will return -1 for input 9 when it should return 6, won't it? Jun 17, 2014 at 21:52
• @corsiKa Indeed. Jun 17, 2014 at 23:47

## TI-BASIC, 35

{1,0,6,0,0,3,10,0,9,7}→L1:L1(X+1)-1


Previous attempt, 42:

6(X=2)+3(X=5)+10(X=6)+9(X=8)+7(X=9)-1+(X=0

• 30 bytes: {1,0,6,0,0,3,10,0,9,7}-1:Ans(X+1 Jun 8, 2015 at 1:50

## C++ 85

Uses nested ternary statements.

int main(){int a;cin>>a;a=(a==2)?5:(a==5?2:(a==6?9:(a==9?6:(a%8==0?a:-1))));cout<<a;}

• Doesn't C++ inherit the abuse of integers for Booleans of C? Jun 18, 2014 at 9:58
• @PeterTaylor Yes C++ accepts integers, but I don't want 'a' modified until the end. Jun 18, 2014 at 11:10
• No need to modify a. I was thinking that (a==X)?Y:Z can be (a-X)?Z:Y to save a few bytes. Jun 18, 2014 at 14:13
• Which compiler are you using? I can't find a gcc version that accepts that without #include <iostream> and std::. Mar 31, 2017 at 7:24

# Bash 26

bc<<<tr 0-9 106003A097-1


eg.

for i in {0..9}; do echo $i| bc<<<tr 0-9 106003A097-1 done 0 -1 5 -1 -1 2 9 -1 8 6  # Haskell (full program), 63 main=interact$maybe"-1"(:"").(lookupzip"025689""052986").head


Call main, and whatever character your input starts with gets used. Anything that isn't '0', '2', '5', '6', '8', or '9' prints "-1". Unfortunately show wraps characters in single quotes, so the output wouldn't have been consistent, so I had to use (:"") (or (:[])).

# Python3: 47

A very basic implementation, that accepts a input and translates accordingly.

print([0,-1,5,-1,-1,-1,9,-1,8,6][int(input())])


PEP8 is of course sacrificed for the sake of characters. This doesn't come close to rivaling the other Python solution near the top. However, as my first golf, I don't think it's that bad :).

• This could be written print[0,-1,5,-1,-1,-1,9,-1,8,6][input()] in Python 2. Also one could increase all the numbers by 1 to save one character. Jun 19, 2014 at 16:58

# Batch - 79

set /aa=%1+1&for /f "tokens=%a%" %%1 in ("0 -1 5 -1 -1 2 9 -1 8 6") do echo>%%1


Used as filename.bat number, output is as a file with the name of the correct number.

# Zozotez LISP 87

((:'f(\(k n v)(? k(?(=(a k)n)(a v)(f(d k)n(d v)))'-1)))'(0 2 5 6 8 9)(r)'(0 5 2 9 8 6))


This assumes you start it by seeding one of the REPL supplied so the printing actually is done by Without a driver/REPL you need 3 chars extra:

(p((:'f(\(k n v)(? k(?(=(a k)n)(a v)(f(d k)n(d v)))'-1)))'(0 2 5 6 8 9)(r)'(0 5 2 9 8 6)))


# Ruby, 30 bytes

What, no Ruby solution yet?

p'0 5  29 86'.index(\$*[0])||-1


Takes the input as a command-line argument.

# Pyth - 16 bytes

x[ZZ5ZZ2 9Z8 6)Q


Doesn't really count because Pyth was created after this was posted, but it's still pretty neat.

• @ThomasKwa Fixed. Jun 9, 2015 at 0:14

# dimwit24 23 bytes (non-competing)

Edit: removed end bracket, as it is not necessary

Another question that I thought could be good for working on the language with...

R2,5,6,9,[1-7]}5,2,9,6,-1


### Explanation:

• R - map replace
• 2,5,7,9,[1-7]} - the searches. Since 2, 5, 6, and 9 are checked first, 1-7 can be used and will simply check if it's 1, 3, 4, or 7. 0 and 8 will therefore stay the same.
• 5,2,9,6,-1 - the replacements. So 2 is replaced by 5, 5 is replaced by 2, etc.

# C (gcc), 46 27 bytes

f(i){i="BAGAADKAJH"[i]-66;}


Try it online!