Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pp:2024:scala:l03 [2024/03/13 17:11] tudor.condrea created |
pp:2024:scala:l03 [2024/03/13 19:27] (current) tudor.condrea Am modificat increment sa ia predicat si am scos percentage pentru a da mai mult timp la merge sort |
||
---|---|---|---|
Line 7: | Line 7: | ||
==== 3.1. Common list operations ==== | ==== 3.1. Common list operations ==== | ||
- | **3.1.1.** Write a function which returns true if a list of integers has at least k elements. Use patterns. Write a second function which returns true if the list has at least k elements that satisfy a predicate. | + | **3.1.1.** Write a function which returns true if a list of integers has at least ''k'' elements. Use **patterns**. Write a second function which returns ''true'' if the list has at least ''k'' elements that satisfy a predicate. |
<code scala> | <code scala> | ||
- | def atLeastk(k: Int, l: List[Int]): Boolean = | + | def atLeastk(k: Int, l: List[Int]): Boolean = { |
if (k == 0) ??? | if (k == 0) ??? | ||
else ??? | else ??? | ||
- | } | + | } |
| | ||
def atLeastkPred(pred: Int => Boolean)(k: Int, l: List[Int]): Boolean = ??? | def atLeastkPred(pred: Int => Boolean)(k: Int, l: List[Int]): Boolean = ??? | ||
</code> | </code> | ||
- | **3.1.2.** Write a function which returns the first ''n'' elements from a given list. The function should not be implemented as tail-recursive. | + | **3.1.2.** Write a function which returns the first ''n'' elements from a given list. The function should **not** be implemented as tail-recursive. |
<code scala> | <code scala> | ||
Line 42: | Line 42: | ||
def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? | def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? | ||
// part(_%2 == 0)(List(1,2,3,4,5,6)) = (List(2,4,6),List(1,3,5)) | // part(_%2 == 0)(List(1,2,3,4,5,6)) = (List(2,4,6),List(1,3,5)) | ||
+ | </code> | ||
+ | |||
+ | **3.1.6.** Write a function that reverses the elements of a list. Use a ''fold'' function (determine if it should be right or left). | ||
+ | <code scala> | ||
+ | def rev(l: List[Int]): List[Int] = ??? | ||
+ | // rev(List(1,2,3,4,5,6)) = List(6,5,4,3,2,1) | ||
</code> | </code> | ||
Line 78: | Line 84: | ||
</code> | </code> | ||
- | **3.2.4.** Split the list between first names and email domains. What ingredients (auxiliary functions) are necessary? Use either a fold or a tail-recursive function in your implementation. | + | **(!) 3.2.4.** Implement a function that tokenizes a String split by a given delimiter using ''foldRight''. Try to figure out what the accumulator should do. |
- | + | ||
- | <code scala> | + | |
- | def namesEmails(list: List[Str]): List[(Str, Str)] = ??? | + | |
- | </code> | + | |
- | + | ||
- | **3.2.5.** Identify the list of the employed domain names (e.g. ''gmail.com''). Remove duplicates. Use no recursion. | + | |
- | <code scala> | + | |
- | def domains(l: List[Str]): List[Str] = ??? | + | |
- | </code> | + | |
- | + | ||
- | **(!) 3.2.6.** In some previous exercise you have, most likely, used already defined function to split the emails. Try implementing a split function using ''foldRight''. Try to figure out what the accumulator should do. | + | |
<code scala> | <code scala> | ||
- | def mySplit(l: Str): List[Str] = ??? | + | def mySplit(l: Str, sep: Char): List[Str] = ??? |
</code> | </code> | ||
- | **3.2.7.** Generalize the former function for any given character. Use it to implement a function that return the domains without the dot (ex. ''gmail''). | + | **3.2.5.** Implement a function that return the domains without the dot (ex. ''gmail''). |
<code scala> | <code scala> | ||
- | def domain(list: List[Str]): List[Str] = ??? | + | def domains(list: List[Str]): List[Str] = ??? |
</code> | </code> | ||
Line 122: | Line 117: | ||
Add this type alias to your code before solving the following exercises. | Add this type alias to your code before solving the following exercises. | ||
- | **3.3.1.** Write a function which adds one point to all students which have a passing grade (>= 5), and leaves all other grades unchanged. | + | **3.3.1.** Write a function which adds one point to all students that satisfy a given predicate (ex: grade >= 5), and leaves all other grades unchanged. |
<code scala> | <code scala> | ||
- | def increment(g: Gradebook): Gradebook = | + | def increment(g: Gradebook, p: (Str, Int) => Boolean): Gradebook = |
g.map(???) | g.map(???) | ||
</code> | </code> | ||
Line 133: | Line 128: | ||
</code> | </code> | ||
- | **3.3.3.** Write a function which takes a gradebook and returns the percentage of failed vs. passed students, as a pair (x,y). | + | **3.3.3.** Write a function which takes a gradebook and returns the list of names which have passed. Use filter and map from Scala. |
- | <code scala> | + | |
- | def percentage(g: Gradebook): (Double,Double) = ??? | + | |
- | </code> | + | |
- | + | ||
- | **3.3.4.** Write a function which takes a gradebook and returns the list of names which have passed. Use filter and map from Scala. | + | |
<code scala> | <code scala> | ||
def pass(g: Gradebook): List[Str] = ??? | def pass(g: Gradebook): List[Str] = ??? | ||
</code> | </code> | ||
- | **3.3.5.** Implement merge-sort (in ascending order) over gradebooks: | + | **3.3.4.** Implement merge-sort (in ascending order) over gradebooks: |
<code scala> | <code scala> | ||
def mergeSort(l: Gradebook): Gradebook = { | def mergeSort(l: Gradebook): Gradebook = { | ||
Line 151: | Line 141: | ||
</code> | </code> | ||
- | **3.3.6** Write a function which takes a gradebook and reports all passing students in **descending** order of their grade. | + | **3.3.5** Write a function which takes a gradebook and reports all passing students in **descending** order of their grade. |
<code scala> | <code scala> | ||
def honorsList(g: Gradebook): List[Str] = ??? | def honorsList(g: Gradebook): List[Str] = ??? | ||
</code> | </code> |