# Code Golf Challenge

I have an isdue, my fingrrs are fat and I freqintly jave an isdue of ty[ing one keystrpke to the right on my kryboard.

I'm afraid the isdue is getyng worse anf worsr as time goes on.

Sopn every keystrpke I make wil; be shiftrd pne to the right!

Befpre then I'd like a program (or functipn) to autp shift every keystrpke back to the left one.

I'll make sure to take my tome typong the rest of tjis chal;enge so as not to cause anu confudion!

# Objective:

Write a program or function that takes an input of one of the following green keys on a standard QWERTY keyboard and returns the character of the key one to the left of it.

# Conditions:

•You may assume that the person running this program is using a QWERTY keyboard like the one pictured above.

•Input and Output are both case-insensitive, you may use any case (or a mixture of case combinations) for this challenge and you can also assume all input will be in one case if desired.

•If your language has no way of allowing a user to input the return key for some reason you may ignore that one keystroke for this

•This challenge is just for the default values of the keys, for example if the 4 key is pressed you can assume it will always be 4 and never $ •You can assume only the green keys will ever be pressed. # Example Input -> Output: S -> a 4 -> 3 = -> - [ -> p This is , so the program with the shortest bytecount wins! • Could you provide testcases? – user41805 Feb 24 '17 at 11:05 • @KritixiLithos Certaimly! I'll edit tjat in now@ – Albert Renshaw Feb 24 '17 at 11:06 • Did you mean ] -> [ ? – shooqie Feb 24 '17 at 11:22 • related – Rod Feb 24 '17 at 11:28 • You might not care, but here's an interesting piece of trivia: this is an ANSI keyboard layout. Note the backslash above the Enter key; in contrast to an ISO keyboard, in which the backslash button is to the left of Z. (It's also American, but that's easier to identify!) – Doddy Feb 24 '17 at 16:59 ## 16 Answers ## Ruby, 76 71 69 bytes ->a{"1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./"[/.#{a}/][0]}  # Perl 6, 8783 69 bytes {[Q"1234567890-=qwertyuiop[]\asdfghjkl;' zxcvbnm,./".comb].&{%(.[1..*]Z=>$_)}{$_}}  {~Q"1234567890-=qwertyuiop[]\asdfghjkl;' zxcvbnm,./".match(/.)>$_/)}


Try it online!

Wondering if there's a way to encode that hard-coded string to something shorter...

(Stole G B's regex idea for -14 bytes.)

• Maybe if we had $" the ranges could save a few chars – Ven Feb 24 '17 at 12:20 # Jelly, 34 33 bytes ØD”1¦ṭØqż“[]\“;'¶“,./“0-=”Fṡ2UZy  Try it online! ### How it works ØD”1¦ṭØqż“[]\“;'¶“,./“0-=”Fṡ2UZy Main link. Argument: s (string) ØD Digits; yield "0123456789". ”1| Replace '0' with a backtick. Øq Qwerty; yield ["qwertyuiop", "asdfghjkl", "zxcvbnm"]. ṭ Tack; add "123456789" as the last element of the qwerty array. “[]\“;'¶“,./“0-=” Yield ["[]\\", ";'\n", "0-="]. ż Zip; combine the strings of the array to the left with the corresponding strings of the array to the right, yielding an array of string pairs. F Flatten, yielding a string. ṡ2 Obtain all overlapping pairs of characters. U Upend; reverse each pair. Z Zip, yielding a pair of strings. y Translate the input s according to the generated replacement table.  • I don't know Jelly, can you add an explanation of your code? I'm curious how you got away without typing a string literal of the whole keyboard – Albert Renshaw Feb 24 '17 at 18:30 • I didn't have the time yet, but I'll add an explanation asap. – Dennis Feb 24 '17 at 18:43 • Done. I also golfed it a bit. – Dennis Feb 24 '17 at 19:13 • Øq nifty! I like this, thanks! – Albert Renshaw Feb 24 '17 at 19:16 # Python 3, 85 78 bytes: lambda x,k="1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm<>?":k[k.‌​find(x)-1]  • You can pass the string as optional parameter, using 1 lambda : lambda x,k="1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm<>?":k[k.find(x)-1] to reduce few bytes – Rod Feb 24 '17 at 14:15 • Good point, I'll add that. – L3viathan Feb 24 '17 at 14:16 • I think you need a backtick to be to the left of 1. – xnor Feb 24 '17 at 15:17 • @xnor Right, fixed that. – L3viathan Feb 24 '17 at 15:32 # Python, 76 bytes s="1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm<>?" dict(zip(s,''+s)).get  Try it online! Makes a dictionary that takes each key to the one to its left by zipping the character string with its shifted version. The bottom line is the function, the top one is a definition. Using translate to create a mapping gave a longer solution. Try it online! lambda s:s.translate("';"*22+"_0__912345678_LM-<>_\\VXSWDFGUHJKNBIO=EARYCQZT\nP][___"*4)  # Retina, 53 51 bytes T1-90\-=QW\ERTYUI\OP[]\\ASDF-HJ-L;'¶ZXCVBNM,./\o  Try it online! A simple transliteration shifting every character 1 position backwards. Everything from 1 to / is the original character set, while the following part is the new set, using o to indicate the Other set. H and L are special character classes for transliteration in retina (respectively mapping to Hex digits and uppercase Letters), but fortunately they occur on the keyboard inside alfabetically ordered sequences (FGH and JKL), so we can avoid escaping them by putting them in ranges and gain like that 2 bytes. # C++, 109 bytes void f(char i){std::string k="1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./";std::cout<<k[k.find(i)-1];}  Try it online! # TI-Basic, 70 bytes I doubt that it could get any shorter than this... Input Str1 "1234567890-=QWERTYUIOP[]\ASDFGHJKL;'ZXCVBNM,./ sub(Ans,inString(Ans,Str1)-1,1  P.S. The two-byte tokens are Str1, , \, sub(, and inString(. # V, 5754 51 bytes 3 bytes saved thanks to @nmjcman101 for using hxVp instead of what I had for the multiline keyboard i¬190-=qwertyuiop[]\asdfghjkl;'zxcvbnm,./<esc>/<C-r>a hxVp  Try it online! <esc> is 0x1b and <c-r> is 0x12 Note: this doesn't not support the enter key Contains unprintables, so here's the hexdump 00000000: 6960 ac31 3930 2d3d 7177 6572 7479 7569 i.190-=qwertyui 00000010: 6f70 5b5d 5c61 7364 6667 686a 6b6c 3b27 op[]\asdfghjkl;' 00000020: 7a78 6376 626e 6d2c 2e2f 1b2f 1261 0a68 zxcvbnm,././.a.h 00000030: 7856 70 xVp  ### Explanation Most of the program generates the keyboard. i enters insert mode and every character following it is printed to the buffer. But there is a small quirk here, ¬19 inserts characters between 1 and 9. The program exits insert mode at the <esc>. And then here /<c-r>a it searches for the argument in the buffer. This brings the cursor on top of the character it found. h " move the cursor to the left x " delete this character Vp " select this line and replace it with the deleted character  • I'm not 100%, but I think instead of hylHVGp you can do what @DJMcMayhem did in the motorcycle question like hxVp. I'm not sure why you put the G in there, isn't it all one line? Also dhVp would work. – nmjcman101 Feb 24 '17 at 20:53 • @nmjcman101 Ah yes, I must have kept the G since when the keyboard used to be multiline. Thanks! – user41805 Feb 25 '17 at 6:03 # PowerShell, 82 bytes $k="1234567890-=qwertyuiop[]\asdfghjkl;'
zxcvbnm,./";$k[$k.IndexOf((read-host))-1]


The Enter key is supported, but cannot be tested with Read-Host because the act of hitting enter with no value returns nothing in PowerShell.

# Japt, 56 42 bytes

;D=Dv ·q i"[]\\",A i";'",22 +",./")Dg1nDbU


### Explanation

;D=Dv ·q i"[]\\",A i";'",22 +",./")Dg1nDbU

