6 kyu

Reverse or rotate?

974 of 24,174g964

Description:

The input is a string str of digits. Cut the string into chunks (a chunk here is a substring of the initial string) of size sz (ignore the last chunk if its size is less than sz).

If the sum of a chunk's digits is divisible by 2, reverse that chunk; otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.

If

  • sz is <= 0 or if str == "" return ""
  • sz is greater (>) than the length of str it is impossible to take a chunk of size sz hence return "".

Examples:

("123456987654", 6) --> "234561876549"
("123456987653", 6) --> "234561356789"
("66443875", 4) --> "44668753"
("66443875", 8) --> "64438756"
("664438769", 8) --> "67834466"
("123456779", 8) --> "23456771"
("", 8) --> ""
("123456779", 0) --> "" 
("563000655734469485", 4) --> "0365065073456944"
Example of a string rotated to the left by one position:
s = "123456" gives "234561".
Algorithms
Strings

Similar Kata:

More By Author:

Check out these other kata created by g964

Stats:

CreatedFeb 6, 2016
PublishedFeb 6, 2016
Warriors Trained89900
Total Skips29772
Total Code Submissions143010
Total Times Completed24174
Ruby Completions974
Python Completions5817
JavaScript Completions7069
CoffeeScript Completions24
Haskell Completions319
Clojure Completions128
C# Completions1110
Java Completions2535
Elixir Completions206
C++ Completions1658
PHP Completions884
TypeScript Completions759
Crystal Completions23
F# Completions76
C Completions628
Rust Completions488
Swift Completions312
R Completions86
Shell Completions69
OCaml Completions30
Kotlin Completions552
Scala Completions149
Julia Completions38
PowerShell Completions37
Go Completions580
Nim Completions8
Racket Completions24
Reason Completions1
VB Completions27
Lua Completions80
Pascal Completions7
Perl Completions16
Elm Completions2
D Completions6
COBOL Completions4
Total Stars1237
% of votes with a positive feedback rating87% of 3130
Total "Very Satisfied" Votes2419
Total "Somewhat Satisfied" Votes588
Total "Not Satisfied" Votes123
Ad
Contributors
  • g964 Avatar
  • NaMe613 Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • akar-0 Avatar
Ad