5
3
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 letterR
:- Reverse the string
times
times.
- Reverse the string
- Otherwise, if
instruction
is equal to the letterl
:- Shift the string left
times
times
- Shift the string left
- Otherwise, for all other values of
instruction
:- Shift the string right
times
times
- Shift the string right
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?"
Rules
- 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
andl
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, notr
. - 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 \$)
Scoring
This is code-golf, so the answer with the fewest bytes wins.
1May we assume the magnitude of a shift is less than the length of the string? – FlipTack – 2019-12-24T20:40:19.260
1Yes, you may indeed make that assumption. – Lyxal – 2019-12-24T20:42:10.983
1And that the integer given will always be non-negative? – FlipTack – 2019-12-24T21:06:01.247
reverse the stack you mean string, right? – Luis Mendo – 2019-12-24T21:13:09.500
May the input requires
R
everse the string 2 times? – tsh – 2019-12-25T02:27:14.383@tsh yes, the input could possibly be reversed 2 times – Lyxal – 2019-12-25T02:28:21.043