Reversing and Shifting

5

3

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.

Lyxal

Posted 2019-12-24T20:13:58.770

Reputation: 5 253

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 Reverse 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

Answers

6

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.

FlipTack

Posted 2019-12-24T20:13:58.770

Reputation: 13 242

6

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

Adám

Posted 2019-12-24T20:13:58.770

Reputation: 37 779

3

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!

Chas Brown

Posted 2019-12-24T20:13:58.770

Reputation: 8 959

3

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

Value Ink

Posted 2019-12-24T20:13:58.770

Reputation: 10 608

3

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.

79037662

Posted 2019-12-24T20:13:58.770

Reputation: 1 739

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. – Kevin Cruijssen – 2020-01-03T09:04:24.307

2

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

AZTECCO

Posted 2019-12-24T20:13:58.770

Reputation: 2 441

2

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

DeathIncarnate

Posted 2019-12-24T20:13:58.770

Reputation: 916

1

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.

Nick Kennedy

Posted 2019-12-24T20:13:58.770

Reputation: 11 829

1

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.

Neil

Posted 2019-12-24T20:13:58.770

Reputation: 95 035

1

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

uanirudhx

Posted 2019-12-24T20:13:58.770

Reputation: 11

1

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.

tsh

Posted 2019-12-24T20:13:58.770

Reputation: 13 072