Given the following input:

  • An integer n where n > 0.
  • A string s where s is not empty and s~=[0-9A-Z]+ (alpha-numeric capitals only).

Using a standard, simplified QWERTY keyboard (as shown below):


Perform the following operation:

  • Find the original row that each character is in on the keyboard.
  • Replace the letter with the correct shifted equivalent for n based on its original position + n.
    • E.G. s="AB" and n=2: A would become D and B would become M.
  • If keyboard_row[position + n] > keyboard_row.length, wrap back to the start.
    • E.G. s="0P" and n=2: 0 would become 2 and P would become W.


f("0PLM",1)    = 1QAZ
f("ZXCVB",2)   = CVBNM
f("HELLO",3)   = LYDDW
f("0PLM",11)   = 1QSV
f("0PLM",2130) = 0PHX


This is slightly more difficult than it seems at first glance.

  • 2
    \$\begingroup\$ Are we allowed to take the input as character-array instead of string? Currently assumed we, but forgot to ask.. \$\endgroup\$ Feb 5, 2018 at 14:51
  • \$\begingroup\$ @KevinCruijssen shrug sure, it's not too outlandish. Unless it saves you a byte to break a tie I'm not complaining. \$\endgroup\$ Feb 5, 2018 at 22:43

15 Answers 15


Jelly, 13 bytes


Try it online!

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
            y  Translate s according to the mapping we've built.
  • 2
    \$\begingroup\$ Jelly has keyboard-layout built-ins eh? \$\endgroup\$ Feb 5, 2018 at 14:13
  • 4
    \$\begingroup\$ @MagicOctopusUrn No, only QWERTY right now :-P \$\endgroup\$ Feb 5, 2018 at 14:14
  • \$\begingroup\$ 13 bytes? Which character set is that supposed to be? In UTF-8 it's 26 bytes! \$\endgroup\$
    – Cephalopod
    Feb 5, 2018 at 21:23
  • 2
    \$\begingroup\$ @Cephalopod Jelly uses the Jelly code page. \$\endgroup\$
    – Dennis
    Feb 5, 2018 at 21:26
  • \$\begingroup\$ I'm not going to complain about regex anymore \$\endgroup\$
    – Xeoncross
    Aug 22 at 14:52

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)

Try it online!

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.


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.


Try it online.

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 the current qwerty-line contains the character
         //     Set `j` to the index of this character on that line + input `n`
         //     Replace the character at index `i`
         //     with the new character (at index `j` modulo length_of_qwerty_line)
  • 1
    \$\begingroup\$ 158 bytes, at the cost of input-outputting the char[]. \$\endgroup\$ Feb 6, 2018 at 9:30

Retina, 49 bytes


Try it online! Takes input n and s on separate lines. Explanation:


Repeat n times.


Shift all the characters one key to the right.


Delete n.


JavaScript (ES6), 101 99 bytes

Takes input in currying syntax (s)(n). Works with arrays of characters.


Test cases

let f =


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


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.


Jelly, 18 bytes


Try it online!


C,  152  149 bytes

Thanks to @gastropner for saving three bytes!


Try it online!


    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]);
  • \$\begingroup\$ Either I'm hallucinating, or the inner loop can be changed to for(j=l;j--;) but I don't know why without any other changed. Still, should get you to 149. \$\endgroup\$
    – gastropner
    Feb 7, 2018 at 21:12
  • \$\begingroup\$ @gastropner Ah, yes, the search order doesn't matter, so it works. Thanks! \$\endgroup\$
    – Steadybox
    Feb 7, 2018 at 22:11

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

Try it online!


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

Haskell, 99 bytes

f(s,n)=[dropWhile(/=c)(cycle r)!!n|c<-s,r<-words"1234567890 QWERTYUIOP ASDFGHJKL ZXCVBNM",elem c r]

Try it online!

  • \$\begingroup\$ You can use s#n= ... instead of f(s,n)= ... which is only an example notation used for the examples. \$\endgroup\$
    – Laikoni
    Feb 6, 2018 at 8:16

Perl 5, 94 + 1 (-p) = 95 bytes


Try it online!

  • \$\begingroup\$ Damn, I didn't see your answer. They're basically the same, feel free to use my optimisations and I'll remove my answer. Let me know, if not, I'll just remove this comment :) \$\endgroup\$ Feb 5, 2018 at 17:04
  • \$\begingroup\$ @DomHastings They are different enough. Please keep both. I like to see variations in approach. I learn from all of them... \$\endgroup\$
    – Ton Hospel
    Feb 5, 2018 at 22:22

Japt, 20 bytes

Running out the door to dinner so more golfing and an explanation to follow.


Try it


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<>'
  • \$\begingroup\$ Wow, I can't believe you got 29 bytes off mine! I was pretty happy with it originally... \$\endgroup\$ Feb 9, 2018 at 8:14

Perl 5, 85 bytes

84 bytes code + 1 for -p.

eval join"",map"y/$_/@{[/./&&$'.$&]}/;",(1234567890,QWERTYUIOP,ASDFGHJKL,ZXCVBNM)x<>

Try it online!


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]

Try it online!

Lambda function with the signature Int ![Char] -> [Char]


Ruby, 101 bytes


Try it online!

I'm honestly a little disappointed that I couldn't do better with 'cleverer' methods. The closest I got was along the lines of

n.times{s.tr! a,b}

for a net gain of 7 characters.


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.