23
\$\begingroup\$

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:

enter image description here

And when flipped horizontally it looks like this:

enter image description here

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!

\$\endgroup\$
17
  • 19
    \$\begingroup\$ 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. \$\endgroup\$
    – Jwosty
    Commented Jun 17, 2014 at 17:40
  • 32
    \$\begingroup\$ 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. \$\endgroup\$
    – kernigh
    Commented Jun 17, 2014 at 18:41
  • 6
    \$\begingroup\$ 6, 9 rotated 180 deg, 2, 5 flipped horizontally, and 1, 3 in fact are reflections of themselves across the vertical axis. \$\endgroup\$
    – jimmy23013
    Commented Jun 17, 2014 at 19:24
  • 25
    \$\begingroup\$ The translations defined in the question are not consistent at all. Why do 2 and 5 flip, but 3 doesn't? \$\endgroup\$
    – Rynant
    Commented Jun 17, 2014 at 19:29
  • 6
    \$\begingroup\$ 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? \$\endgroup\$ Commented Jun 18, 2014 at 12:01

55 Answers 55

1
2
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ Hey, thanks! Even better, should I post? \$\endgroup\$
    – ʰᵈˑ
    Commented Mar 31, 2017 at 14:58
1
\$\begingroup\$

Vyxal, 16 bytes

kd`0u5uu29u86`⇿ₑ

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.

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

Excel, 31 bytes

=MID(1060039097,A1+1,1)-(A1<>6)
\$\endgroup\$
1
\$\begingroup\$

Jelly, 14 bytes

ị“-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.
\$\endgroup\$
1
  • \$\begingroup\$ ịV = žh‡ in 05AB1E \$\endgroup\$ Commented Mar 31, 2017 at 17:58
1
\$\begingroup\$

Python 3.11 - 24

lambda x:"0.5..29.86"[x]

Based on this Python 2.x answer by Willem.

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

J, 18 bytes

_1".{&'0x5xx29x86'

Attempt This Online!

_1".{&'0x5xx29x86'
    {&'0x5xx29x86'  NB. index into a string literal
_1".                NB. eval the result using _1 as a default value
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ 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" \$\endgroup\$
    – Pinna_be
    Commented Jun 17, 2014 at 21:42
  • \$\begingroup\$ Also, a small change would be to put the if x==9 last and say x>8 instead, saving a character. \$\endgroup\$
    – Pinna_be
    Commented Jun 17, 2014 at 21:49
  • 1
    \$\begingroup\$ @Pinna_be: You’re forgetting the return. \$\endgroup\$
    – Ry-
    Commented Jun 18, 2014 at 5:09
0
\$\begingroup\$

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]
\$\endgroup\$
2
  • \$\begingroup\$ why not altogether f=(lambda x:[-1,-1,5,-1,-1,2,9,-1,8,6][x])(x) \$\endgroup\$ Commented Jun 17, 2014 at 18:24
  • 1
    \$\begingroup\$ 43 def f(x):print[-1,-1,5,-1,-1,2,9,-1,8,6][x] \$\endgroup\$ Commented Jun 17, 2014 at 18:56
0
\$\begingroup\$

PowerShell 32

(0,-1,5,-1,-1,2,9,-1,8,6)[$args]
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Fails for 0 and 8 \$\endgroup\$
    – Dancrumb
    Commented Jun 17, 2014 at 22:30
  • \$\begingroup\$ I missed that in the rules, thanks! \$\endgroup\$
    – Jwosty
    Commented Jun 17, 2014 at 22:31
0
\$\begingroup\$

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;}
\$\endgroup\$
2
  • \$\begingroup\$ i>8:-1 will return -1 for input 9 when it should return 6, won't it? \$\endgroup\$
    – corsiKa
    Commented Jun 17, 2014 at 21:52
  • \$\begingroup\$ @corsiKa Indeed. \$\endgroup\$
    – Ypnypn
    Commented Jun 17, 2014 at 23:47
0
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 30 bytes: {1,0,6,0,0,3,10,0,9,7}-1:Ans(X+1 \$\endgroup\$
    – lirtosiast
    Commented Jun 8, 2015 at 1:50
0
\$\begingroup\$

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

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

Haskell (full program), 63

main=interact$maybe"-1"(:"").(`lookup`zip"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 (:[])).

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

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 :).

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – seequ
    Commented Jun 19, 2014 at 16:58
0
\$\begingroup\$

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.

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

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)))
\$\endgroup\$
0
\$\begingroup\$

Ruby, 30 bytes

What, no Ruby solution yet?

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

Takes the input as a command-line argument.

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ @ThomasKwa Fixed. \$\endgroup\$ Commented Jun 9, 2015 at 0:14
0
\$\begingroup\$

dimwit 24 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.

Try it Here

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

C (gcc), 46 27 bytes

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

Try it online!

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

Thunno 2, 14 bytes

6 8u9yuu5u0K$i

Try it online!

¿£Ṣƥ*ðdiṅ×Ɗ€¿i

Try it online!

Explanation

6 8u9yuu5u0K$i  # Implicit input
6 8u9yuu5u0     # Push 6,8,-1,9,2,-1,-1,5,-1,0
           K    # Wrap the stack (reversed)
            $i  # Index the input in
                # Implicit output
¿£Ṣƥ*ðdiṅ×Ɗ€¿i  # Implicit input
¿£Ṣƥ*ðdiṅ×Ɗ€¿   # Push compressed list
                # [0,-1,5,-1,-1,2,9,-1,8,6]
             i  # Index the input in
                # Implicit output
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 25 bytes

x=>'0x'+'106003a097'[x]-1

Try it online!

\$\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.