22
\$\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
    Jun 17, 2014 at 17:40
  • 30
    \$\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
    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
    Jun 17, 2014 at 19:24
  • 24
    \$\begingroup\$ The translations defined in the question are not consistent at all. Why do 2 and 5 flip, but 3 doesn't? \$\endgroup\$
    – Rynant
    Jun 17, 2014 at 19:29
  • 5
    \$\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\$ Jun 18, 2014 at 12:01

51 Answers 51

33
\$\begingroup\$

Haskell - 43 31

43 characters without anything fancy.

f 0=0
f 8=8
f 2=5
f 5=2
f 6=9
f 9=6
f _= -1

Got it down to 31 characters by making it a partial function.

f=([0,-1,5,-1,-1,2,9,-1,8,6]!!)
\$\endgroup\$
3
  • 4
    \$\begingroup\$ I laughed. Have a +1. \$\endgroup\$
    – seequ
    Jun 17, 2014 at 20:03
  • 6
    \$\begingroup\$ +1 For using Haskell to do exactly what the spec. says! \$\endgroup\$ Jun 18, 2014 at 18:48
  • 1
    \$\begingroup\$ I don't think you need f= for the second solution as it is a valid expression \$\endgroup\$
    – Cyoce
    Sep 26, 2016 at 0:58
23
\$\begingroup\$

GolfScript, 15 14

I read the spec again and found that the input must be a string.

"0.5..29.86"\?

To run:

echo -n 2 | ruby golfscript.rb a.gs

Old version(which has integer input):

[0.5..2 9.8 6]?

The newer one(14 byte) is somewhat inspired by the CJam answer by aditsu.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Can't believe I didn't think of that... \$\endgroup\$
    – Dennis
    Jun 17, 2014 at 18:04
20
\$\begingroup\$

PowerShell - 27

'0 5  29 86'.indexof($args)
\$\endgroup\$
14
\$\begingroup\$

Python 2.x - 28

'015..29.86'.find(`input()`)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Python 2.x, specifically. \$\endgroup\$
    – seequ
    Jun 17, 2014 at 19:36
  • 6
    \$\begingroup\$ With Python 3, you could remove the `s and save 2 chars. \$\endgroup\$
    – Rynant
    Jun 17, 2014 at 19:39
11
\$\begingroup\$

JavaScript 37 36

alert("0_5__29_86".search(prompt()))
\$\endgroup\$
2
  • \$\begingroup\$ use .search() and save a byte. \$\endgroup\$ Jun 18, 2014 at 13:03
  • \$\begingroup\$ @IsmaelMiguel Nice one, Thanks! \$\endgroup\$
    – DarkAjax
    Jun 18, 2014 at 13:46
9
\$\begingroup\$

BEFUNGE 93 - 18 14 20 Bytes

I guess the commentators are right, though Befunge being a 2d language lines are kinda different. Still, in this instant, the commentators are right.

&1g01g-.  
! &  #* )'

Steps:

&

Reads input as a numerical value x, and pushes it on the stack.

1g

Gets the character value c (so, like '!' = 33, or '*' = 42. An empty cell = 32) at position x, 1.

01g-.

Reads the character value of cell 0,1 (33), subtracts it from c, and outputs it as a numerical value.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Quite nice. Have a +1. \$\endgroup\$
    – seequ
    Jun 17, 2014 at 18:47
  • \$\begingroup\$ Please correct the length: it's 20 bytes \$\endgroup\$
    – alephreish
    Jun 19, 2014 at 10:05
  • 1
    \$\begingroup\$ You've actually been counting your bytes wrong. You used 19 bytes. We count newlines and spaces. But if you switch to Befunge 98, you can save one; change 1st line to: &1g'!-. \$\endgroup\$
    – Justin
    Jun 19, 2014 at 17:39
8
\$\begingroup\$

CJam, 20 bytes

q25691347`"5296W"er~

Try it online.

Output

$ for i in {0..9}; { cjam <(echo 'q25691347`"5296W"er~') <<< $i; echo; }
0
-1
5
-1
-1
2
9
-1
8
6

How it works

q          " Read from STDIN. The leaves a string on the stack.            ";
25691347`  " Push the string '25691347'.                                   ";
"5296W"    " Push the string '5296W'.                                      ";
er         " Perform character transliteration.                            ";
~          " Evaluate the result. Examples: '2' returns 2, 'W' returns -1. ";
\$\endgroup\$
8
\$\begingroup\$

Java - 49

long f(int a){return(0x790a300601L>>(a*4)&15)-1;}

Here, 0x790a300601 is a value stuffed with the desired outputs, with one added to make them positive. The values are stored in nibbles within the value, so a bit of shifting and masking is required to pop them out.

Java - 100 (fun option)

int f(int a){Random r=new Random(0x2000A2F47D6Fl);int b=0;for(;a>=0;a--)b=r.nextInt(11)-1;
return b;}

Not the smallest option, but a bit of fun. I found a random seed that produces the correct values when called X times (where 0 >= X <= 9).

\$\endgroup\$
1
  • \$\begingroup\$ Clever. Have a +1 \$\endgroup\$
    – Cyoce
    Sep 23, 2016 at 18:39
8
\$\begingroup\$

JavaScript (ECMAScript 6) 25

x=>'1060039097'[x]-(x!=6)

JavaScript (ECMAScript 5) 43

function f(x){return'1060039097'[x]-(x!=6)}

UPDATE: edc65 has suggested a much better technique. toothbrush has suggested a much better language. At this point, my primary contributions are debugging and gumption.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ If you change it to ECMAScript 6 (supported in Firefox), you could simply do x=>[0,-1,5,-1,-1,2,9,-1,8,6][x]. \$\endgroup\$
    – Toothbrush
    Jun 17, 2014 at 20:17
  • \$\begingroup\$ At first, I nearly posted function(x)[0,-1,5,-1,-1,2,9,-1,8,6][x], also thanks to Firefox. I wasn't going to win anyway, so I decided I'd just stick with the highest-compatibility answer. If I start switching languages for brevity, then I'll eventually start defining my own language for every challenge I do. But I'll go ahead and mention the ECMAScript 6 version anyway, since you suggested it \$\endgroup\$
    – Keen
    Jun 17, 2014 at 21:02
  • 1
    \$\begingroup\$ Same concept but shorter (bye bye commas): x=>'106003907'[x]-(x!=6) \$\endgroup\$
    – edc65
    Jun 17, 2014 at 22:44
  • \$\begingroup\$ @edc65 You know, I'd wanted to use a string, and I had completely blanked on the fact that I could coerce the result back to a number. A bizarre lapse. Yet I still wouldn't have come up with -(x!=6). Thank you. \$\endgroup\$
    – Keen
    Jun 18, 2014 at 0:08
  • 1
6
\$\begingroup\$

bash 29

tr 1-9 x5xx29x86|sed s/x/-1/g

e.g.

$ echo 0 1 2 3 4 5 6 7 8 9 | tr 1-9 x5xx29x86|sed s/x/-1/g
0 -1 5 -1 -1 2 9 -1 8 6
\$\endgroup\$
2
  • \$\begingroup\$ You can omit the ' around the sed expression. Also I think you can omit the g because the spec only supplies one digit at a time \$\endgroup\$ Jun 17, 2014 at 18:55
  • \$\begingroup\$ Thanks. It's just in the example, the submission itself doesn't use '. Liking the g for longer input! \$\endgroup\$
    – user15259
    Jun 17, 2014 at 19:57
4
\$\begingroup\$

Kona - 29

This function returns the element x from the array 0 -1 5...

f:{0 -1 5 -1 -1 2 9 -1 8 6@x}

Examples:

> f 2
  5
> f 5
  2
> f 8
  8
\$\endgroup\$
3
  • \$\begingroup\$ Is a vector by itself really allowed? \$\endgroup\$
    – seequ
    Jun 17, 2014 at 19:17
  • \$\begingroup\$ @TheRare: Hmm, it does say "algorithm" so I suppose not. I'll change it and make it more like yours... \$\endgroup\$
    – Kyle Kanos
    Jun 17, 2014 at 19:43
  • \$\begingroup\$ Seems better, have a +1. \$\endgroup\$
    – seequ
    Jun 17, 2014 at 19:53
4
\$\begingroup\$

JavaScript 36 37 41

alert('0x'+'65b558f5ec'[prompt()]-6)

as ES6 function - 27:

f=v=>'0x'+'65b558f5ec'[v]-6
\$\endgroup\$
6
  • 2
    \$\begingroup\$ 28: f=v=>~-[1,,6,,,3,10,,9,7][v] \$\endgroup\$
    – nderscore
    Jun 17, 2014 at 20:29
  • \$\begingroup\$ @nderscore You just love improving people's codes, don't you? \$\endgroup\$
    – seequ
    Jun 17, 2014 at 20:56
  • 3
    \$\begingroup\$ @TheRare I'm just on a quest to find the shortest javascript code. :) If someone else has already posted a good answer, it makes more sense to me to find optimizations in it rather than post a new answer that's almost a duplicate. I'm not here to compete, just to cooperate towards achieving this goal. \$\endgroup\$
    – nderscore
    Jun 17, 2014 at 21:00
  • \$\begingroup\$ @nderscore I have the same mentality as long as my idea is similar enough. Anyways, nice one. \$\endgroup\$
    – seequ
    Jun 17, 2014 at 21:04
  • \$\begingroup\$ @nderscore You really gave me a motivation. I'm not sure if I can make it shorter, but I'll try :) \$\endgroup\$
    – core1024
    Jun 17, 2014 at 23:11
4
\$\begingroup\$

Sclipting, 11 characters

걄럣뉥밈결⓷方分결剩貶

Finally I have found a Windows computer with Visual Studio installed to build its interpreter. And it has defeated my GolfScript code easily.

It reads 18453063256\11\?/11%( in GolfScript.

\$\endgroup\$
5
  • 2
    \$\begingroup\$ Interesting, but your GolfScript answer still wins. Unless the question states otherwise, length is measured in bytes. \$\endgroup\$
    – Dennis
    Sep 14, 2014 at 4:17
  • \$\begingroup\$ @Dennis This was my 3rd or 4th answer on this site. I didn't know. And I think APL is an exception. \$\endgroup\$
    – jimmy23013
    Sep 14, 2014 at 8:43
  • \$\begingroup\$ @Dennis And most people don't like Sclipting. :) \$\endgroup\$
    – jimmy23013
    Sep 14, 2014 at 8:47
  • \$\begingroup\$ Not really an exception, we just let people choose their encoding. This answer would score 22 bytes, since that's its size using UTF-16. APL uses only 256 different characters, and there's an APL code page where once character is exactly one bytes. \$\endgroup\$
    – Dennis
    Sep 14, 2014 at 13:42
  • \$\begingroup\$ @Dennis Ah, you are right. \$\endgroup\$
    – jimmy23013
    Sep 14, 2014 at 13:56
4
+100
\$\begingroup\$

APL (Dyalog Extended), 22 bytes

⊢⌷{1-⍨⎕A⍳'BAGAADKAJH'}

Explanation:

⊢⌷{1-⍨⎕A⍳'BAGAADKAJH'}
      ⎕A⍳'BAGAADKAJH'     ⍝ numeric vector 2 1 7 1 1 4 11 1 10 8
   1-⍨                    ⍝ decrement:     1 0 6 0 0 3 10 0  9 7
⊢⌷                        ⍝ value at index ⍵ in the lut

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ there's an easy -1 byte here. \$\endgroup\$
    – Razetime
    Dec 22, 2020 at 9:35
  • \$\begingroup\$ commute, I see. \$\endgroup\$ Dec 22, 2020 at 9:35
  • 2
    \$\begingroup\$ 1-⍨⎕A⍳⌷∘'BAGAADKAJH' \$\endgroup\$
    – Adám
    Mar 23, 2021 at 5:28
3
\$\begingroup\$

J - 28 27 bytes

You know what I like? Simplicity (28 bytes). Note that in J, _1 is negative one (-1).

f=:{&0 _1 5 _1 _1 2 9 _1 8 6

Add a little complexity and you have 27 bytes.

f=:-{&0 2 _3 4 5 3 _3 8 0 3

Example:

   f 2
5
   f 6
9
   f 5
2
...
\$\endgroup\$
3
\$\begingroup\$

CJam - 14

Input/output version:

"0 5  29 86"q#

Stack version (assumes the number is on the stack):

[0W5WWY9W8 6]=

Try them at http://cjam.aditsu.net/

\$\endgroup\$
3
  • \$\begingroup\$ There's no Wikipedia article about CJAM, and the link just goes to a blank fiddler. Where would you find information about the language and its established releases? \$\endgroup\$ Jun 19, 2014 at 15:38
  • \$\begingroup\$ Looks like this is it: sourceforge.net/projects/cjam \$\endgroup\$ Jun 19, 2014 at 15:40
  • \$\begingroup\$ @Panzercrisis cjam.aditsu.net has "CJam" linked to the sourceforge page \$\endgroup\$ Jun 19, 2014 at 23:40
3
\$\begingroup\$

Perl, 27 26

Count includes the p flag

y/2569/5296/,s/[1347]/-1/

Usage:

$ echo 7 | perl -pe y/2569/5296/,s/[1347]/-1/
-1

Wait, did Perl just beat J? :)

\$\endgroup\$
10
  • \$\begingroup\$ +1 I was going to post y+0-9+9a4aa70a13+;$_=9-hex, which is the same length, but your is more original ;) \$\endgroup\$
    – core1024
    Jun 18, 2014 at 20:35
  • 1
    \$\begingroup\$ @core1024 : And it just got shorter ;) \$\endgroup\$
    – Zaid
    Jun 18, 2014 at 20:43
  • \$\begingroup\$ It didn't: echo 1 | perl -pe y/2569/5296/,s/[347]/-1/ \$\endgroup\$
    – core1024
    Jun 18, 2014 at 20:48
  • 1
    \$\begingroup\$ perl -pE "y/2569/5296/or$_=-1" :) \$\endgroup\$
    – alyx-brett
    Jun 9, 2015 at 11:42
  • 1
    \$\begingroup\$ Sure, could put the 0 in the 'y///' I guess? \$\endgroup\$
    – alyx-brett
    Jun 9, 2015 at 12:53
3
\$\begingroup\$

Marbelous 34 bytes

}0
=6=9=2=5=0=8?0
+3-3+3-3....--

It's not the shortest solution, but it's not the longest either.

How it works

}0 spawns a marble representing the first command line input. This marble drops down the next tick, onto the =6 cell. =6 is a comparison cell, it pushes any marble with value 6 down and all others to the right. This line-up of comparison cells pushes marbles right until they equal a desired value. 0 and 8 just fall through and get printed when tehy fall off the bottom of the board, 6 and 2, and 9 and 5 first get 3 added to them, subtracted from them respectively. If a marble doesn't equal any of the desired values, it lands on the ?0 cell, which turn any marble into a 0 marble1. This marble then gets decremented and falls off the board.

1 A ?n marble technically turns any marble into a marble between 0 and n. This has the nice side effect that ?0 turns anything into 0.

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

MATLAB - 35

I would wrap this in a function with n as the only parameter.

f=[0,-1,5,-1,-1,2,9,-1,8,6];
f(n+1)

35 characters.

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

ECMAScript 6, 24

f=x=>~-++'0n5nn29n86'[x]

Using normal JavaScript it would be 33:

alert(~-++'0n5nn29n86'[prompt()])
\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 24 22

-1+int(11fPart(11^Ans.0954191904

This encodes the possible outputs in a lookup table stored as a base-11 floating point number; the (N+1)th base-11 digit after the decimal point is extracted from the table to get the value of the inverted digit. In base 11 the number is .106003A097, and the digits of this less one are exactly 0,-1,5,-1,-1,2,9,-1,8,6.

edc65's trick of subtracting one in the case of 6 leads to this 24-byte solution, where ⑩^( is a single one-byte token:

-(Ans≠6)+int(10fPart(.1060039097⑩^(Ans

The string approach is 29 bytes:

-(Ans≠6)+expr(inString("1060039097",Ans+1,1

The array approach (which Ypnpyn also took) is 30 bytes, assuming the number is stored in X:

{1,0,6,0,0,3,10,0,9,7}-1:Ans(X+1

24 -> 22: Removed two extra digits of precision in the magic constant.

\$\endgroup\$
1
  • \$\begingroup\$ This can almost certainly be golfed to 19, and possibly to 18; however, I need to search for the correct numbers. \$\endgroup\$
    – lirtosiast
    Jun 16, 2015 at 5:47
2
\$\begingroup\$

C - 47 bytes [was 48 Bytes]

f(i){return(int[]){1,0,6,0,0,3,10,0,9,7}[i]-1;}

To add I/O (which other answers didn't always) for 86 bytes:

main(i){i=(int[]){1,0,6,0,0,3,10,0,9,7}[getchar()-48]-1;i<0?puts("-1"):putchar(i+48);}
\$\endgroup\$
1
2
\$\begingroup\$

05AB1E, 13 bytes

0®5®®Y9®8 6¹@

Try it online!

-3 thanks to Emigna.

Explanation:

0®5®®Y9®8 6¹@ Takes an integer from STDIN.
0®5®®Y9®8 6   Push 0, r=-1, 5, r=-1, r=-1, Y=2, 9, r=-1, 8, 6.
           ¹  Push first input item.
            @ Pop and push the 0-indexed stack item at the respective index.
\$\endgroup\$
4
  • \$\begingroup\$ 0®5®®Y9®8 6¹@ should work as well. \$\endgroup\$
    – Emigna
    Mar 31, 2017 at 6:48
  • \$\begingroup\$ @Emigna I had no idea, thanks! \$\endgroup\$ Mar 31, 2017 at 6:50
  • \$\begingroup\$ Good thinking with @. Not often you see that used :) \$\endgroup\$
    – Emigna
    Mar 31, 2017 at 6:56
  • \$\begingroup\$ @Emigna A byte shorter than )¹è or )sè. \$\endgroup\$ Mar 31, 2017 at 7:01
1
\$\begingroup\$

Python - 34

f=lambda n:ord("A@F@@CJ@IG"[n])-65
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 33 f=lambda n:ord(" "[n])-3 working with space codeskulptor.org/#user34_Q7NbNvQy55_0.py \$\endgroup\$ Jun 17, 2014 at 18:33
  • \$\begingroup\$ You might want to explain how and why this works \$\endgroup\$
    – Riot
    Jun 17, 2014 at 18:39
  • \$\begingroup\$ Using the ascii table asciitable.com the white spaces chosen are printable in python. it actually looks something like &#002;&#002;&#008;&#002;&#002;&#005;&#012;&#002;&#011;&#009; It's minus 3 for the reason that -1 leaves a null character which is no good, and minus 2 leaves a line feed which is reserved in python \$\endgroup\$ Jun 17, 2014 at 18:52
1
\$\begingroup\$

Java, 58 59

int f(int i){int[]a={1,0,6,0,0,3,10,0,9,7};return a[i]-1;}

OR

int f(int i){return new int[]{1,0,6,0,0,3,10,0,9,7}[i]-1;}
\$\endgroup\$
2
  • \$\begingroup\$ You can make your code a bit shorter if you add 1 to each value in array and after [i] substract 1. \$\endgroup\$
    – barteks2x
    Jun 17, 2014 at 18:43
  • \$\begingroup\$ @Barteks2x Good point; thanks \$\endgroup\$
    – Ypnypn
    Jun 17, 2014 at 19:18
1
\$\begingroup\$

JavaScript 42 37

Run it on the console of your browser

alert(~-[1,,6,,,3,10,,9,7][prompt()])
\$\endgroup\$
1
\$\begingroup\$

C - 117 108 106 77 76 bytes

a[]={1,0,6,0,0,3,10,0,9,7};main(int c,char**b){printf("%d",a[*b[1]-48]-1);}

Not the best language for golfing, but oh well...
Compile with gcc progname.c -o progname. (Ignore the warnings, or add -include stdio.h to the compile command.)

Usage: ./progname <number>

EDIT

As per @bebe's suggestion, here is an example that takes the input from STDIN instead:

C - 68 51 bytes

main(){printf("%d","106003:097"[getchar()-48]-49);}
\$\endgroup\$
6
  • \$\begingroup\$ using d=*b[1]-48 could be a good idea \$\endgroup\$
    – bebe
    Jun 17, 2014 at 20:08
  • \$\begingroup\$ @bebe Ah yes, thanks! \$\endgroup\$
    – BenjiWiebe
    Jun 17, 2014 at 20:46
  • 1
    \$\begingroup\$ main(){printf("%d",(int[]){1,0,6,0,0,3,10,0,9,7}[getchar()-48]-1);} sorry for bothering you this much but i find this a bit shorter. \$\endgroup\$
    – bebe
    Jun 17, 2014 at 20:53
  • \$\begingroup\$ You can save another character by making the array global so you don't need the cast. a[]={1,0,6,0,0,3,10,0,9,7};main(){printf("%d",a[getchar()-48]-1);} \$\endgroup\$
    – Allbeert
    Jun 18, 2014 at 14:18
  • 2
    \$\begingroup\$ main(){printf("%d","106003:097"[getchar()-48]-49);} 51 bytes \$\endgroup\$
    – bebe
    Jun 18, 2014 at 21:25
1
\$\begingroup\$

J (24, function)

(the input panel isn't playing nice. Paste the following in a python interpreter and my answer shall be revealed:)

print "f=:{&(_3+3 u:'^C^B^B^E^L^B^K\t')"
\$\endgroup\$
1
\$\begingroup\$

Clojure - 36

#(case % 2 5 5 2 6 9 9 6 0 0 8 8 -1)
\$\endgroup\$
1
\$\begingroup\$

Perl, 22B

perl -pe "y/25690/52960/or$_=-1"

Based off https://codegolf.stackexchange.com/a/32012/19039, but shorter. 1B penalty for -p.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.