| Bytes | Lang | Time | Link |
|---|---|---|---|
| 149 | C | 180205T162025Z | Steadybo |
| 101 | Ruby | 180207T005700Z | benj2240 |
| 158 | Java 8 | 180205T144327Z | Kevin Cr |
| 099 | JavaScript ES6 | 180205T162352Z | Arnauld |
| 056 | Perl | 180205T214802Z | Ton Hosp |
| 099 | Haskell | 180206T033741Z | Roman Cz |
| 119 | Clean | 180206T015755Z | Οurous |
| 152 | Red | 180205T163823Z | Galen Iv |
| 020 | Japt | 180205T180012Z | Shaggy |
| 085 | Perl 5 | 180205T170342Z | Dom Hast |
| nan | Perl 5 | 180205T164558Z | Xcali |
| 013 | Jelly | 180205T140133Z | Dennis |
| 110 | Python 2 | 180205T144331Z | Rod |
| 049 | Retina | 180205T135643Z | Neil |
| 018 | Jelly | 180205T135059Z | Erik the |
C, 152 149 bytes
Thanks to @gastropner for saving three bytes!
j,l;f(S,n){for(char*s=S,*k;*s;++s)for(k="1234567890\0QWERTYUIOP\0ASDFGHJKL\0ZXCVBNM\0";l=strlen(k);k+=l+1)for(j=l;j--;)k[j]-*s||putchar(k[(j+n)%l]);}
Unrolled:
j,l;
f(S,n)
{
for (char*s=S, *k; *s; ++s)
for (k="1234567890\0QWERTYUIOP\0ASDFGHJKL\0ZXCVBNM\0"; l=strlen(k); k+=l+1)
for (j=l; j--;)
k[j]-*s || putchar(k[(j+n)%l]);
}
Ruby, 101 bytes
->s,n{n.times{s.tr! '1234567890QWERTYUIOPASDFGHJKLZXCVBNM','2345678901WERTYUIOPQSDFGHJKLAXCVBNMZ'};s}
I'm honestly a little disappointed that I couldn't do better with 'cleverer' methods. The closest I got was along the lines of
a=%w{1234567890 QWERTYUIOP ASDFGHJKL ZXCVBNM}
b=a.map{|r|r[1..-1]<<r[0]}*''
a*=''
n.times{s.tr! a,b}
for a net gain of 7 characters.
Java 8, 159 158 bytes
n->s->{for(int i=s.length,j;i-->0;)for(String x:"1234567890;QWERTYUIOP;ASDFGHJKL;ZXCVBNM".split(";"))if((j=x.indexOf(s[i])+n)>=n)s[i]=x.charAt(j%x.length());}
-1 byte thanks to @OlivierGrégoire modifying the input-array instead of printing directly.
Explanation:
n->s->{ // Method with integer and character-array parameters, and no return-type
for(int i=s.length,j;i-->0;)
// Loop over the input character-array with index
for(String x:"1234567890;QWERTYUIOP;ASDFGHJKL;ZXCVBNM".split(";"))
// Inner loop over the qwerty-lines
if((j=x.indexOf(s[i])+n)>=n)
// If the current qwerty-line contains the character
// Set `j` to the index of this character on that line + input `n`
s[i]=x.charAt(j%x.length());}
// Replace the character at index `i`
// with the new character (at index `j` modulo length_of_qwerty_line)
JavaScript (ES6), 101 99 bytes
Takes input in currying syntax (s)(n). Works with arrays of characters.
s=>n=>s.map(c=>(S='1QAZ2WSX3EDC4RFV5TGB6YHN7UJM8IK_9OL_0P')[(p=S.search(c)+n*4)%(-~'9986'[p%4]*4)])
Test cases
let f =
s=>n=>s.map(c=>(S='1QAZ2WSX3EDC4RFV5TGB6YHN7UJM8IK_9OL_0P')[(p=S.search(c)+n*4)%(-~'9986'[p%4]*4)])
console.log(JSON.stringify(f([..."0PLM"])(1))) // 1QAZ
console.log(JSON.stringify(f([..."ZXCVB"])(2))) // CVBNM
console.log(JSON.stringify(f([..."HELLO"])(3))) // LYDDW
console.log(JSON.stringify(f([..."0PLM"])(11))) // 1QSV
console.log(JSON.stringify(f([..."0PLM"])(2130))) // 0PHX
How?
We look for the position p of each character of the input within a string S where the keyboard rows are interleaved: the first 4 characters are '1QAZ' (first column of the keyboard), the next 4 characters are '2WSX' (second column of the keyboard) and so on. Unused positions are padded with underscores and the last ones are simply discarded.
col # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
------+------+------+------+------+------+------+------+------+------+---
row # | 0123 | 0123 | 0123 | 0123 | 0123 | 0123 | 0123 | 0123 | 0123 | 01
------+------+------+------+------+------+------+------+------+------+---
char. | 1QAZ | 2WSX | 3EDC | 4RFV | 5TGB | 6YHN | 7UJM | 8IK_ | 9OL_ | 0P
This allows us to easily identify the row with p mod 4 and eliminates the need for explicit separators between the rows.
We advance by 4n positions, apply the correct modulo for this row (40, 40, 36 and 28 respectively) and pick the replacement character found at this new position in S.
Perl, 59 58 57 56 bytes
Includes + for -p
Give input on STDIN as 2 lines, first the string, then the repeat
(echo 0PLM; echo 2130) | perl -pe '$a="OPQWERTYUILASDF-MZXCVBNM0-90";eval"y/HI$a/J$a/;"x<>'
Haskell, 99 bytes
f(s,n)=[dropWhile(/=c)(cycle r)!!n|c<-s,r<-words"1234567890 QWERTYUIOP ASDFGHJKL ZXCVBNM",elem c r]
Clean, 144 119 bytes
import StdEnv
\n s=[l.[(i+n)rem(size l)]\\c<-s,l<-["1234567890","QWERTYUIOP","ASDFGHJKL","ZXCVBNM"],i<-[0..]&j<-:l|j==c]
Lambda function with the signature Int ![Char] -> [Char]
Red, 152 bytes
f: func[s n][foreach c s[foreach[t l]["1234567890"10"QWERTYUIOP"10"ASDFGHJKL"9"ZXCVBNM"7][if p: find t c[if(i:(index? p)+ n // l)= 0[i: l]prin t/(i)]]]]
Ungolfed:
f: func [s n][1
foreach c s [
foreach [t l] ["1234567890"10"QWERTYUIOP"10"ASDFGHJKL"9"ZXCVBNM"7][
p: find t c
if p [
i: (index? p) + n // l
if i = 0 [i: l]
prin t/(i) ]]]]
Japt, 20 bytes
Running out the door to dinner so more golfing and an explanation to follow.
;£=D·i9òs)æøX)gV+UbX
Perl 5, 85 bytes
84 bytes code + 1 for -p.
eval join"",map"y/$_/@{[/./&&$'.$&]}/;",(1234567890,QWERTYUIOP,ASDFGHJKL,ZXCVBNM)x<>
Perl 5, 94 + 1 (-p) = 95 bytes
$s=<>;for$i(1234567890,QWERTYUIOP,ASDFGHJKL,ZXCVBNM){eval"y/$i/".(substr$i,$s%length$i)."$i/"}
Jelly, 13 bytes
ØQØDṭ,ṙ€¥⁸F€y
How it works
ØQØDṭ,ṙ€¥⁸F€y Main link. Left argument: n (integer). Right argument: s (string)
ØQ Qwerty; set the return value to
["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"].
ØD Digits; yield "0123456789".
ṭ Tack, yielding ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM", "0123456789"].
¥⁸ Call the two links to the left as a dyadic chain, with right
argument n.
ṙ€ Rotate each string in the array n units to the left.
, Yield the pair of the unmodified and the rotated string array.
F€ Flatten each, mapping, e.g., ["QWERTYUIOP", ..., "0123456789"] to
"QWERTYUIOPASDFGHJKLZXCVBNM0123456789".
y Translate s according to the mapping we've built.
Python 2, 110 bytes
lambda s,n,y='1234567890'*99+'QWERTYUIOP'*99+'ASDFGHJKL'*99+'ZXCVBNM'*99:''.join(y[y.find(c)+n%630]for c in s)
This uses a big enough string (99 copies of each row) and the LCM between the rows lengths (630) to find the correct substitution avoiding individual correction between each row.
Retina, 49 bytes
"$&"+T`9o`dQW\ERTYUI\OPQASDFG\HJK\LAZXC\VBNMZ
0A`
Try it online! Takes input n and s on separate lines. Explanation:
"$&"+
Repeat n times.
T`9o`dQW\ERTYUI\OPQASDFG\HJK\LAZXC\VBNMZ
Shift all the characters one key to the right.
0A`
Delete n.