Fix my Fat Fingers

21

3

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!

Albert Renshaw

Posted 2017-02-24T11:02:32.980

Reputation: 2 955

Could you provide testcases? – user41805 – 2017-02-24T11:05:37.290

1@KritixiLithos Certaimly! I'll edit tjat in now@ – Albert Renshaw – 2017-02-24T11:06:35.357

Did you mean ] -> [ ? – shooqie – 2017-02-24T11:22:03.990

@shooqie fixrd! – Albert Renshaw – 2017-02-24T11:22:28.710

Do we need to provide functionality for backspaces? – Okx – 2017-02-24T11:25:27.767

@Okx No thats npt a green key@ (in my imsge) – Albert Renshaw – 2017-02-24T11:26:13.260

3related – Rod – 2017-02-24T11:28:42.513

3You 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 – 2017-02-24T16:59:30.317

@Doddy ah nice to know. I was wondering why the Enter key was cut in half. – Andrea Lazzarotto – 2017-02-24T19:19:00.077

I can see no green keys on the image, only black keys overlaid with green circles. – John Dvorak – 2017-02-25T14:36:31.690

@Doddy On my work keyboard the |\ key is used twice for some reason.. Left of the Z as you said, but another one between the '" and ENTER ↵, which I usually use. Didn't even knew one was right of the Z before.. xD – Kevin Cruijssen – 2017-08-18T07:16:47.837

Answers

7

Ruby, 76 71 69 bytes

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

G B

Posted 2017-02-24T11:02:32.980

Reputation: 11 099

5

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

smls

Posted 2017-02-24T11:02:32.980

Reputation: 4 352

1Maybe if we had $" the ranges could save a few chars – Ven – 2017-02-24T12:20:21.220

5

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.

Dennis

Posted 2017-02-24T11:02:32.980

Reputation: 196 637

3I 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 – 2017-02-24T18:30:07.823

1I didn't have the time yet, but I'll add an explanation asap. – Dennis – 2017-02-24T18:43:18.887

1Done. I also golfed it a bit. – Dennis – 2017-02-24T19:13:02.413

1Øq nifty! I like this, thanks! – Albert Renshaw – 2017-02-24T19:16:07.887

4

Python 3, 85 78 bytes:

lambda x,k="`1234567890-=qwertyuiop[]\\asdfghjkl;'\nzxcvbnm<>?":k[k.‌​find(x)-1]

L3viathan

Posted 2017-02-24T11:02:32.980

Reputation: 3 151

1You 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 – 2017-02-24T14:15:05.130

Good point, I'll add that. – L3viathan – 2017-02-24T14:16:03.760

I think you need a backtick to be to the left of 1. – xnor – 2017-02-24T15:17:16.863

@xnor Right, fixed that. – L3viathan – 2017-02-24T15:32:47.540

4

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.

Leo

Posted 2017-02-24T11:02:32.980

Reputation: 8 482

4

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)

xnor

Posted 2017-02-24T11:02:32.980

Reputation: 115 687

3

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

user41805

Posted 2017-02-24T11:02:32.980

Reputation: 16 320

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 – 2017-02-24T20:53:01.143

@nmjcman101 Ah yes, I must have kept the G since when the keyboard used to be multiline. Thanks! – user41805 – 2017-02-25T06:03:53.577

3

C++, 109 bytes

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

Try it online!

Divcy

Posted 2017-02-24T11:02:32.980

Reputation: 501

3

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

Timtech

Posted 2017-02-24T11:02:32.980

Reputation: 12 038

2

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!

Oliver

Posted 2017-02-24T11:02:32.980

Reputation: 7 160

This one is cool, can you add an explanation? – Albert Renshaw – 2017-02-24T18:28:58.060

1@AlbertRenshaw Added an explanation. – Oliver – 2017-02-24T19:00:34.243

2

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.

Tor

Posted 2017-02-24T11:02:32.980

Reputation: 201

2

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

Neil

Posted 2017-02-24T11:02:32.980

Reputation: 95 035

@KevinCruijssen Thanks for spotting that I'd accidentally shifted those three keys. I've unshifted them now. – Neil – 2017-08-18T07:52:09.650

2

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

Kevin Cruijssen

Posted 2017-02-24T11:02:32.980

Reputation: 67 575

1I 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 – 2017-08-18T10:30:13.023

1

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
`
\
]

seshoumara

Posted 2017-02-24T11:02:32.980

Reputation: 2 878

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

Okx

Posted 2017-02-24T11:02:32.980

Reputation: 15 025

1

Pyth - 56 bytes

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

Test Suite.

Maltysen

Posted 2017-02-24T11:02:32.980

Reputation: 25 023