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 code-golf. Shortest answer in bytes wins.
-
12\$\begingroup\$ Welcome back to CGCC! \$\endgroup\$user– user2021年07月01日 21:36:16 +00:00Commented Jul 1, 2021 at 21:36
-
2\$\begingroup\$ Related \$\endgroup\$Arnauld– Arnauld2021年07月01日 22:44:58 +00:00Commented Jul 1, 2021 at 22:44
-
7\$\begingroup\$ Why fix it? Bob always understands her text. :) \$\endgroup\$tsh– tsh2021年07月02日 06:09:03 +00:00Commented Jul 2, 2021 at 6:09
-
\$\begingroup\$ Good challenge! I already enjoy the idea of it. \$\endgroup\$AJFaraday– AJFaraday2021年07月02日 08:05:21 +00:00Commented Jul 2, 2021 at 8:05
26 Answers 26
Jelly, (削除) 15 (削除ここまで) 13 bytes
Øqż"[;,"FUƝFy
-1 byte directly from emanresu A, and minus a second byte from a golf of that suggestion.
How it works
This exploits the leading constant chain structure in Jelly, wherein if a chain begins with a constant (Øq in this case), and is followed exclusively by monads and dyad-nilad pairs, the chain simply operates on the leading constant, ignoring any provided arguments, until this pattern is broken, saving us from having to use any grouping quicks while we construct the translation mapping.
Øqż"[;,"FUƝFy - Main link. Takes the string S on the left
Øqż"[;,"FUƝF - Leading constant chain:
Øq - Constant: "qwertyuiop"asdfghjkl"zxcvbnm"
ż"[;," - Dyad-nilad: Zip with "[;,"
F - Monad: Flatten; "qwertyuiop[asdfghjkl;zxcvbnm,"
UƝ - Monad:
Ɲ - Over overlapping pairs:
U - Reverse
F - Monad: Flatten; "wqewretrytuyiuoipo[pa[sadsfdgfhgjhkjlk;lz;xzcxvcbvnbmn,m"
y - With the transliteration string on the left and S on the right, transliterate S according to the string
The way Jelly's transliterate atom, y, works when provided a flat string on the left is to break it up into adjacent pairs of characters, then replace the first character with the second in the target string on the right. So "wqewretrytuyiuoipo[pa[sadsfdgfhgjhkjlk;lz;xzcxvcbvnbmn,m"y transliterates
w -> q
e -> w
r -> e
...
, -> m
-
\$\begingroup\$ 14 bytes with a different use of
y. There's probably a 13 or even 12 in there somewhere. \$\endgroup\$emanresu A– emanresu A2024年07月09日 08:04:47 +00:00Commented Jul 9, 2024 at 8:04 -
\$\begingroup\$ @emanresuA Indeed, there is! \$\endgroup\$2024年07月09日 16:20:44 +00:00Commented Jul 9, 2024 at 16:20
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.
-
-
1\$\begingroup\$ @ErikF remove
voidwould work if I change the language to C but not C++. \$\endgroup\$tsh– tsh2021年07月03日 02:50:54 +00:00Commented Jul 3, 2021 at 2:50 -
\$\begingroup\$ Is there an online environment where I can run this? \$\endgroup\$sergiol– sergiol2025年05月15日 22:49:54 +00:00Commented May 15 at 22:49
J, 43 bytes
'qwertyuiop[asdfghjkl;zxcvbnm, '(<:@i:{[)]
'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.
Vyxal, (削除) 16 13 (削除ここまで) 12 bytes
k•`[;,`Y∑:ǔL·
k• # Qwerty keyboard (array of rows)
`[;,`Y # Interleave with `[;,`
∑ # Concatenate all of that
:ǔL· # Shift each character 1 right relative to this in the input.
Python 3, 61 bytes
lambda s:s.translate('p?ml ??'*14+'vxswdfguhjknbio?earycqzt')
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.
-
\$\begingroup\$ Can you please explain this code? How does translate works without maketrans? \$\endgroup\$Diana Andrei– Diana Andrei2021年07月09日 01:17:22 +00:00Commented 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\$dingledooper– dingledooper2021年07月09日 03:15:00 +00:00Commented Jul 9, 2021 at 3:15
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.
Haskell, 65 bytes
a#(c:d)|a==c=d!!0|1>0=a#d
map(#",mnbvcxz;lkjhgfdsa[poiuytrewq ")
(#) 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.
C# (Visual C# Interactive Compiler), 64 bytes
x=>x.Select(t=>" mlpvxswdfguhjknbio?earycqzt"[t%94%88%57%43%32])
-
\$\begingroup\$ What is the logic behind? Could you explain steps/way how you figure out the index modulo numbers ? \$\endgroup\$Soner from The Ottoman Empire– Soner from The Ottoman Empire2021年07月04日 02:37:06 +00:00Commented Jul 4, 2021 at 2:37
JavaScript (ES6), 66 bytes
I/O format: array of characters.
a=>a.map(c=>(S=" ,mnbvcxz;lkjhgfdsa[poiuytrewq")[S.indexOf(c)+1])
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])
-
1\$\begingroup\$ Could you explain steps/way how you figure out index modulo numbers? And the logic behind? \$\endgroup\$Soner from The Ottoman Empire– Soner from The Ottoman Empire2021年07月04日 02:38:58 +00:00Commented Jul 4, 2021 at 2:38
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]));}
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.
Japt -m, 29 bytes
;D+S ·¬v gD·mÅí"[;,"¬'+ ¬v bU
; -> 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
Common Lisp, 102 bytes
(defun f(i &aux(s",mnbvcxz;lkjhgfdsa[poiuytrewq "))(map'string(lambda(c)(elt s(1+(position c s))))i))
Test cases included in TIO.
Excel, 93 bytes
=CONCAT(IFERROR(CHAR(FIND(MID(A1,SEQUENCE(LEN(A1)),1),"snvfrghjokl;,mp[wtdyibecux")+96)," "))
PHP, 80 bytes
str_replace($a=str_split('wertyuiop[asdfghjkl;yxcvbnm,'),[-1=>'q']+$a,$argv[1]);
Avoids string repetition and double str_split().
Charcoal, 36 bytes
≔"⧴3�7Rrηs⟲θ]+L÷↗1T"[〜↓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�7Rrηs⟲θ]+L÷↗1T"[〜↓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.
Perl 5, 57 bytes
s/./",mnbvcxz;lkjhgfdsa[poiuytrewq "=~m|\Q$&\E(.)|;1ドル/ge
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
R, (削除) 68 (削除ここまで) 55 bytes
function(x)chartr("snvfrghjokl;,mp[wtdyibecux","a-z",x)
Abusing laconic Ranges are supported in the specifications in chartr documentation (forced to work by try and error).
Inspired by @Neil's answer.
Pyth, (削除) 45 (削除ここまで) 35 bytes
.rQ"[poiuytrewq;lkjhgfdsa,mnbvcxz
sm?gJtxK.",zcVÖzùwÔ\"Ã?4ØÝgR133"dZ@KJ\ Q
(削除ここまで)*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.
05AB1E, 13 bytes
žV€¦...[;,øSžW‡
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)
sed has a command specifically for this that I rarely get to use, so:
sed, 56 bytes
y/wertyuiop[sdfghjkl;xcvbnm,/qwertyuiopasdfghjklzxcvbnm/
the 'y' command maps letters matching the first string to their position in the second
Uiua, 39 characters/45 bytes
⊏+1⊸⊗:" ,mnbvcxz;lkjhgfdsa[poiuytrewq"
" ,mnbvcxz;lkjhgfdsa[poiuytrewq" is the entire keyboard, plus the shifted characters, plus two spaces, written in reverse (explained later). This string is pushed to the stack first, then:
: # Flip the top two stack values, placing the keyboard string under the input
⊸⊗ # Replace characters in the input with their indices in the keyboard string,
# but keep the keyboard string underneath on the stack
+1 # Add 1 to all the indices, shifting left in the keyboard string
⊏ # Select all the indices in the resulting array from the keyboard string
⊗ searches the haystack string from the left, which means that spaces will get replaced with the index of the first space, meaning the index must be +1'd to shift it to the second space. For this reason, the rest of the keyboard string is written in reverse so that +1 acts as a leftward shift.
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.
Go, 138 bytes
import."strings"
func f(s string)(o string){M:=`,mnbvcxz;lkjhgfdsa[poiuytrewq `
for _,r:=range s{i:=IndexRune(M,r)+1
o+=M[i:i+1]}
return}
Go, 142 bytes
import."strings"
func f(s string)string{M:=`,mnbvcxz;lkjhgfdsa[poiuytrewq `
return Map(func(r rune)rune{return rune(M[IndexRune(M,r)+1])},s)}
Both solutions use the same keyboard mapping found by other users. Amazing how the solution using the manual loop is only 4 bytes shorter than using the stdlib approach.