Skip to content

Commit

Permalink
Add batch of problems
Browse files Browse the repository at this point in the history
- Binary Addition
- Is this a triangle?
- Two to One
- Convert Number to Reversed Array of Digits
- Your order, please!
  • Loading branch information
sierikov committed Feb 1, 2024
1 parent 62e09c6 commit ca3b37f
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Stores my solutions to kata problems listed on [Codewars](https://www.codewars.c
[Counting Duplicates](https://www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1),
[Find The Parity Outlier](https://www.codewars.com/kata/5526fc09a1bbd946250002dc),
[Persistent Bugger.](https://www.codewars.com/kata/55bf01e5a717a0d57e0000ec),
[Unique In Order](https://www.codewars.com/kata/54e6533c92449cc251001667)
[Unique In Order](https://www.codewars.com/kata/54e6533c92449cc251001667),
[Your order, please!](https://www.codewars.com/kata/55c45be3b2079eccff00010f),

### 7 kyu

Expand All @@ -79,6 +80,9 @@ Stores my solutions to kata problems listed on [Codewars](https://www.codewars.c
[Beginner Series #3 Sum of Numbers](https://www.codewars.com/kata/55f2b110f61eb01779000053),
[Credit Card Mask](https://www.codewars.com/kata/5412509bd436bd33920011bc),
[Friend or Foe?](https://www.codewars.com/kata/55b42574ff091733d900002f),
[Binary Addition](https://www.codewars.com/kata/551f37452ff852b7bd000139),
[Is this a triangle?](https://www.codewars.com/kata/56606694ec01347ce800001b),
[Two to One](https://www.codewars.com/kata/5656b6906de340bd1b0000ac),

### 8 kuy

Expand All @@ -90,3 +94,4 @@ Stores my solutions to kata problems listed on [Codewars](https://www.codewars.c
[Return Negative](https://www.codewars.com/kata/55685cd7ad70877c23000102),
[Sum of Positive](https://www.codewars.com/kata/5715eaedb436cf5606000381),
[Keep Hydrated!](https://www.codewars.com/kata/582cb0224e56e068d800003c),
[Convert Number to Reversed Array of Digits](https://www.codewars.com/kata/5583090cbe83f4fd8c000051),
7 changes: 7 additions & 0 deletions src/main/scala/sierikov/codewars/kyu6/YourOrderPlease.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sierikov.codewars.kyu6

object YourOrderPlease {
def order(str: String): String = {
str.split(" ").sortBy(_.filter(_.isDigit)).mkString(" ")
}
}
5 changes: 5 additions & 0 deletions src/main/scala/sierikov/codewars/kyu7/BinaryAddition.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sierikov.codewars.kyu7

object BinaryAddition {
def addBinary(a: Int, b: Int): String = (a + b).toBinaryString
}
8 changes: 8 additions & 0 deletions src/main/scala/sierikov/codewars/kyu7/IsThisTriangle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package sierikov.codewars.kyu7

object IsThisTriangle {

def isTriangle(a: Int, b: Int, c: Int): Boolean = {
a + b > c && a + c > b && b + c > a
}
}
5 changes: 5 additions & 0 deletions src/main/scala/sierikov/codewars/kyu7/TwoToOne.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sierikov.codewars.kyu7

object TwoToOne {
def longest(s1: String, s2: String): String = (s1 ++ s2).distinct.sorted
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sierikov.codewars.kyu8

object ConvertNumberToReversedArrayDigits {
def digitize(n: Long): Seq[Int] = n.toString.reverse.map(_.asDigit)
}
20 changes: 20 additions & 0 deletions src/test/scala/sierikov/codewars/kyu6/YourOrderPleaseSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sierikov.codewars.kyu6

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers.shouldBe

class YourOrderPleaseSpec extends AnyFlatSpec {

private val tests = List(
("is2 Thi1s T4est 3a", "Thi1s is2 3a T4est"),
("4of Fo1r pe6ople g3ood th5e the2", "Fo1r the2 g3ood 4of th5e pe6ople"),
("", "")
)

tests.foreach { case (input, expected) =>
it should s"return $expected given $input" in {
YourOrderPlease.order(input) shouldBe expected
}
}

}
23 changes: 23 additions & 0 deletions src/test/scala/sierikov/codewars/kyu7/BinaryAdditionSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sierikov.codewars.kyu7

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers.shouldBe

class BinaryAdditionSpec extends AnyFlatSpec {

private val tests = List(
(1, 1, "10"),
(0, 1, "1"),
(1, 0, "1"),
(2, 2, "100"),
(51, 12, "111111"),
(0, 0, "0"),
(100, 100, "11001000")
)

tests.foreach { case (a, b, expected) =>
it should s"return $expected for $a + $b" in {
BinaryAddition.addBinary(a, b) shouldBe expected
}
}
}
30 changes: 30 additions & 0 deletions src/test/scala/sierikov/codewars/kyu7/IsThisTriangleSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sierikov.codewars.kyu7

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers.shouldBe

class IsThisTriangleSpec extends AnyFlatSpec {
private val tests = Seq(
(1, 2, 2, true),
(7, 2, 2, false),
(1, 2, 3, false),
(1, 3, 2, false),
(3, 1, 2, false),
(5, 1, 2, false),
(1, 2, 5, false),
(2, 5, 1, false),
(4, 2, 3, true),
(5, 1, 5, true),
(2, 2, 2, true),
(-1, 2, 3, false),
(1, -2, 3, false),
(1, 2, -3, false),
(0, 2, 3, false)
)

tests.foreach { case (a, b, c, expected) =>
it should s"return $expected when sides are ($a, $b, $c)" in {
IsThisTriangle.isTriangle(a, b, c) shouldBe expected
}
}
}
19 changes: 19 additions & 0 deletions src/test/scala/sierikov/codewars/kyu7/TwoToOneSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sierikov.codewars.kyu7

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers.shouldBe

class TwoToOneSpec extends AnyFlatSpec {

private val tests = List(
("aretheyhere", "yestheyarehere", "aehrsty"),
("loopingisfunbutdangerous", "lessdangerousthancoding", "abcdefghilnoprstu"),
("inmanylanguages", "theresapairoffunctions", "acefghilmnoprstuy")
)

tests.foreach { case (a, b, expected) =>
it should s"return $expected for ${a.take(5)}.. and ${b.take(5)}" in {
TwoToOne.longest(a, b) shouldBe expected
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sierikov.codewars.kyu8

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers.shouldBe

class ConvertNumberToReversedArrayDigitsSpec extends AnyFlatSpec {
private val tests = List(
(35231L, Seq(1, 3, 2, 5, 3)),
(348597L, Seq(7, 9, 5, 8, 4, 3)),
(0L, Seq(0)),
(23582357L, Seq(7, 5, 3, 2, 8, 5, 3, 2)),
(984764738L, Seq(8, 3, 7, 4, 6, 7, 4, 8, 9)),
(45762893920L, Seq(0, 2, 9, 3, 9, 8, 2, 6, 7, 5, 4)),
(548702838394L, Seq(4, 9, 3, 8, 3, 8, 2, 0, 7, 8, 4, 5))
)

tests.foreach { case (input, expected) =>
it should s"return $expected for $input" in {
ConvertNumberToReversedArrayDigits.digitize(input) shouldBe expected
}
}
}

0 comments on commit ca3b37f

Please sign in to comment.