-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kata: 56fcc393c5957c666900024d
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/scala/sierikov/codewars/kyu5/CodingWithSquaredStrings.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package sierikov.codewars.kyu5 | ||
|
||
/** | ||
* Kata: https://www.codewars.com/kata/56fcc393c5957c666900024d | ||
*/ | ||
object CodingWithSquaredStrings { | ||
|
||
def code(s: String): String = { | ||
val n = math.sqrt(s.length).ceil.toInt | ||
val square = n * n | ||
val str = s + "\u000b" * (square - s.length) | ||
|
||
rotateMatrixClockwise(str.toList.grouped(n).toList).map(_.mkString).mkString("\n") | ||
} | ||
|
||
def decode(s: String): String = { | ||
val m = rotateMatrixAnticlockwise(s.split("\n").map(_.split("").toList).toList) | ||
m.flatten.mkString.takeWhile(x => x != '\u000b') | ||
} | ||
|
||
private def rotateMatrixClockwise[A](m: List[List[A]]): List[List[A]] = { | ||
if (m.isEmpty || m.head.isEmpty) return m | ||
m.head.indices.map { i => m.reverseIterator.map(_(i)).toList }.toList | ||
} | ||
|
||
private def rotateMatrixAnticlockwise[A](m: List[List[A]]): List[List[A]] = { | ||
if (m.isEmpty || m.head.isEmpty) return m | ||
rotateMatrixClockwise(m.map(_.reverse)).map(_.reverse) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/scala/sierikov/codewars/kyu5/CodingWithSquaredStringsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package sierikov.codewars.kyu5 | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import sierikov.codewars.kyu5.CodingWithSquaredStrings.* | ||
|
||
class CodingWithSquaredStringsTest extends AnyFlatSpec { | ||
|
||
val data = "What do you remember? When I looked at his streaky glasses, I wanted " + | ||
"to leave him. And before that? He stole those cherries for me at midnight. We were walking " + | ||
"in the rain and I loved him. And before that? I saw him coming " + | ||
"toward me that time at the picnic, edgy, foreign." | ||
|
||
val dataEncoded = | ||
"\u000bctg?.nadr d gdbW\n\u000b,i lnis tl eh\n\u000b mtIAakietboaara\n\u000beeo nnigsoe st?t\n\u000bd wsddnh lfls \n\u000bgaaa gtfeoeehWd\n" + | ||
"\u000bytrwbI .o rasiho\n\u000b, d e i rtev,se \n\u000b t hflnW h e ny\n\u000bfhmioo emot Is o\n\u000boeemrvt eshh tIu\n\u000br eehw eaiwr \n" + | ||
"\u000beptc deea tmaelr\n\u000biihot rtc?.naoe\n\u000bgcamhhre h tkom\n\u000bnntiaia meHAeyke\n\u000b.i ntmiwirend em" | ||
|
||
it should "pass basic tests code" in { | ||
testing(code(data), dataEncoded) | ||
|
||
} | ||
it should "pass basic tests decode" in { | ||
testing(decode(dataEncoded), data) | ||
} | ||
|
||
private def testing(actual: String, expect: String): Unit = { | ||
println("Actual: " + actual) | ||
println("\nExpect: " + expect) | ||
println("-") | ||
assertResult(expect) { | ||
actual | ||
} | ||
} | ||
|
||
} |