Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2024:scala:l03 [2024/03/13 18:48] tudor.condrea Am adaugat un exercitiu pentru reverse |
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 84: | 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> | <code scala> | ||
- | def namesEmails(list: List[Str]): List[(Str, Str)] = ??? | + | def mySplit(l: Str, sep: Char): List[Str] = ??? |
</code> | </code> | ||
- | **3.2.5.** Identify the list of the employed domain names (e.g. ''gmail.com''). Remove duplicates. Use no recursion. | + | **3.2.5.** Implement a function that return the domains without the dot (ex. ''gmail''). |
<code scala> | <code scala> | ||
- | def domains(l: List[Str]): List[Str] = ??? | + | def domains(list: 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> | + | |
- | def mySplit(l: Str): List[Str] = ??? | + | |
- | </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''). | + | |
- | <code scala> | + | |
- | def domain(list: List[Str]): List[Str] = ??? | + | |
</code> | </code> | ||
Line 128: | 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 139: | 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 157: | 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> |