;D=D                                        // Shortcut for QWERTY (with newlines and spaces), assigning to variable D
v                                       // Setting D to lowercase
·q                                    // Joining D into an array with no spaces or newlines
i"[]\\",A                          // Inserting "[]\" into index 10 (A)
i";'",22                 // Inserting ";'" into index 22
+",./"           // Appending ",./"
Dg        // Returns the character at index:
1n      //    -1+
DbU  //     Index of U (the input)


Try it online!

• This one is cool, can you add an explanation? – Albert Renshaw Feb 24 '17 at 18:28
• @AlbertRenshaw Added an explanation. – Oliver Feb 24 '17 at 19:00

# Java 8, 99 bytes

c->{String r="1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./";return r.charAt(r.indexOf(c)-1);}


Explanation:

Try it here.

c->{                // Method with character as both parameter and return-type
String r="1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./";
//  Literal String of the qwerty keyboard layout of the challenge
return r.charAt(  //  Return the character at the following index:
r.indexOf(c)    //   The index of the input character
-1);            //   -1 to shift it to the left
}                   // End of method

• I give up. I was trying to do it with regex, but I miserably fail with some special characters... c->"1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./".replaceAll("(?s).*(.)"+c+".*","$1"). – Olivier Grégoire Aug 18 '17 at 10:30 ## JavaScript (ES6), 74 bytes c=>(s=".,mnbvcxz\n';lkjhgfdsa\\][poiuytrewq=-0987654321")[s.indexOf(c)+1]  Since / isn't in my string, indexOf returns -1, which when incremented causes . to be output. 93 bytes to process a string: s=>s.replace(/./g,c=>s[s.indexOf(c)+1],s="><mnbvcxz\n';lkjhgfdsa\\][poiuytrewq=-0987654321")  • @KevinCruijssen Thanks for spotting that I'd accidentally shifted those three keys. I've unshifted them now. – Neil Aug 18 '17 at 7:52 ## GNU sed, 72 + 1(r flag) = 73 bytes s:$:1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./:
s:(.).*(.)\1.*:\2:


The return key can't be tested, because sed by design splits the input using \n as the delimiter and then runs the script as many times as there are lines.

Test run: continuous input-output pair (when done press Ctrl + D or Ctrl + C)

me@LCARS:/PPCG\$ sed -rf shift_key.sed
s
a
4
3
=
-
a
\
1

\
]


# 05AB1E, 50 bytes

'žhÀ"-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./"JDÀ‡


Try it online!

Explanation:

'                                                  # 1 char string:
žh                                                # Push numbers 0123456789
À                                               # Rotated 1 left (123456890)
"-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./"      # Push string literal
J     # Join all elements pushed to the stack to one string
D    # Duplicate
À   # Rotate 1 left
‡  # Transliterate: a.get(b.indexOf(input))


# Pyth - 56 bytes

@K"1234567890-=qwertyuiop[]\\asdfghjkl;'
zxcvbnm,./"txK
`