Adapted from exercise 8 of 100 little Keg exercises

String manipulation is a very important part of any programming language. Consequently, that is what this challenge is about.

The Challenge

I want you to write a program that:

  • Takes a series of characters as input and,
  • Then takes another input (integer/letter) (we'll call this instruction) and,
  • Takes a single integer (we'll call this times) as the last input and then,
  • If instruction is equal to the letter R:
    • Reverse the string times times.
  • Otherwise, if instruction is equal to the letter l:
    • Shift the string left times times
  • Otherwise, for all other values of instruction:
    • Shift the string right times times

Test Cases

String, Instruction, Times -> Output
"Howdy!", R, 1 -> "!ydwoH"
"elloH", r, 1 -> "Hello"
"tedShif", l, 3 -> "Shifted"
"sn't it?Amazing, i", l, 8 -> "Amazing, isn't it?"


  • Input can be taken as any convenient format... if a list of three items works best for you, take the input as a list. If newline-separated input is how you choose to work, go ahead and use newline-separated input.
  • The string, instruction and times integer can be taken in any order
  • Output is also to be given in any convenient format.
  • The input string will not contain newlines, nor unprintable ascii characters.
  • You can substitute R and l for any other two characters, as long as you specify what the two characters are
  • All instructions are case sensitive. In other words, only input of R should reverse the string, not r.
  • The magnitude of a shift will always be less than the length of the string
  • The times integer is a positive integer (i.e. \$ n > 0 \$)


This is , so the answer with the fewest bytes wins.

  • 1
    \$\begingroup\$ May we assume the magnitude of a shift is less than the length of the string? \$\endgroup\$
    – FlipTack
    Commented Dec 24, 2019 at 20:40
  • 1
    \$\begingroup\$ Yes, you may indeed make that assumption. \$\endgroup\$
    – lyxal
    Commented Dec 24, 2019 at 20:42
  • 1
    \$\begingroup\$ And that the integer given will always be non-negative? \$\endgroup\$
    – FlipTack
    Commented Dec 24, 2019 at 21:06
  • \$\begingroup\$ reverse the stack you mean string, right? \$\endgroup\$
    – Luis Mendo
    Commented Dec 24, 2019 at 21:13
  • \$\begingroup\$ May the input requires Reverse the string 2 times? \$\endgroup\$
    – tsh
    Commented Dec 25, 2019 at 2:27

12 Answers 12


APL (Dyalog Unicode), 20 bytesSBCS

Full program. Prompts for the string, then times, then instruction:

  • + for reversal
  • 1 for left shift
  • any other character for right shift

Try it online!

 prompt for string

on that, do the following:

 prompt for times

()⌽⍣ apply the function that many times, with the following to its left:

∩'+1' intersection of the following and "+1":

 prompt for instruction

¯1,⍨ append negative one

 pick the first one


 evaluate (gives 1 or -1 or the complex conjugate function +)

The function does a:

  • left shift if it has 1 on its left
  • right shift if it has -1 on its left
  • reversal if it has any function on its left

+ negates the imaginary part of its argument but strings have no imaginary parts, so it does nothing


Python 3.8 (pre-release), 65 bytes

lambda s,i,t:[s[(n:=[-t,t][i=='l']):]+s[:n],s[::(-1)**t]][i=='R']

Try it online!

Abuses the de-facto ternary statement [a,b][condition].

  • If i=='R':
    • Return the string, read with step (-1)**t. This is -1 (reversed) for odd t and 1 (not reversed) for even t.
  • Else:
    • Let n be [-t,t][i=='l']. This is t when left-shifting and -t otherwise (right-shifting).
    • Return the string, spliced accordingly: s[n:] + s[:n]. Thanks to Python's negative indexing, this works like a charm for both cases.

Python 2, 67 bytes

lambda s,t,i:[s[t:]+s[:t],s[::1-t%2*2],s[-t:]+s[:-t]]['lR'.find(i)]

Try it online!


Ruby, 63 bytes


Try it online!

An anonymous lambda that takes 3 arguments. If the instruction is R, it returns the original or the reversed version based on whether n is even or odd. Otherwise, it converts the string into a character array and rotates that left or right based on the instruction specified (Ruby rotates left if the argument provided is positive) the joins the char array back together into a string using *''.


05AB1E, 16 bytes


Try it online!

1 instead of R, 0 instead of l.

I'm new to this language, would appreciate if anyone could give me tips.

²³               # Push the second and third inputs.
  F              # Do the following [third input] times:
   1¹Qi          # If the first input is equal to 1,
       R         # Reverse (at this point, the second input will be at the top of the stack).
        ë0¹Qi    # Else-if the first input is equal to 0,
             À   # Shift left.
              ëÁ # Else shift right.
  • \$\begingroup\$ F²iRë²_iÀëÁ (11 bytes) with the input-order changed a bit. Some tips: 1) Input is implicitly if nothing is on the stack, so you can take advantage of that. This is what I did with the initial F without explicitly using the first input, and I do the same with the R/À/Á and the third implicit input-string. 2) Only 1 is truthy in 05AB1E, so no need to explicitly do the 1Q check. Simply ²i is therefore enough to check if this second input-integer is exactly 1. As for the ²_i, the _ is a builtin to check if an integer is exactly 0. \$\endgroup\$ Commented Jan 3, 2020 at 9:04

C (clang), 72 bytes


Try it online!

Input as: char* , length, instruction(r=-1 , R=0 , l =1), times

Loop into input string using a shifted index with modulo i?(z+t*i+j)%z or a length - index -1 for reverse


Burlesque, 32 bytes


Try it online!

Takes arguments as: "StringToMod" N "Op" Where op is a reverse ordered string for reverse, a string in order for rotate right and anything else for rotate left, other short options or op could be space character, alpha character, other. If those are unacceptable input add 4 bytes for {'R==} & {'r==} as conditions.

pe         # Push inputs to stack
 {so} {<-} # If sorted push reverse
 {SO} {rt} # If reverse-sorted push rotate right
 {1}  {RT} # Else push rotate left
}cn        # Condition on op
jE!        # Evaluate N times

Jelly, 18 bytes


Try it online!

A dyadic link taking a Jelly string prepended with the instruction as its left argument (e.g. RHello world) and the integer as its right argument. Returns a Jely string.


Pyth, 38 bytes


Try it online!

This is a quite naive strategy but it works ¯\_(ツ)_/¯

Input is taken on three lines: the first is the input, second is the operation, third is the count.

Substitute R for (space). Substitute l for " (double quote).


JavaScript (Node.js), 74 bytes


Try it online!

Input i (instruction, 1 = Reverse, 2 = Left, 3 = Right), n (times), a (array of characters).

Output array of characters.


Charcoal, 27 25 bytes


Try it online! Link is to verbose version of code. Explanation:


Convert the third input to integer, then multiply it by the index of the second input in Rr. This leaves it zero if the second input is R and unchanged if it is r but it negates the third input if the second input is anything else.


If the third input is now zero then reverse the first input the original number of times.


Shift the first input rightwards by the third input.


Vyxal D, 11 bytes


Try it Online!

Uses 0 for reverse, 1 for shift left, and 2 for shift right.


Your Answer

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

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