Skip to content

Commit

Permalink
Add Coding with squared strings
Browse files Browse the repository at this point in the history
Kata: 56fcc393c5957c666900024d
  • Loading branch information
sierikov committed Sep 4, 2024
1 parent 82f3b10 commit 4db05db
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Stores my solutions to kata problems listed on [Codewars](https://www.codewars.c
[The Hashtag Generator](https://www.codewars.com/kata/52449b062fb80683ec000024),
[Human Readable Time](https://www.codewars.com/kata/52685f7382004e774f0001f7),
[Last Digit of a Large Number](./src/main/scala/sierikov/codewars/kyu5/LastDigitOfLargeNumber.scala),
[Coding with Squared String](./src/main/scala/sierikov/codewars/kyu5/CodingWithSquaredStrings.scala)

### 6 kuy

Expand Down
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)
}
}
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
}
}

}

0 comments on commit 4db05db

Please sign in to comment.