5
\$\begingroup\$

Sandbox

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?"

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 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 \$)

Scoring

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

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

11 Answers 11

6
\$\begingroup\$

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
(⍎⍕⊃¯1,⍨⍞∩'+1')⌽⍣⎕⊢⍞

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

 stringify

 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

|improve this answer|||||
\$\endgroup\$
6
\$\begingroup\$

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.
|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

Ruby, 63 bytes

->s,c,n{c==?R?[s,s.reverse][n%2]:s.chars.rotate(c==?l?n:-n)*''}

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 *''.

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 16 bytes

²³F1¹QiRë0¹QiÀëÁ

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.

²³F1¹QiRë0¹QiÀëÁ
²³               # 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.
|improve this answer|||||
\$\endgroup\$
  • \$\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\$ – Kevin Cruijssen Jan 3 at 9:04
2
\$\begingroup\$

C (clang), 72 bytes

j;f(char*s,z,i,t){for(j=0;j<z;++j)putchar(s[i?(z+t*i+j)%z:t%2?z+~j:j]);}

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

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

Burlesque, 32 bytes

pe{{so}{<-}{SO}{rt}{1}{RT}}cnjE!

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
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Jelly, 18 bytes

Ḣ⁾lRi‘ịUṛ¡ṭṙN,$}¥ɗ

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.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Pyth, 38 bytes

J.zKs@J2A<J2?qHd_FGK?qHN+>GK<GK+>KG<KG

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

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 74 bytes

i=>n=>g=a=>n--?g(a.map((_,p)=>a[(i-1?i-2?L+p-1:p+1:L+~p)%L],L=a.length)):a

Try it online!

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

Output array of characters.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 27 25 bytes

≔×⌕RrηIζδF׬δIζ≦⮌θ⭆θ§θ⁻κδ

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

≔×⌕RrηIζζ

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.

F׬δIζ≦⮌θ

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.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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