added final exam

This commit is contained in:
Louis Heredero 2025-05-20 14:00:13 +02:00
parent ec0e1e8ae4
commit b7ca6610fc
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 107 additions and 0 deletions

View File

@ -21,6 +21,8 @@
* [Lesson 7 - Advanced typing and infinite lists](#lesson-7---advanced-typing-and-infinite-lists)
* [Lesson 8 - Futures and parallel collections](#lesson-8---futures-and-parallel-collections)
* [Lesson 9 - DSLs](#lesson-9---dsls)
* [Final preparation](#final-exam-preparation)
* [Final](#final-exam)
* [Assignments](#assignments)
* [Assignment 1 - Square root](#assignment-1---square-root)
* [Assignment 2 - Map-reduce](#assignment-2---map-reduce)
@ -100,6 +102,10 @@
### Final exam preparation
[Files](src/FinalPrep1)
### Final exam
[Files](src/Final1)
## Assignments
### Assignment 1 - Square root

View File

@ -0,0 +1,36 @@
package exercises
object Exercise1 extends App {
def dup[A](r: List[Int], l: List[A]): List[A] = {
r.zip(l).flatMap(p => {
List.fill(p._1)(p._2)
})
}
def removeDup[A](l: List[A]): List[A] = {
l match {
case head::tail => {
head::removeDup(tail.filterNot(e => e == head))
}
case _ => l
}
}
def zip[A, B](first: List[A], second: List[B]): List[(A, B)] = {
first match {
case head1::tail1 => {
second match {
case head2::tail2 => {
(head1, head2)::zip(tail1, tail2)
}
case _ => Nil
}
}
case _ => Nil
}
}
def zipWith[A, B, C](xs: List[A], ys: List[B])(f: (A, B) => C): List[C] = {
zip(xs, ys).map((p: (A, B)) => f(p._1, p._2))
}
}

View File

@ -0,0 +1,10 @@
package exercises
object Exercise2 extends App {
def gen(charSet: String, length: Int): List[String] = {
if (length <= 0) List("")
else charSet.toList.flatMap(c => {
gen(charSet, length - 1).map(pwd => c.toString + pwd)
})
}
}

View File

@ -0,0 +1,55 @@
package exercises
object Exercise3 extends App {
sealed abstract class Tree {
def isMirrorOf(other: Tree): Boolean
def isSymmetric(): Boolean
def computeDepth(): Int = {
this match {
case Empty => 1
case Node(left, _, right) => 1 + Math.max(left.computeDepth(), right.computeDepth())
}
}
def traverseBreadthFirst(): List[Int] = {
val depth: Int = computeDepth()
// Construct list of levels
def helper(tree: Tree, curDepth: Int = 0): List[List[Int]] = {
tree match {
// Add empty levels for consistent indices
case Empty => List.fill(depth - curDepth)(List.empty[Int])
case Node(left, elem, right) => {
val leftList: List[List[Int]] = helper(left, curDepth + 1)
val rightList: List[List[Int]] = helper(right, curDepth + 1)
val res: List[List[Int]] = (0 until depth - curDepth - 1).map(i => {
leftList(i) ::: rightList(i)
}).toList
// Add this level
List(elem)::res
}
}
}
helper(this).flatten
}
}
case class Node(left: Tree, elem: Int, right: Tree) extends Tree {
def isMirrorOf(other: Tree): Boolean = {
other match {
case Node(left2, _, right2) => (left isMirrorOf right2) && (right isMirrorOf left2)
case _ => false
}
}
def isSymmetric: Boolean = left isMirrorOf right
}
case object Empty extends Tree {
def isMirrorOf(other: Tree): Boolean = {
other match {
case Empty => true
case _ => false
}
}
def isSymmetric: Boolean = true
}
}