6 kyu

Moves in squared strings (II)

152 of 5,006g964

Description:

You are given a string of n lines, each substring being n characters long: For example:

s = "abcd\nefgh\nijkl\nmnop"

We will study some transformations of this square of strings.

  • rot(s):
    Clock rotation 180 degrees.
    rot(s) => "ponm\nlkji\nhgfe\ndcba"
    
  • selfie_and_rot(s) (or selfieAndRot or selfie-and-rot):
    It is an initial string combined with its 180-degree clock-rotated version, interspersed with dots proportional to the length of the segments, to better illustrate the rotation when printed.
    s = "abcd\nefgh\nijkl\nmnop" --> 
    "abcd....\nefgh....\nijkl....\nmnop....\n....ponm\n....lkji\n....hgfe\n....dcba"
    

On printing, these functions work as follows:

|rot             |selfie_and_rot
|abcd --> ponm   |abcd --> abcd....
|efgh     lkji   |efgh     efgh....
|ijkl     hgfe   |ijkl     ijkl....   
|mnop     dcba   |mnop     mnop....
                           ....ponm
                           ....lkji
                           ....hgfe
                           ....dcba

Notice that the number of dots is the common length of "abcd", "efgh", "ijkl", "mnop".

Task:

  • Write these two functions rot and selfie_and_rot

and

  • high-order function oper(fct, s) where

  • fct is the function of one variable f to apply to the string s (fct will be one of rot, selfie_and_rot)

Examples:

s = "abcd\nefgh\nijkl\nmnop"
oper(rot, s) => "ponm\nlkji\nhgfe\ndcba"
oper(selfie_and_rot, s) => "abcd....\nefgh....\nijkl....\nmnop....\n....ponm\n....lkji\n....hgfe\n....dcba"

Notes:

  • The form of the parameter fct in oper changes according to the language. You can see each form according to the language in "Your test cases".
  • It could be easier to take these katas from number (I) to number (IV)

Forthcoming katas will study other tranformations.

Bash Note:

The input strings are separated by , instead of \n. The ouput strings should be separated by \r instead of \n. See "Sample Tests".

Algorithms
Strings

More By Author:

Check out these other kata created by g964

Stats:

CreatedMar 6, 2016
PublishedMar 6, 2016
Warriors Trained19458
Total Skips7179
Total Code Submissions24854
Total Times Completed5006
Ruby Completions152
Python Completions1268
JavaScript Completions1129
CoffeeScript Completions12
Java Completions394
C# Completions250
Haskell Completions110
Clojure Completions61
C++ Completions381
PHP Completions226
TypeScript Completions152
F# Completions36
C Completions162
Crystal Completions11
Rust Completions198
Swift Completions143
R Completions45
Shell Completions40
Objective-C Completions13
OCaml Completions21
Scala Completions66
Julia Completions21
Go Completions111
Nim Completions7
Racket Completions15
Kotlin Completions92
Prolog Completions12
Dart Completions110
Pascal Completions4
Lua Completions33
Perl Completions10
Elm Completions6
D Completions8
Erlang Completions5
Total Stars252
% of votes with a positive feedback rating90% of 855
Total "Very Satisfied" Votes702
Total "Somewhat Satisfied" Votes127
Total "Not Satisfied" Votes26
Ad
Contributors
  • g964 Avatar
  • NaMe613 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • B1ts Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • Kacarott Avatar
  • 4500zenja1 Avatar
  • saudiGuy Avatar
Ad