21
\$\begingroup\$

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.enter image description here


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!

\$\endgroup\$
  • \$\begingroup\$ Could you provide testcases? \$\endgroup\$ – Kritixi Lithos Feb 24 '17 at 11:05
  • 1
    \$\begingroup\$ @KritixiLithos Certaimly! I'll edit tjat in now@ \$\endgroup\$ – Albert Renshaw Feb 24 '17 at 11:06
  • \$\begingroup\$ Did you mean ] -> [ ? \$\endgroup\$ – shooqie Feb 24 '17 at 11:22
  • 3
    \$\begingroup\$ related \$\endgroup\$ – Rod Feb 24 '17 at 11:28
  • 3
    \$\begingroup\$ 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!) \$\endgroup\$ – Doddy Feb 24 '17 at 16:59

16 Answers 16

7
\$\begingroup\$

Ruby, 76 71 69 bytes

->a{"`1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./"[/.#{a}/][0]}
\$\endgroup\$
5
\$\begingroup\$

Perl 6, 87 83 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.)

\$\endgroup\$
  • 1
    \$\begingroup\$ Maybe if we had $" the ranges could save a few chars \$\endgroup\$ – Ven Feb 24 '17 at 12:20
5
\$\begingroup\$

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.
\$\endgroup\$
  • 3
    \$\begingroup\$ 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 \$\endgroup\$ – Albert Renshaw Feb 24 '17 at 18:30
  • 1
    \$\begingroup\$ I didn't have the time yet, but I'll add an explanation asap. \$\endgroup\$ – Dennis Feb 24 '17 at 18:43
  • 1
    \$\begingroup\$ Done. I also golfed it a bit. \$\endgroup\$ – Dennis Feb 24 '17 at 19:13
  • 1
    \$\begingroup\$ Øq nifty! I like this, thanks! \$\endgroup\$ – Albert Renshaw Feb 24 '17 at 19:16
4
\$\begingroup\$

Python 3, 85 78 bytes:

lambda x,k="`1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm<>?":k[k.‌​find(x)-1]
\$\endgroup\$
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Rod Feb 24 '17 at 14:15
  • \$\begingroup\$ Good point, I'll add that. \$\endgroup\$ – L3viathan Feb 24 '17 at 14:16
  • \$\begingroup\$ I think you need a backtick to be to the left of 1. \$\endgroup\$ – xnor Feb 24 '17 at 15:17
  • \$\begingroup\$ @xnor Right, fixed that. \$\endgroup\$ – L3viathan Feb 24 '17 at 15:32
4
\$\begingroup\$

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__9`12345678_LM-<>_\\VXSWDFGUHJKNBIO=EARYCQZT\nP][___"*4)
\$\endgroup\$
4
\$\begingroup\$

Retina, 53 51 bytes

T`1-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.

\$\endgroup\$
3
\$\begingroup\$

C++, 109 bytes

void f(char i){std::string k="`1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm,./";std::cout<<k[k.find(i)-1];}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

V, 57 54 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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – nmjcman101 Feb 24 '17 at 20:53
  • \$\begingroup\$ @nmjcman101 Ah yes, I must have kept the G since when the keyboard used to be multiline. Thanks! \$\endgroup\$ – Kritixi Lithos Feb 25 '17 at 6:03
2
\$\begingroup\$

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.

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

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!

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

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
\$\endgroup\$
  • 1
    \$\begingroup\$ 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"). \$\endgroup\$ – Olivier Grégoire Aug 18 '17 at 10:30
2
\$\begingroup\$

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`")
\$\endgroup\$
  • \$\begingroup\$ @KevinCruijssen Thanks for spotting that I'd accidentally shifted those three keys. I've unshifted them now. \$\endgroup\$ – Neil Aug 18 '17 at 7:52
1
\$\begingroup\$

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

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

Pyth - 56 bytes

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

Test Suite.

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