Return the flipped version of a number

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:

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!

James Williams

Posted 2014-06-17T17:25:33.430

Reputation: 1 735

Maybe your example should be 6 and 9 because 5 could be flipped horizontally or vertically to become 2 – Cyoce – 2016-09-24T01:06:39.570

1I'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, and 9 really throw my definition off (foolish positive bias!). I'll pay more attention from here on out. Fortunately, everything but 4 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.113

4I 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.307

1Agree. 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

Answers

32

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]!!)

Taylor Fausak

Posted 2014-06-17T17:25:33.430

Reputation: 761

1I don't think you need f= for the second solution as it is a valid expression – Cyoce – 2016-09-26T00:58:25.697

4I laughed. Have a +1. – seequ – 2014-06-17T20:03:59.503

6+1 For using Haskell to do *exactly* what the spec. says! – recursion.ninja – 2014-06-18T18:48:01.003

23

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.

jimmy23013

Posted 2014-06-17T17:25:33.430

Reputation: 34 042

1Can't believe I didn't think of that... – Dennis – 2014-06-17T18:04:48.077

19

PowerShell - 27

'0 5  29 86'.indexof($args)

Rynant

Posted 2014-06-17T17:25:33.430

Reputation: 2 353

14

Python 2.x - 28

'015..29.86'.find(`input()`)

Willem

Posted 2014-06-17T17:25:33.430

Reputation: 1 528

2Python 2.x, specifically. – seequ – 2014-06-17T19:36:36.820

