added final exam
This commit is contained in:
parent
ec0e1e8ae4
commit
b7ca6610fc
@ -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
|
||||
|
36
src/Final1/Exercise1.scala
Normal file
36
src/Final1/Exercise1.scala
Normal 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))
|
||||
}
|
||||
}
|
10
src/Final1/Exercise2.scala
Normal file
10
src/Final1/Exercise2.scala
Normal 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)
|
||||
})
|
||||
}
|
||||
}
|
55
src/Final1/Exercise3.scala
Normal file
55
src/Final1/Exercise3.scala
Normal 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
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user