Last active
January 23, 2020 16:10
-
-
Save kryptt/1478c2825b4fd6e8a7bbdf0dfb598450 to your computer and use it in GitHub Desktop.
Logic in your Types Answers
This file contains hidden or 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
| def sumOfAllNumbers(): Int = | |
| Array(10,20,30,40,50).sum | |
| // -- sanity check | |
| sumOfAllNumbers() == 150 | |
| def evenElements[A](ls: List[A]): List[A] = | |
| ls.view.zipWithIndex.filter(_._2 % 2 == 1).map(_._1).toList | |
| // -- sanity check | |
| evenElements(List(1, 2, 3, 4, 5)) == List(2, 4) | |
| def isPalindrome[A](ls: List[A]): Boolean = | |
| ls.reverse == ls | |
| // -- sanity check | |
| isPalindrome(List(1, 2, 1)) == true | |
| isPalindrome(List(1)) == true | |
| isPalindrome(List(1, 2, 2, 1)) == true | |
| def largest[A <% Ordered[A]](ls: List[A]): A = | |
| ls.max | |
| // -- sanity check | |
| largest(List(1, 5, 2)) == 5 | |
| largest(List(5, 1, 2)) == 5 | |
| largest(List(2, 1, 5)) == 5 | |
| // -- get the nth Catalan number | |
| // (1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862) | |
| // -- start external context | |
| object Succ { def unapply(i: Int) = if (i == 0) None else Some(i - 1) } | |
| // -- end external context | |
| val CatalanNumbers = Stream.unfold(0 -> 1l) { | |
| case (0, c) => Some(c -> (1, 1l)) | |
| case (Succ(n), cn) => | |
| val c = cn * (4 * n + 2) / (n + 2) | |
| Some(c -> (n + 2, c)) | |
| } | |
| def catalan(i: Int): Long = CatalanNumbers(i) | |
| // -- sanity check | |
| catalan(0) == 1 | |
| catalan(1) == 1 | |
| catalan(3) == 5 | |
| catalan(6) == 132 | |
| // -- start external context | |
| val data: Iterator[Int] = List(1, 2, 3).iterator // emulate input from environment | |
| sealed trait Result | |
| case class Data(data: Int) extends Result | |
| case object Done extends Result | |
| case class Error(message: String) extends Result | |
| def receive: Result = if (data.hasNext) Data(data.next) else Done | |
| // -- end external context | |
| def getAll(): Option[List[Int]] = { | |
| var last: Option[Result] = None | |
| val data = Stream.continually(receive) | |
| .tapEach(s => last = Some(s)) | |
| .takeWhile(_.isInstanceOf[Data]) | |
| .foldLeft(List.empty[Int]) { | |
| case (ds, Data(d)) => ds :+ d | |
| } | |
| last.fold(Option.empty[List[Int]]) { | |
| case e: Error => None | |
| case _ => Some(data) | |
| } | |
| } | |
| // -- sanity check | |
| getAll() == List(1, 2, 3) |
This file contains hidden or 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
| import scala.util.Try | |
| def parseInt(number: String): Try[Int] = Try(number.toInt) | |
| // -- sanity check | |
| parseInt("oops") | |
| def root(number: Int): Option[Int] = if (number < 1) None else Some(Math.sqrt(number.toDouble).toInt) | |
| // -- sanity check | |
| root(-1) | |
| case class NonEmptyList[A](head: A, tail: List[A]) | |
| def head[A](list: NonEmptyList[A]): A = list.head | |
| // -- sanity check | |
| head(NonEmptyList(1, Nil)) | |
| def find[K, V](dictionary: Map[K, V], key: K): Option[V] = dictionary.get(key) | |
| // -- sanity check | |
| find(Map.empty[Int, String], 1) | |
| find(Map(1 -> "Uno"), 2) | |
| def safeSquare(i: Int): Either[Exception, Int] = { | |
| if (i > 46340) Left(new IllegalArgumentException("Input too high")) | |
| else if (i < -46340) Left(new IllegalArgumentException("Input too low")) | |
| else Right(i * i) | |
| } | |
| // -- sanity check | |
| safeSquare(46500) |
This file contains hidden or 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
| import scala.util.Random | |
| def abs: Int = | |
| if (number < 0) -number | |
| else number | |
| // -- sanity check | |
| (number + abs) == (abs + number) | |
| def addToTotal(a: Int)(total: Int) : Int = { | |
| total + a | |
| } | |
| def getLargestNumber(x: Int, y: Int) : Int = | |
| if (x > y) x | |
| else y | |
| // -- sanity check | |
| getLargestNumber(-1, -2) == -1 | |
| getLargestNumber(1, 2) == 2 | |
| getLargetNumber(-1, -2) == -1 | |
| // -- start external context | |
| def unknown(x: Int): Int = unknown(x) | |
| // -- end | |
| def first[A, B](a: A, b: => B): a | |
| // -- sanity check | |
| first(1, unknown(2)) | |
| def randomIntBetween(min: Int, max: Int)(seed: Long) : Int = { | |
| val r = new Random() | |
| r.setSeed(seed) | |
| min + r.nextInt(max - min) | |
| } | |
| // -- sanity check | |
| randomIntBetween(1, 2) == randomIntBetween(1, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment