From b7ca6610fc1ca4d1b9f4633bc91c6df4f289d57e Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 20 May 2025 14:00:13 +0200 Subject: [PATCH] added final exam --- README.md | 6 +++++ src/Final1/Exercise1.scala | 36 +++++++++++++++++++++++++ src/Final1/Exercise2.scala | 10 +++++++ src/Final1/Exercise3.scala | 55 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/Final1/Exercise1.scala create mode 100644 src/Final1/Exercise2.scala create mode 100644 src/Final1/Exercise3.scala diff --git a/README.md b/README.md index 4ae9783..b21b34c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Final1/Exercise1.scala b/src/Final1/Exercise1.scala new file mode 100644 index 0000000..b06f892 --- /dev/null +++ b/src/Final1/Exercise1.scala @@ -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)) + } +} \ No newline at end of file diff --git a/src/Final1/Exercise2.scala b/src/Final1/Exercise2.scala new file mode 100644 index 0000000..d9188e4 --- /dev/null +++ b/src/Final1/Exercise2.scala @@ -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) + }) + } +} diff --git a/src/Final1/Exercise3.scala b/src/Final1/Exercise3.scala new file mode 100644 index 0000000..f7a47f3 --- /dev/null +++ b/src/Final1/Exercise3.scala @@ -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 + } +} \ No newline at end of file