6With Python 3, you could remove the ```s and save 2 chars. – Rynant – 2014-06-17T19:39:24.977

11

JavaScript 37 36

alert("0_5__29_86".search(prompt()))

DarkAjax

Posted 2014-06-17T17:25:33.430

Reputation: 669

use .search() and save a byte. – Ismael Miguel – 2014-06-18T13:03:08.693

@IsmaelMiguel Nice one, Thanks! – DarkAjax – 2014-06-18T13:46:38.820

8

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. ";

Dennis

Posted 2014-06-17T17:25:33.430

Reputation: 196 637

8

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.

AndoDaan

Posted 2014-06-17T17:25:33.430

Reputation: 2 232

2Quite nice. Have a +1. – seequ – 2014-06-17T18:47:57.830

Please correct the length: it's 20 bytes – har-wradim – 2014-06-19T10:05:06.327

1You'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'!-. – Justin – 2014-06-19T17:39:58.507

8

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.

Keen

Posted 2014-06-17T17:25:33.430

Reputation: 261

@Cyoce Good point. – Keen – 2016-09-25T17:27:11.480

1If 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]. – Toothbrush – 2014-06-17T20:17:24.673

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 – Keen – 2014-06-17T21:02:34.750

1Same concept but shorter (bye bye commas): x=>'106003907'[x]-(x!=6) – edc65 – 2014-06-17T22:44:44.387

@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. – Keen – 2014-06-18T00:08:05.513

8

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

Duncan Jones

Posted 2014-06-17T17:25:33.430

Reputation: 181

Clever. Have a +1 – Cyoce – 2016-09-23T18:39:58.283

6

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

user15259

Posted 2014-06-17T17:25:33.430

Reputation:

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 – Digital Trauma – 2014-06-17T18:55:37.667

Thanks. It's just in the example, the submission itself doesn't use '. Liking the g for longer input! – None – 2014-06-17T19:57:53.157

4

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

Kyle Kanos

Posted 2014-06-17T17:25:33.430

Reputation: 4 270

Is a vector by itself really allowed? – seequ – 2014-06-17T19:17:16.620

@TheRare: Hmm, it does say "algorithm" so I suppose not. I'll change it and make it more like yours... – Kyle Kanos – 2014-06-17T19:43:54.070

Seems better, have a +1. – seequ – 2014-06-17T19:53:45.867

4

JavaScript 36 37 41

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

as ES6 function - 27:

f=v=>'0x'+'65b558f5ec'[v]-6

core1024

Posted 2014-06-17T17:25:33.430

Reputation: 1 811

228: f=v=>~-[1,,6,,,3,10,,9,7][v] – nderscore – 2014-06-17T20:29:03.057

@nderscore You just love improving people's codes, don't you? – seequ – 2014-06-17T20:56:57.453

3@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. – nderscore – 2014-06-17T21:00:49.567

@nderscore I have the same mentality as long as my idea is similar enough. Anyways, nice one. – seequ – 2014-06-17T21:04:47.173

@nderscore You really gave me a motivation. I'm not sure if I can make it shorter, but I'll try :) – core1024 – 2014-06-17T23:11:16.293

@core1024 clever method! – nderscore – 2014-06-18T13:06:36.203

4

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.

jimmy23013

Posted 2014-06-17T17:25:33.430

Reputation: 34 042

2Interesting, but your GolfScript answer still wins. Unless the question states otherwise, length is measured in bytes. – Dennis – 2014-09-14T04:17:00.533

@Dennis This was my 3rd or 4th answer on this site. I didn't know. And I think APL is an exception. – jimmy23013 – 2014-09-14T08:43:15.800

@Dennis And most people don't like Sclipting. :) – jimmy23013 – 2014-09-14T08:47:24.840

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. – Dennis – 2014-09-14T13:42:05.527

@Dennis Ah, you are right. – jimmy23013 – 2014-09-14T13:56:53.827

3

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

seequ

Posted 2014-06-17T17:25:33.430

Reputation: 1 714

3

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/

aditsu quit because SE is EVIL

Posted 2014-06-17T17:25:33.430

Reputation: 22 326

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? – Panzercrisis – 2014-06-19T15:38:37.220

Looks like this is it: http://sourceforge.net/projects/cjam/

– Panzercrisis – 2014-06-19T15:40:50.610

@Panzercrisis http://cjam.aditsu.net/ has "CJam" linked to the sourceforge page

– aditsu quit because SE is EVIL – 2014-06-19T23:40:23.333

3

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

Zaid

Posted 2014-06-17T17:25:33.430

Reputation: 1 015

perl -pE "y/2569/5296/or$_=-1" :) – alexander-brett – 2015-06-09T11:42:42.160

@alexander-brett : won't work for 0 – Zaid – 2015-06-09T12:47:08.880

Sure, could put the 0 in the 'y///' I guess? – alexander-brett – 2015-06-09T12:53:27.277

+1 I was going to post y+0-9+9a4aa70a13+;$_=9-hex, which is the same length, but your is more original ;) – core1024 – 2014-06-18T20:35:14.363

1@core1024 : And it just got shorter ;) – Zaid – 2014-06-18T20:43:09.693

It didn't: echo 1 | perl -pe y/2569/5296/,s/[347]/-1/ – core1024 – 2014-06-18T20:48:44.690

@core1024 : I misread the specs, should be fixed now. It's still shorter than the J solution. – Zaid – 2014-06-18T21:05:40.540

There really is no concise way to do this in J, as values are strictly typed and numbers can't match strings. – seequ – 2014-06-18T21:10:59.953

@TheRare : I don't think it'll get much shorter in Perl than this either. – Zaid – 2014-06-18T21:14:00.787

I actually clocked around triple the characters doing it in this style. Have a +1 for the nice idea. – seequ – 2014-06-18T21:20:15.647

3

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.

overactor

Posted 2014-06-17T17:25:33.430

Reputation: 3 500

2

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.

lukass

Posted 2014-06-17T17:25:33.430

Reputation: 71

2

ECMAScript 6, 24

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

Using normal JavaScript it would be 33:

alert(~-++'0n5nn29n86'[prompt()])

ShadowCat7

Posted 2014-06-17T17:25:33.430

Reputation: 121

2

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.

lirtosiast

Posted 2014-06-17T17:25:33.430

Reputation: 20 331

This can almost certainly be golfed to 19, and possibly to 18; however, I need to search for the correct numbers. – lirtosiast – 2015-06-16T05:47:21.953

2

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);}

NoSeatbelts

Posted 2014-06-17T17:25:33.430

Reputation: 89

1

Wow, I had no clue inline array literals like this were a thing. Very nice first answer, and welcome to PPCG! (There is no need to add I/O function submissions are perfectly valid.)

– Martin Ender – 2016-09-23T19:17:55.917

1

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.

Erik the Outgolfer

Posted 2014-06-17T17:25:33.430

Reputation: 38 134

ịV = žh‡ in 05AB1E – Magic Octopus Urn – 2017-03-31T17:58:29.937

1

05AB1E, 13 bytes (non-competing)

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.

Erik the Outgolfer

Posted 2014-06-17T17:25:33.430

Reputation: 38 134

0®5®®Y9®8 6¹@ should work as well. – Emigna – 2017-03-31T06:48:24.633

@Emigna I had no idea, thanks! – Erik the Outgolfer – 2017-03-31T06:50:40.993

Good thinking with @. Not often you see that used :) – Emigna – 2017-03-31T06:56:09.973

@Emigna A byte shorter than )¹è or )sè. – Erik the Outgolfer – 2017-03-31T07:01:23.680

1

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!

ʰᵈˑ

Posted 2014-06-17T17:25:33.430

Reputation: 1 426

Hey, thanks! Even better, should I post? – ʰᵈˑ – 2017-03-31T14:58:43.083

1

Python - 34

f=lambda n:ord("A@F@@CJ@IG"[n])-65

user19214

Posted 2014-06-17T17:25:33.430

Reputation:

1

33 f=lambda n:ord(" "[n])-3 working with space http://www.codeskulptor.org/#user34_Q7NbNvQy55_0.py

– Dylan Madisetti – 2014-06-17T18:33:51.750

You might want to explain how and why this works – Riot – 2014-06-17T18:39:59.360

Using the ascii table http://www.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

– Dylan Madisetti – 2014-06-17T18:52:40.563

1

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;}

Ypnypn

Posted 2014-06-17T17:25:33.430

Reputation: 10 485

You can make your code a bit shorter if you add 1 to each value in array and after [i] substract 1. – barteks2x – 2014-06-17T18:43:24.337

@Barteks2x Good point; thanks – Ypnypn – 2014-06-17T19:18:59.347

1

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);}

BenjiWiebe

Posted 2014-06-17T17:25:33.430

Reputation: 231

using d=*b[1]-48 could be a good idea – bebe – 2014-06-17T20:08:50.207

@bebe Ah yes, thanks! – BenjiWiebe – 2014-06-17T20:46:03.540

1main(){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. – bebe – 2014-06-17T20:53:54.257

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);} – Allbeert – 2014-06-18T14:18:17.273

2main(){printf("%d","106003:097"[getchar()-48]-49);} 51 bytes – bebe – 2014-06-18T21:25:17.877

@bebe Wow, you are a lot better golfer than me!! – BenjiWiebe – 2014-06-19T02:33:37.940

1

JavaScript 42 37

Run it on the console of your browser

alert(~-[1,,6,,,3,10,,9,7][prompt()])

William Barbosa

Posted 2014-06-17T17:25:33.430

Reputation: 3 269

1

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')"

ɐɔıʇǝɥʇuʎs

Posted 2014-06-17T17:25:33.430

Reputation: 4 449

1

Clojure - 36

#(case % 2 5 5 2 6 9 9 6 0 0 8 8 -1)

coredump

Posted 2014-06-17T17:25:33.430

Reputation: 6 292

0

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

James Williams

Posted 2014-06-17T17:25:33.430

Reputation: 1 735

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" – Pinna_be – 2014-06-17T21:42:20.940

Also, a small change would be to put the if x==9 last and say x>8 instead, saving a character. – Pinna_be – 2014-06-17T21:49:36.083

1@Pinna_be: You’re forgetting the return. – Ry- – 2014-06-18T05:09:57.467

0

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]

Danstahr

Posted 2014-06-17T17:25:33.430

Reputation: 101

why not altogether f=(lambda x:[-1,-1,5,-1,-1,2,9,-1,8,6][x])(x) – Dylan Madisetti – 2014-06-17T18:24:42.340

143 def f(x):print[-1,-1,5,-1,-1,2,9,-1,8,6][x] – Dylan Madisetti – 2014-06-17T18:56:02.500

0

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;}

Ypnypn

Posted 2014-06-17T17:25:33.430

Reputation: 10 485

i>8:-1 will return -1 for input 9 when it should return 6, won't it? – corsiKa – 2014-06-17T21:52:07.343

@corsiKa Indeed. – Ypnypn – 2014-06-17T23:47:30.490

0

PowerShell 32

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

DarkAjax

Posted 2014-06-17T17:25:33.430

Reputation: 669

0

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

Jwosty

Posted 2014-06-17T17:25:33.430

Reputation: 3 530

Fails for 0 and 8 – Dancrumb – 2014-06-17T22:30:35.670

I missed that in the rules, thanks! – Jwosty – 2014-06-17T22:31:19.993

0

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

Ypnypn

Posted 2014-06-17T17:25:33.430

Reputation: 10 485

30 bytes: {1,0,6,0,0,3,10,0,9,7}-1:Ans(X+1 – lirtosiast – 2015-06-08T01:50:19.000

0

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;}

bacchusbeale

Posted 2014-06-17T17:25:33.430

Reputation: 1 235

Which compiler are you using? I can't find a gcc version that accepts that without #include <iostream> and std::. – Christoph – 2017-03-31T07:24:23.263

Doesn't C++ inherit the abuse of integers for Booleans of C? – Peter Taylor – 2014-06-18T09:58:38.217

@PeterTaylor Yes C++ accepts integers, but I don't want 'a' modified until the end. – bacchusbeale – 2014-06-18T11:10:08.217

1No need to modify a. I was thinking that (a==X)?Y:Z can be (a-X)?Z:Y to save a few bytes. – Peter Taylor – 2014-06-18T14:13:48.457

0

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

rici

Posted 2014-06-17T17:25:33.430

Reputation: 601

0

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

idiot.py

Posted 2014-06-17T17:25:33.430

Reputation: 161

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. – seequ – 2014-06-19T16:58:21.127

0

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 (:[])).

YawarRaza7349

Posted 2014-06-17T17:25:33.430

Reputation: 71

0

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.

Οurous

Posted 2014-06-17T17:25:33.430

Reputation: 7 916

0

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

Sylwester

Posted 2014-06-17T17:25:33.430

Reputation: 3 678

0

Ruby, 30 bytes

What, no Ruby solution yet?

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

Takes the input as a command-line argument.

Martin Ender

Posted 2014-06-17T17:25:33.430

Reputation: 184 808

0

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.

kirbyfan64sos

Posted 2014-06-17T17:25:33.430

Reputation: 8 730

@ThomasKwa Fixed. – kirbyfan64sos – 2015-06-09T00:14:15.933

0

Perl, 22B

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

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

alexander-brett

Posted 2014-06-17T17:25:33.430

Reputation: 1 485

0

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

MCMastery

Posted 2014-06-17T17:25:33.430

Reputation: 783

0

C, 46 bytes

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

ceilingcat

Posted 2014-06-17T17:25:33.430

Reputation: 5 503

0

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

Destructible Lemon

Posted 2014-06-17T17:25:33.430

Reputation: 5 908