Advertisement
Guest User

Reverse or Rotate

a guest
Apr 4th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.40 KB | None | 0 0
  1. object RevRot {
  2.  
  3.   def revRot(strng: String, sz: Int): String = {
  4.     def isDivisible(s: String): Boolean = s.map[Int](x => x.toInt).map(x => x * x * x).sum % 2 == 0
  5.  
  6.     def rotate(s: String): String = s.drop(1) + s.head
  7.  
  8.     if (sz > strng.length || sz <= 0 || strng.length == 0) ""
  9.     else strng.grouped(sz).filter(_.length == sz).map(x => if (isDivisible(x)) x.reverse else rotate(x)).mkString
  10.   }
  11. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement