Skip to content

Commit

Permalink
Merge branch 'solutions-oct-2023' of github.com:lukestephenson-zendes…
Browse files Browse the repository at this point in the history
…k/intro-to-scala into solutions-oct-2023
  • Loading branch information
waltaro committed Oct 10, 2023
2 parents 20c92b2 + deaf076 commit a4d26c1
Showing 1 changed file with 54 additions and 33 deletions.
87 changes: 54 additions & 33 deletions src/main/scala/introcourse/level03/ListExercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ object ListExercises {
*
* Hint: Refer the construction of list
*/
def prependToList[A](x: A, xs: List[A]): List[A] = ???

def prependToList[A](x: A, xs: List[A]): List[A] = x :: xs
/**
* scala> appendToList(1, List(2, 3, 4))
* > List(2,3,4,1)
*
* Hint: Use the :+ operator
*/
def appendToList[A](x: A, xs: List[A]): List[A] = ???
def appendToList[A](x: A, xs: List[A]): List[A] = xs :+ x

/**
* `List` has an `.isEmpty` method that you can call to know whether an instance is empty or not.
Expand All @@ -69,7 +68,11 @@ object ListExercises {
* }
* ```
*/
def isEmptyList[A](xs: List[A]): Boolean = ???
def isEmptyList[A](xs: List[A]): Boolean =
xs match {
case head :: tail => false
case Nil => true
}

/**
* scala> showListSize(List(1, 2, 3))
Expand All @@ -83,7 +86,11 @@ object ListExercises {
*
* Hint: Use pattern matching, string interpolation and length
*/
def showListSize[A](xs: List[A]): String = ???
def showListSize[A](xs: List[A]): String =
xs match {
case head :: tail => s"This is a list of size ${xs.length}"
case Nil => "This is an empty list"
}

/**
* Mapping a function over a List
Expand All @@ -96,7 +103,8 @@ object ListExercises {
*
* Hint: Use .map
**/
def addNumToEach(num: Int, nums: List[Int]): List[Int] = ???
def addNumToEach(num: Int, nums: List[Int]): List[Int] =
nums.map(_ + num)

/**
* Filter a List
Expand All @@ -108,7 +116,8 @@ object ListExercises {
*
* Hint: Use .filter and '%' for mod operator
*/
def filterEven(nums: List[Int]): List[Int] = ???
def filterEven(nums: List[Int]): List[Int] =
nums.filter(_ % 2 == 0)

/**
* Folds
Expand All @@ -132,7 +141,7 @@ object ListExercises {
*
* Hint: Use .foldLeft
*/
def product(nums: List[Int]): Int = ???
def product(nums: List[Int]): Int = nums.foldLeft(1)((acc, next) => acc * next)

/**
* scala> min(List(4, 6, 1))
Expand All @@ -145,8 +154,10 @@ object ListExercises {
**/
def min(nums: List[Int]): Int =
nums match {
case Nil => ???
case head :: tail => ???
case Nil => Int.MinValue
case head :: tail => tail.foldLeft(head)(
(prev, next) => if prev < next then prev else next
)
}

private[level03] val peopleList =
Expand All @@ -170,8 +181,13 @@ object ListExercises {
*
* Hint: Use pattern matching and .foldLeft
*/
def youngestPerson(persons: List[Person]): Person = ???

def youngestPerson(persons: List[Person]): Person =
persons match {
case Nil => Person("Nobody", 0)
case head :: tail => tail.foldLeft(head) { (prevPerson, nextPerson) =>
if prevPerson.age <= nextPerson.age then prevPerson else nextPerson
}
}
/**
* Return a list of pairs of a Person and their position in the `peopleList`.
* The position should be a 1-based index.
Expand All @@ -192,7 +208,8 @@ object ListExercises {
*
* Hint: Use `zipWithIndex`
*/
def personWithIndex(people: List[Person]): List[(Person, Int)] = ???
def personWithIndex(people: List[Person]): List[(Person, Int)] =
people.zipWithIndex.map((person, i) => (person, i + 1))

/**
* Log every nth person from the `peopleList` given an index `n`.
Expand All @@ -208,7 +225,13 @@ object ListExercises {
* Hint: Use `personWithIndex`, `filter` and `showPerson`.
*
*/
def showEveryNthPerson(n: Int, persons: List[Person]): List[String] = ???
def showEveryNthPerson(n: Int, persons: List[Person]): List[String] =
if n <= 0 then
persons.map(showPerson)
else
personWithIndex(persons)
.filter((_, i) => i % n == 0)
.map((person, _) => showPerson(person))

private[level03] def showPerson(person: Person): String =
person match {
Expand All @@ -222,28 +245,17 @@ object ListExercises {
/**
* Rewrite this function that uses a mutable variable and for-loop in an immutable fashion
*/
@SuppressWarnings(Array("org.wartremover.warts.Var"))
def getNames(persons: List[Person]): List[String] = {
var names: List[String] = Nil
for (person <- persons) {
names = names :+ person.name
}
names
persons.map(_.name)
}

/**
* Rewrite this function that uses a mutable variable and for-loop in an immutable fashion
*
* Return people aged >= 18
*/
@SuppressWarnings(Array("org.wartremover.warts.Var"))
def getAdults(persons: List[Person]): List[Person] = {
var adults: List[Person] = Nil
for (person <- persons) {
if (person.age >= 18)
adults = adults :+ person
}
adults
persons.filter(_.age >= 18)
}


Expand All @@ -252,13 +264,14 @@ object ListExercises {
*
* Don't use `.reverse` because that's cheating ;)
*/
@SuppressWarnings(Array("org.wartremover.warts.Var"))
def reverseList[A](xs: List[A]): List[A] = {
var result: List[A] = Nil
for (x <- xs) {
result = x :: result
xs match {
case head :: tail =>
tail.foldLeft(List(head)){ (acc, next) =>
next :: acc
}
case Nil => Nil
}
result
}

/**
Expand All @@ -268,5 +281,13 @@ object ListExercises {
* Given: val l1 = List("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e")
* sublists(l1) == List(List("a", "a", "a", "a"), List("b"), List("c", "c"), List("a", "a"), List("d"), List("e", "e", "e", "e"))
*/
def sublists[A](xs: List[A]): List[List[A]] = ???
def sublists[A](xs: List[A]): List[List[A]] =
reverseList(
xs.foldLeft(List[List[A]]()): (list, x) =>
list match
case (head :: tail1) :: tail2 if head == x =>
(x :: head :: tail1) :: tail2
case _ =>
List(x) :: list
)
}

0 comments on commit a4d26c1

Please sign in to comment.