{"id":24362987,"url":"https://github.com/shivamka1/99-scala-problems","last_synced_at":"2026-07-19T05:33:25.148Z","repository":{"id":143929389,"uuid":"71248929","full_name":"shivamka1/99-scala-problems","owner":"shivamka1","description":"Solutions and tests for Phil Gold's S-99: Ninety-Nine Scala Problems","archived":false,"fork":false,"pushed_at":"2016-11-19T15:43:27.000Z","size":117,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-13T06:20:53.192Z","etag":null,"topics":["99probs","scala"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shivamka1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-18T13:03:50.000Z","updated_at":"2017-04-09T14:32:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"e0b3a338-1a85-41e2-8501-5b75205a5fd9","html_url":"https://github.com/shivamka1/99-scala-problems","commit_stats":null,"previous_names":["shivam-880/99-scala-problems","shivamka1/99-scala-problems"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamka1%2F99-scala-problems","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamka1%2F99-scala-problems/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamka1%2F99-scala-problems/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamka1%2F99-scala-problems/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shivamka1","download_url":"https://codeload.github.com/shivamka1/99-scala-problems/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243188805,"owners_count":20250593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["99probs","scala"],"created_at":"2025-01-18T22:54:44.924Z","updated_at":"2025-10-16T00:32:18.079Z","avatar_url":"https://github.com/shivamka1.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 99-scala-problems\nSolutions and tests for Phil Gold's [S-99: Ninety-Nine Scala Problems](http://aperiodic.net/phil/scala/s-99/).\n\n## Table of Contents\n\n* [Lists](#lists)\n* [Arithmetic](#arithmetic)\n* [Logic and Codes](#logic-and-codes)\n* [Binary Trees](#binary-trees)\n* [Multiway Trees](#multiway-trees)\n* [Graphs](#graphs)\n* [Miscellaneous](#miscellaneous)\n\n## Lists\n\n### [P01](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P01.scala) (*) Find the last element of a list.\n  ``` scala\n  scala\u003e last(List(1, 1, 2, 3, 5, 8))\n  res0: Int = 8\n  ```\n\n### [P02](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P02.scala) (*) Find the last but one element of a list.\n  ``` scala\n  scala\u003e penultimate(List(1, 1, 2, 3, 5, 8))\n  res0: Int = 5\n  ```\n\n### [P03](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P03.scala) (*) Find the Kth element of a list.\n  ``` scala\n  scala\u003e nth(2, List(1, 1, 2, 3, 5, 8))\n  res0: Int = 2\n  ```\n\n### [P04](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P04.scala) (*) Find the number of elements of a list.\n  ``` scala\n  scala\u003e length(List(1, 1, 2, 3, 5, 8))\n  res0: Int = 6\n  ```\n\n### [P05](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P05.scala) (*) Reverse a list.\n  ``` scala\n  scala\u003e reverse(List(1, 1, 2, 3, 5, 8))\n  res0: List[Int] = List(8, 5, 3, 2, 1, 1)\n  ```\n\n### [P06](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P06.scala) (*) Find out whether a list is a palindrome.\n  ``` scala\n  scala\u003e isPalindrome(List(1, 2, 3, 2, 1))\n  res0: Boolean = true\n  ```\n\n### [P07](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P07.scala) (**) Flatten a nested list structure.\n  ``` scala\n  scala\u003e flatten(List(List(1, 1), 2, List(3, List(5, 8))))\n  res0: List[Any] = List(1, 1, 2, 3, 5, 8)\n  ```\n\n### [P08](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P08.scala) (**) Eliminate consecutive duplicates of list elements.\nIf a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed. \n  ``` scala\n  scala\u003e compress(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))\n  res0: List[Symbol] = List('a, 'b, 'c, 'a, 'd, 'e)\n  ```\n\n### [P09](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P09.scala) (**) Pack consecutive duplicates of list elements into sublists.\nIf a list contains repeated elements they should be placed in separate sublists. \n  ``` scala\n  scala\u003e pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))\n  res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e))\n  ```\n\n### [P10](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P10.scala) (*) Run-length encoding of a list.\nUse the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as tuples (N, E) where N is the number of duplicates of the element E. \n  ``` scala\n  scala\u003e encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))\n  res0: List[(Int, Symbol)] = List((4,'a), (1,'b), (2,'c), (2,'a), (1,'d), (4,'e))\n  ```\n\n### [P11](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P11.scala) (*) Modified run-length encoding.\nModify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N, E) terms. \n  ``` scala\n  scala\u003e encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))\n  res0: List[Any] = List((4,'a), 'b, (2,'c), (2,'a), 'd, (4,'e))\n  ```\n\n### [P12](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P12.scala) (**) Decode a run-length encoded list.\nGiven a run-length code list generated as specified in problem P10, construct its uncompressed version. \n  ``` scala\n  scala\u003e decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e)))\n  res0: List[Symbol] = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)\n  ```\n\n### [P13](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P13.scala) (**) Run-length encoding of a list (direct solution).\nImplement the so-called run-length encoding data compression method directly. I.e. don't use other methods you've written (like P09's pack); do all the work directly. \n  ``` scala\n  scala\u003e encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))\n  res0: List[(Int, Symbol)] = List((4,'a), (1,'b), (2,'c), (2,'a), (1,'d), (4,'e))\n  ```\n\n### [P14](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P14.scala) (*) Duplicate the elements of a list.\n  ``` scala\n  scala\u003e duplicate(List('a, 'b, 'c, 'c, 'd))\n  res0: List[Symbol] = List('a, 'a, 'b, 'b, 'c, 'c, 'c, 'c, 'd, 'd)\n  ```\n\n### [P15](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P15.scala) (**) Duplicate the elements of a list a given number of times.\n  ``` scala\n  scala\u003e duplicateN(3, List('a, 'b, 'c, 'c, 'd))\n  res0: List[Symbol] = List('a, 'a, 'a, 'b, 'b, 'b, 'c, 'c, 'c, 'c, 'c, 'c, 'd, 'd, 'd)\n  ```\n\n### [P16](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P16.scala) (**) Drop every Nth element from a list.\n  ``` scala\n  scala\u003e drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))\n  res0: List[Symbol] = List('a, 'b, 'd, 'e, 'g, 'h, 'j, 'k)\n  ```\n\n### [P17](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P17.scala) (*) Split a list into two parts.\nThe length of the first part is given. Use a Tuple for your result. \n  ``` scala\n  scala\u003e split(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))\n  res0: (List[Symbol], List[Symbol]) = (List('a, 'b, 'c),List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k))\n  ```\n\n### [P18](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P18.scala) (**) Extract a slice from a list.\nGiven two indices, I and K, the slice is the list containing the elements from and including the Ith element up to but not including the Kth element of the original list. Start counting the elements with 0. \n  ``` scala\n  scala\u003e slice(3, 7, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))\n  res0: List[Symbol] = List('d, 'e, 'f, 'g)\n  ```\n\n### [P19](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P19.scala) (**) Rotate a list N places to the left.\n  ``` scala\n  scala\u003e rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))\n  res0: List[Symbol] = List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c)\n\n  scala\u003e rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))\n  res1: List[Symbol] = List('j, 'k, 'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i)\n  ```\n\n### [P20](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P20.scala) (*) Remove the Kth element from a list.\nReturn the list and the removed element in a Tuple. Elements are numbered from 0. \n  ``` scala\n  scala\u003e removeAt(1, List('a, 'b, 'c, 'd))\n  res0: (List[Symbol], Symbol) = (List('a, 'c, 'd),'b)\n  ```\n\n### [P21](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P21.scala) (*) Insert an element at a given position into a list.\n  ``` scala\n  scala\u003e insertAt('new, 1, List('a, 'b, 'c, 'd))\n  res0: List[Symbol] = List('a, 'new, 'b, 'c, 'd)\n  ```\n\n### [P22](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P22.scala) (*) Create a list containing all integers within a given range.\n  ``` scala\n  scala\u003e range(4, 9)\n  res0: List[Int] = List(4, 5, 6, 7, 8, 9)\n  ```\n\n### [P23](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P23.scala) (**) Extract a given number of randomly selected elements from a list.\n  ``` scala\n  scala\u003e randomSelect(3, List('a, 'b, 'c, 'd, 'f, 'g, 'h))\n  res0: List[Symbol] = List('e, 'd, 'a)\n  ```\n\n**Hint:** Use the solution to problem P20\n\n### [P24](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P24.scala) (*) Lotto: Draw N different random numbers from the set 1..M.\n  ``` scala\n  scala\u003e lotto(6, 49)\n  res0: List[Int] = List(23, 1, 17, 33, 21, 37)\n  ```\n\n### [P25](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P25.scala) (*) Generate a random permutation of the elements of a list.\n**Hint:** Use the solution of problem P23. \n\n  ``` scala\n  scala\u003e randomPermute(List('a, 'b, 'c, 'd, 'e, 'f))\n  res0: List[Symbol] = List('b, 'a, 'd, 'c, 'e, 'f)\n  ```\n\n### [P26](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P26.scala) (**) Generate the combinations of K distinct objects chosen from the N elements of a list.\nIn how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficient). For pure mathematicians, this result may be great. But we want to really generate all the possibilities. \n\n  ``` scala\n  scala\u003e combinations(3, List('a, 'b, 'c, 'd, 'e, 'f))\n  res0: List[List[Symbol]] = List(List('a, 'b, 'c), List('a, 'b, 'd), List('a, 'b, 'e), ...\n  ```\n\n### [P27](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P27.scala) (**) Group the elements of a set into disjoint subsets.\na) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities.\n  ``` scala\n  scala\u003e group3(List(\"Aldo\", \"Beat\", \"Carla\", \"David\", \"Evi\", \"Flip\", \"Gary\", \"Hugo\", \"Ida\"))\n  res0: List[List[List[String]]] = List(List(List(Aldo, Beat), List(Carla, David, Evi), List(Flip, Gary, Hugo, Ida)), ...\n  ```\n\nb) Generalize the above predicate in a way that we can specify a list of group sizes and the predicate will return a list of groups.\n  ``` scala\n  scala\u003e group(List(2, 2, 5), List(\"Aldo\", \"Beat\", \"Carla\", \"David\", \"Evi\", \"Flip\", \"Gary\", \"Hugo\", \"Ida\"))\n  res0: List[List[List[String]]] = List(List(List(Aldo, Beat), List(Carla, David), List(Evi, Flip, Gary, Hugo, Ida)), ...\n  ```\n\nNote that we do not want permutations of the group members; i.e. ((Aldo, Beat), ...) is the same solution as ((Beat, Aldo), ...). However, we make a difference between ((Aldo, Beat), (Carla, David), ...) and ((Carla, David), (Aldo, Beat), ...).\n\nYou may find more about this combinatorial problem in a good book on discrete mathematics under the term \"multinomial coefficients\".\n\n### [P28](https://github.com/codingkapoor/99-scala-problems/blob/master/src/main/scala/com/codingkapoor/p99/_01_lists/P28.scala) (**) Sorting a list of lists according to length of sublists.\na) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of the list according to their length. E.g. short lists first, longer lists later, or vice versa.\n  ``` scala\n  scala\u003e lsort(List(List('a, 'b, 'c), List('d, 'e), List('f, 'g, 'h), List('d, 'e), List('i, 'j, 'k, 'l), List('m, 'n), List('o)))\n  res0: List[List[Symbol]] = List(List('o), List('d, 'e), List('d, 'e), List('m, 'n), List('a, 'b, 'c), List('f, 'g, 'h), List('i, 'j, 'k, 'l))\n  ```\n\nb) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements according to their length frequency; i.e. in the default, sorting is done ascendingly, lists with rare lengths are placed, others with a more frequent length come later.\n  ``` scala\n  scala\u003e lsortFreq(List(List('a, 'b, 'c), List('d, 'e), List('f, 'g, 'h), List('d, 'e), List('i, 'j, 'k, 'l), List('m, 'n), List('o)))\n  res1: List[List[Symbol]] = List(List('i, 'j, 'k, 'l), List('o), List('a, 'b, 'c), List('f, 'g, 'h), List('d, 'e), List('d, 'e), List('m, 'n))\n  ```\n\nNote that in the above example, the first two lists in the result have length 4 and 1 and both lengths appear just once. The third and fourth lists have length 3 and there are two list of this length. Finally, the last three lists have length 2. This is the most frequent length.\n\n## Arithmetic\n\n## Logic and Codes\n\n## Binary Trees\n\n## Multiway Trees\n\n## Graphs\n\n## Miscellaneous\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivamka1%2F99-scala-problems","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshivamka1%2F99-scala-problems","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivamka1%2F99-scala-problems/lists"}