18
1
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 code-golf challenge, the shortest code wins!
Maybe your example should be
6
and9
because5
could be flipped horizontally or vertically to become2
– Cyoce – 2016-09-24T01:06:39.5701I've gone and edited the specification to match the answers. Hopefully this challenge can be reopened now. – None – 2017-03-30T17:40:06.453
You can submit your own answer to your question, so feel free to add it. – Kyle Kanos – 2014-06-17T17:27:30.620
Okay, I will do that. Thanks – James Williams – 2014-06-17T17:31:00.963
17I 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. – Jwosty – 2014-06-17T17:40:41.870
3You should specify I/O. Do you want a function, a program or anything specific? – Dennis – 2014-06-17T18:03:52.230
why wouldn't 8 and 0 return -1? – Mhmd – 2014-06-17T18:08:00.787
29I 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. – kernigh – 2014-06-17T18:41:23.080
@kernigh I think it's meant to be a reflection across a horizontal axis. – Keen – 2014-06-17T18:53:06.550
2@Cory But the question states that 9 and 6 should flip to 6 and 9. That is not a reflection across the horizontal axis. – Rynant – 2014-06-17T19:09:31.590
66, 9 rotated 180 deg, 2, 5 flipped horizontally, and 1, 3 in fact are reflections of themselves across the vertical axis. – jimmy23013 – 2014-06-17T19:24:22.223
22The translations defined in the question are not consistent at all. Why do 2 and 5 flip, but 3 doesn't? – Rynant – 2014-06-17T19:29:32.020
@Rynant Yeah,
1
,3
,6
, and9
really throw my definition off (foolish positive bias!). I'll pay more attention from here on out. Fortunately, everything but4
is explicitly spelled out in the challenge. So the rule is consistent, but with an extra layer of complexity. – Keen – 2014-06-17T20:07:26.1134I 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? – Jack Aidley – 2014-06-18T12:01:43.610
4@JackAidley: I guess since the problem is so trivial and, in fact, tailored to
IndexOf
-type methods on strings I doubt this will lead to significant savings. – Joey – 2014-06-18T18:15:47.3071Agree. 6 rotated 180 degrees becomes 9 but 2 and 5 rotated 180 degrees become itself again. This should be restated as flipped or rotated version of a number, and 1 should be included in – phuclv – 2014-06-19T04:06:15.307
3 should be flipped to E – Beta Decay – 2014-09-09T09:16:11.900