25
\$\begingroup\$

When Alice was touch typing on her QWERTY keyboard (Figure 1), she accidentally shifted both of her hands rightwards by one key, so q became w, w became e, etc. (p became [). Spaces were not affected because the space bar was quite big.

Your task is to help her fix her message using the shortest number of bytes, i.e. undo the shifting of her hand positions. More precisely, you will be given a string consisting of spaces as well as characters from wertyuiop[sdfghjkl;xcvbnm, and you need to map the characters to qwertyuiopasdfghjklzxcvbnm and leave spaces untouched.

Here are some testcases for you to test your program:

input      output
----------------------
s[[;r      apple
s gom      a fin
werty      qwert
uiop[      yuiop
sdfgh      asdfg
jkl;       hjkl
xcvb       zxcv
nm,        bnm
 ;p;;o[p[   lollipop
[2 spaces] [2 spaces]

(the lollipop testcase starts with a space)

Figure 1: Alice's QWERTY keyboard

This is . Shortest answer in bytes wins.

\$\endgroup\$
4
  • 12
    \$\begingroup\$ Welcome back to CGCC! \$\endgroup\$
    – user
    Jul 1, 2021 at 21:36
  • 2
    \$\begingroup\$ Related \$\endgroup\$
    – Arnauld
    Jul 1, 2021 at 22:44
  • 7
    \$\begingroup\$ Why fix it? Bob always understands her text. :) \$\endgroup\$
    – tsh
    Jul 2, 2021 at 6:09
  • \$\begingroup\$ Good challenge! I already enjoy the idea of it. \$\endgroup\$
    – AJFaraday
    Jul 2, 2021 at 8:05

23 Answers 23

12
\$\begingroup\$

Jelly, 15 bytes

ØqḊ€ż“[;,”F,Fyɠ

Try it online!

Takes input via STDIN

How it works

ØqḊ€ż“[;,”F,Fyɠ - Main link. Takes no arguments
Øq              - Set the return value to ['qwertyuiop', 'asdfghjkl', 'zxcvbnm']
  Ḋ€            - Dequeue each; ['wertyuiop', 'sdfghjkl', 'xcvbnm']
    ż“[;,”      - Zip with "[;,"; [['wertyuiop', '['], ['sdfghjkl', ';'], ['xcvbnm', ',']]
          F     - Flatten; "wertyuiop[sdfghjkl;xcvbnm,"
            F   - Flatten the return value; "qwertyuiopasdfghjklzxcvbnm"
           ,    - Pair; ['wertyuiop[sdfghjkl;xcvbnm,', 'qwertyuiopasdfghjklzxcvbnm']
              ɠ - Read a line from STDIN
             y  - Translate. Map w -> q, e -> w, etc.
\$\endgroup\$
10
\$\begingroup\$

J, 43 bytes

'qwertyuiop[asdfghjkl;zxcvbnm,  '(<:@i:{[)]

Try it online!

  • 'qwertyuiop[asdfghjkl;zxcvbnm, ' The full keyboard, including the shifted away left-most chars and the extra chars [;,, as well as two extra spaces at the end.
  • <:@i: Find the index of the input within that, searching from the right, and substract one.
  • {[ Pull those shifted indexes from the string in step 1.
\$\endgroup\$
10
\$\begingroup\$

C++ (Windows MSVC), 92 bytes

#include<Windows.h>
void f(PCH s){for(;*s;)*s++=MapVirtualKey(OemKeyScan(*s)-(*s>32),1)|32;}

Take input as a char* (PCH is typedef of char*), output by modify the string in-place.

Trying to make use of some built-ins. But seems not quite short. Maybe some one may take this idea and make a shorter one in other languages.

OemKeyScan convert key to its scancode, 'q' -> 16, 'w' -> 17, 'e' -> 18, ..., 'p' -> 25, 'a' -> 30, ..., 'l' -> 38, 'z' -> 44, ..., 'm' -> 50. MapVirtualKey convert scancode back to (uppercase) character.

Saved 1 byte by ErikF.

\$\endgroup\$
2
  • \$\begingroup\$ 87 bytes by removing the void and moving the increment into the assignment operator (works in a .c file, don't actually try it online): link \$\endgroup\$
    – ErikF
    Jul 3, 2021 at 2:33
  • 1
    \$\begingroup\$ @ErikF remove void would work if I change the language to C but not C++. \$\endgroup\$
    – tsh
    Jul 3, 2021 at 2:50
8
\$\begingroup\$

Vyxal, 16 13 12 bytes

k•`[;,`Y∑:ǔĿ

Try it Online!

k•           # Qwerty keyboard (array of rows)
  `[;,`Y     # Interleave with `[;,`
        ∑    # Concatenate all of that
         :ǔĿ # Shift each character 1 right relative to this in the input.
\$\endgroup\$
7
\$\begingroup\$

Python 3, 61 bytes

lambda s:s.translate('p?ml ??'*14+'vxswdfguhjknbio?earycqzt')

Try it online!

Explanation

A naive implementation with str.translate would use the following translation table:

???????????????????????????????? ???????????m??????????????l???????????????????????????????p??????vxswdfguhjknbio?earycqzt

To optimize for size, notice that the characters ' mlp' all have different positions, modulo 7. As such, we can replace the first 98 characters of the table with 'p?ml ??'*14. The rest of the table is the same.

\$\endgroup\$
2
  • \$\begingroup\$ Can you please explain this code? How does translate works without maketrans? \$\endgroup\$ Jul 9, 2021 at 1:17
  • \$\begingroup\$ @DianaAndrei If given a string as an argument, translate will replace the nth ASCII character with the character at the nth position of the string. I believe it uses __getitem__ internally, which explains this behavior. \$\endgroup\$ Jul 9, 2021 at 3:15
6
\$\begingroup\$

Bash, 34 bytes

tr snvfrghjokl\;,mp[wtdyibecux a-z

Try it online! Using bash because I don't know how to score this as a tr answer.

\$\endgroup\$
6
\$\begingroup\$

Haskell, 65 bytes

a#(c:d)|a==c=d!!0|1>0=a#d
map(#",mnbvcxz;lkjhgfdsa[poiuytrewq  ")

Try it online!

(#) takes a character and a string and returns the character after the first match in the string. Our implementation then just maps this across the string with ,mnbvcxz;lkjhgfdsa[poiuytrewq as the preset string.

\$\endgroup\$
5
\$\begingroup\$

C# (Visual C# Interactive Compiler), 64 bytes

x=>x.Select(t=>" mlpvxswdfguhjknbio?earycqzt"[t%94%88%57%43%32])

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ What is the logic behind? Could you explain steps/way how you figure out the index modulo numbers ? \$\endgroup\$ Jul 4, 2021 at 2:37
5
\$\begingroup\$

JavaScript (ES6), 66 bytes

I/O format: array of characters.

a=>a.map(c=>(S="  ,mnbvcxz;lkjhgfdsa[poiuytrewq")[S.indexOf(c)+1])

Try it online!


JavaScript (ES6), 57 bytes

If taking an array of ASCII codes as input and returning an array of characters is acceptable, we can do:

b=>b.map(c=>"rjbwgtvacnofh_spzleykidumx_q "[c*19%116%30])

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Could you explain steps/way how you figure out index modulo numbers? And the logic behind? \$\endgroup\$ Jul 4, 2021 at 2:38
4
\$\begingroup\$

Ruby -p, 40+1 bytes

$_.tr!"snvfrghjokl;,mp[wtdyibecux","a-z"

Try it online!

\$\endgroup\$
4
\$\begingroup\$

C (gcc), 79 bytes

I combined the source and destination strings so that I can take the character after the match. By inverting the list of characters, I could avoid -1 indexes and having to special-case spaces.

f(char*s){for(;*s;putchar(strchr(",mnbvcxz;lkjhgfdsa[poiuytrewq  ",*s++)[1]));}

Try it online!

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

Retina 0.8.2, 36 bytes

T`snvfrg\hj\ok\l;,m\p[\wt\dyibecux`l

Try it online! Link includes test cases. Explanation: Simply a transliteration, although Retina's built-in classes need to be escaped on the source string (obviously the built-in class l is desired on the target string).

Alternative solution, also 36 bytes:

T`\wertyuio-p[as\df-hj-l;zxcvbnm,`qo

Try it online! Link includes test cases. Transliterates the letters in keyboard order. This costs bytes because a and z have to be included in the source list and q in the destination list but then bytes can be saved by using character ranges to avoid quoting the o, p, h and l character classes.

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

Japt -m, 29 bytes

;D+S ·¬v gD·mÅí"[;,"¬'+ ¬v bU

Try it

; -> use second set of predef. Vars
-m flag -> map input by the program  (U)->

     g   - take from 
D+S·¬v     [ QWERTY +' ' split on (\n),rejoined, to lowercase ]
Dxxx bU  - at index of input in[WERTY..]

D·   - QWERTY split on \n
mÅ   - remove 1st char on each
í    - pair with :
"[;,"¬  - ..splitted
'+   - and join
¬v   - join all and to lowercase
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 36 bytes

≔”⧴3�⁷Rrηs⟲θ]⁺L÷↗¹T“[~↓TφF”η⭆S§η⊕⌕ηι

Try it online! Link is to verbose version of code. Explanation: Port of @ErikF's C answer, except putting the spaces at the beginning because that compresses better.

≔”⧴3�⁷Rrηs⟲θ]⁺L÷↗¹T“[~↓TφF”η

Assign the compressed string ,mnbvcxz;lkjhgfdsa[poiuytrewq to a variable.

⭆S§η⊕⌕ηι

Find each input character in the string and print the character that follows it.

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

Perl 5, 57 bytes

s/./",mnbvcxz;lkjhgfdsa[poiuytrewq  "=~m|\Q$&\E(.)|;$1/ge

Try it online!

s/.                                  #replace one char at a time from input
 /
   ",mnbvcxz;lkjhgfdsa[poiuytrewq  " #...with its next char in this string
     =~ m| \Q$&\E (.)|x;             #found by searching for current char in $&
                                     #which must be escaped by \Q and \E
                                     #since [ has a special meaning in regexes.
   $1                                #Return next char captured into $1 by (.) 
 /gex                                #g = global search: all chars
                                     #e = treat replacement string as eval code
                                     #x = allow white-space, improve readability
\$\endgroup\$
2
\$\begingroup\$

R, 68 55 bytes

function(x)chartr("snvfrghjokl;,mp[wtdyibecux","a-z",x)

Try it online!

Abusing laconic Ranges are supported in the specifications in chartr documentation (forced to work by try and error).

Inspired by @Neil's answer.

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

Haskell, 59 bytes

mapM(`lookup`(zip<*>tail)",mnbvcxz;lkjhgfdsa[poiuytrewq  ")

Try it online!

A function String -> Maybe String; it maps "s gom" to Just "a fin".

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

Common Lisp, 102 bytes

(defun f(i &aux(s",mnbvcxz;lkjhgfdsa[poiuytrewq  "))(map'string(lambda(c)(elt s(1+(position c s))))i))

Try it online!

Test cases included in TIO.

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

Excel, 93 bytes

=CONCAT(IFERROR(CHAR(FIND(MID(A1,SEQUENCE(LEN(A1)),1),"snvfrghjokl;,mp[wtdyibecux")+96)," "))

Link to Spreadsheet

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

PHP, 80 bytes

str_replace($a=str_split('wertyuiop[asdfghjkl;yxcvbnm,'),[-1=>'q']+$a,$argv[1]);

Avoids string repetition and double str_split().

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 186 114 Bytes

$W=str_split(",mnbvcxz;lkjhgfdsa[poiuytrewq");for(;$c=$argv[1][$p++];)echo($c!==" ")?$W[array_flip($W)[$c]+1]:" ";

Try it online! Definitely not the shortest answer on here, but I'm not sure how I could handle this in PHP without using a big array. Any input would be greatly appreciated!

-72 bytes by changing a large keyed array into one small one. Just iterates over every character, find it in the array, then prints the next element in the array.

\$\endgroup\$
1
  • \$\begingroup\$ 102 Bytes Not sure why you went with iterating when str_replace does the job very well \$\endgroup\$
    – Tofandel
    Jul 3, 2021 at 18:05
1
\$\begingroup\$

Pyth, 45 35 bytes

.rQ"[poiuytrewq;lkjhgfdsa,mnbvcxz  

Try it online!

sm?gJtxK.",zcVÖzùwÔ\"Ã?4‰ØÝ”gR¹³3"dZ@KJ\ Q

Try it online!

*Edit: Improved solution is rather boring...
"[poiuytrewq;lkjhgfdsa,mnbvcxz is a string of every possible char followed by the char it's meant to be (i.e. ytrewq) with two spaces on the end (space is meant to be space);

This is all surrounded with .r which will translate each element of Q(Input) into the next element.

Previous solution
Haven't golfed in a while so this feels pretttty rusty...

Maps through qwerty string, shifting char to the left unless it's a space.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 13 bytes

žV€¦…[;,øSžW‡

Try it online.

Explanation:

žV            # Push builtin ["qwertyuiop","asdfghjkl","zxcvbnm"]
  €¦          # Remove the first character from each: ["wertyuiop","sdfghjkl","xcvbnm"]
    …[;,ø     # Zip/transpose with string "[;,":
              #  [["wertyuiop","["],["sdfghjkl",";"],["xcvbnm",","]]
         S    # Convert it to a flattened list of characters: 
              #  ["w","e","r","t","y","u","i","o","p","[","s","d","f","g","h","j","k",
              #   "l",";","x","c","v","b","n","m",","]
          žW  # Push builtin "qwertyuiopasdfghjklzxcvbnm"
            ‡ # Transliterate all characters in the (implicit) input
              # (after which the result is output implicitly)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.