| Bytes | Lang | Time | Link |
|---|---|---|---|
| 3945 | Uiua | 240710T041119Z | Omnikar |
| nan | sed has a command specifically for this that I rarely get to use | 240709T212138Z | guest430 |
| 138 | Go | 240709T185634Z | bigyihsu |
| 013 | Jelly | 210701T212900Z | caird co |
| 013 | 05AB1E | 210818T083713Z | Kevin Cr |
| 012 | Vyxal | 210702T044805Z | emanresu |
| 035 | Pyth | 210705T033516Z | Scott |
| 080 | PHP | 210704T172524Z | Adam P. |
| 093 | Excel | 210703T143601Z | Axuary |
| 102 | Common Lisp | 210703T094636Z | Thun_A |
| 065 | Haskell | 210702T124032Z | Wheat Wi |
| 092 | C++ Windows MSVC | 210702T025647Z | tsh |
| 079 | C gcc | 210702T051404Z | ErikF |
| 114 | PHP | 210702T161443Z | SlamJamm |
| 059 | Haskell | 210702T185943Z | lynn |
| 055 | R | 210702T140602Z | pajonk |
| 057 | Perl 5 | 210702T120320Z | Kjetil S |
| 036 | Charcoal | 210702T094757Z | Neil |
| 066 | JavaScript ES6 | 210701T225237Z | Arnauld |
| 401 | Ruby p | 210702T061424Z | G B |
| 029 | Japt m | 210702T024152Z | AZTECCO |
| 064 | C# Visual C# Interactive Compiler | 210702T011619Z | Gymhgy |
| 036 | Retina 0.8.2 | 210701T232438Z | Neil |
| 034 | Bash | 210701T232651Z | Neil |
| 061 | Python 3 | 210701T232058Z | dingledo |
| 043 | J | 210701T220107Z | Jonah |
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.
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
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.
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
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)
Vyxal, 16 13 12 bytes
k•`[;,`Y∑:ǔĿ
k• # Qwerty keyboard (array of rows)
`[;,`Y # Interleave with `[;,`
∑ # Concatenate all of that
:ǔĿ # Shift each character 1 right relative to this in the input.
Pyth, 45 35 bytes
.rQ"[poiuytrewq;lkjhgfdsa,mnbvcxz
sm?gJtxK.",zcVÖzùwÔ\"Ã?4ØÝgR¹³3"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.
PHP, 80 bytes
str_replace($a=str_split('wertyuiop[asdfghjkl;yxcvbnm,'),[-1=>'q']+$a,$argv[1]);
Avoids string repetition and double str_split().
Excel, 93 bytes
=CONCAT(IFERROR(CHAR(FIND(MID(A1,SEQUENCE(LEN(A1)),1),"snvfrghjokl;,mp[wtdyibecux")+96)," "))
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.
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++ (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.
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]));}
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.
Haskell, 59 bytes
mapM(`lookup`(zip<*>tail)",mnbvcxz;lkjhgfdsa[poiuytrewq ")
A function String -> Maybe String; it maps "s gom" to Just "a fin".
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.
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
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.
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])
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
C# (Visual C# Interactive Compiler), 64 bytes
x=>x.Select(t=>" mlpvxswdfguhjknbio?earycqzt"[t%94%88%57%43%32])
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.
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.
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.
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.