Compare commits
	
		
			2 Commits
		
	
	
		
			5365eabadd
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| b7ca6610fc | |||
| ec0e1e8ae4 | 
| @@ -21,6 +21,8 @@ | |||||||
|     * [Lesson 7 - Advanced typing and infinite lists](#lesson-7---advanced-typing-and-infinite-lists) |     * [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 8 - Futures and parallel collections](#lesson-8---futures-and-parallel-collections) | ||||||
|     * [Lesson 9 - DSLs](#lesson-9---dsls) |     * [Lesson 9 - DSLs](#lesson-9---dsls) | ||||||
|  |     * [Final preparation](#final-exam-preparation) | ||||||
|  |     * [Final](#final-exam) | ||||||
|   * [Assignments](#assignments) |   * [Assignments](#assignments) | ||||||
|     * [Assignment 1 - Square root](#assignment-1---square-root) |     * [Assignment 1 - Square root](#assignment-1---square-root) | ||||||
|     * [Assignment 2 - Map-reduce](#assignment-2---map-reduce) |     * [Assignment 2 - Map-reduce](#assignment-2---map-reduce) | ||||||
| @@ -97,6 +99,13 @@ | |||||||
| [Files](src/Lesson9) | [Files](src/Lesson9) | ||||||
| - DSL | - DSL | ||||||
|  |  | ||||||
|  | ### Final exam preparation | ||||||
|  | [Files](src/FinalPrep1) | ||||||
|  |  | ||||||
|  | ### Final exam | ||||||
|  | [Files](src/Final1) | ||||||
|  |  | ||||||
|  |  | ||||||
| ## Assignments | ## Assignments | ||||||
|  |  | ||||||
| ### Assignment 1 - Square root | ### 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 | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										26
									
								
								src/FinalPrep1/Ex1.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/FinalPrep1/Ex1.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | |||||||
|  | def insertion[T](x: T, xs: List[T]): List[List[T]] = { | ||||||
|  |   return (0 to xs.length).map((i: Int) => { | ||||||
|  |     val p = xs.splitAt(i) | ||||||
|  |     p._1 ::: (x :: p._2) | ||||||
|  |   }).toList | ||||||
|  |  | ||||||
|  |   def buildInsertions(x: T, xs: List[T], before: List[T]): List[List[T]] = { | ||||||
|  |     xs match { | ||||||
|  |       case Nil => (before :+ x) :: Nil | ||||||
|  |       case head::tail => (before ::: (x :: xs)) :: buildInsertions(x, tail, before :+ head) | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   buildInsertions(x, xs, Nil) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | insertion(1, List(2,3,4)) | ||||||
|  |  | ||||||
|  | def permutation[T](xs: List[T]): List[List[T]] = { | ||||||
|  |   xs match { | ||||||
|  |     case head::tail => permutation(tail) flatMap (perm => insertion(head, perm)) | ||||||
|  |     case _ => List(xs) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | permutation(List(1,2,3)) | ||||||
							
								
								
									
										18
									
								
								src/FinalPrep1/Ex2.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/FinalPrep1/Ex2.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | import scala.math.{ceil, min, sqrt} | ||||||
|  |  | ||||||
|  | def fourSquares(n: Int): List[Tuple4[Int, Int, Int, Int]] = { | ||||||
|  |   val tups = for ( | ||||||
|  |     d: Int <- ceil(sqrt(n)).toInt to 0 by -1; | ||||||
|  |     c: Int <- min(d, ceil(sqrt(n - d*d))).toInt to 0 by -1; | ||||||
|  |     b: Int <- min(c, ceil(sqrt(n - d*d - c*c))).toInt to 0 by -1; | ||||||
|  |     a: Int <- min(b, ceil(sqrt(n - d*d - c*c - b*b))).toInt to 0 by -1 | ||||||
|  |     if (a*a + b*b + c*c + d*d == n) | ||||||
|  |   ) yield Tuple4(a, b, c, d) | ||||||
|  |  | ||||||
|  |   tups.toList | ||||||
|  | } | ||||||
|  |  | ||||||
|  | fourSquares(0)  // List(Tuple4(0, 0, 0, 0)) | ||||||
|  | fourSquares(3)  // List(Tuple4(0, 1, 1, 1)) | ||||||
|  | fourSquares(15)  // List(Tuple(1, 1, 2, 3)) | ||||||
|  | fourSquares(88)  // List(Tuple4(0, 4, 6, 6), Tuple4(2, 2, 4, 8)) | ||||||
							
								
								
									
										165
									
								
								src/FinalPrep1/Ex3.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										165
									
								
								src/FinalPrep1/Ex3.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,165 @@ | |||||||
|  | sealed abstract class Tree { | ||||||
|  |   // Additional | ||||||
|  |   def toTree(indent: String = ""): String = indent | ||||||
|  |   // End | ||||||
|  |   def eval(): Double = { | ||||||
|  |     this match { | ||||||
|  |       case Sum(l, r) => l.eval() + r.eval() | ||||||
|  |       case Var(n) => throw new RuntimeException("Cannot evaluate " + this) | ||||||
|  |       case Const(v) => v | ||||||
|  |       case Power(x, y) => Math.pow(x.eval(), y.eval()) | ||||||
|  |       case Product(x, y) => x.eval() * y.eval() | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   def simplify(): Tree = { | ||||||
|  |     this match { | ||||||
|  |       case Sum(Const(v1), Const(v2)) => Const(v1 + v2) | ||||||
|  |       case Sum(l, r) if l == r => Product(Const(2), l) | ||||||
|  |       case Product(_, Const(0)) | Product(Const(0), _) => Const(0) | ||||||
|  |       case Product(v, Const(1)) => v | ||||||
|  |       case Product(Const(1), v) => v | ||||||
|  |       case Product(Const(v1), Const(v2)) => Const(v1 * v2) | ||||||
|  |  | ||||||
|  |       // Additional | ||||||
|  |       case Sum(l, Const(0)) => l | ||||||
|  |       case Sum(Const(0), r) => r | ||||||
|  |       case Product(l, c: Const) => Product(c, l) | ||||||
|  |       case Product(Const(v1), Product(Const(v2), r)) => Product(Const(v1 * v2), r) | ||||||
|  |       case Product(Product(Const(v1), l), Const(v2)) => Product(Const(v1 * v2), l) | ||||||
|  |       case Product(Product(Const(v1), l), Product(Const(v2), r)) => Product(Const(v1 * v2), Product(l, r)) | ||||||
|  |       // End | ||||||
|  |  | ||||||
|  |       case Power(_, Const(0)) => Const(1) | ||||||
|  |       case Power(v, Const(1)) => v | ||||||
|  |       case _ => this | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   def fullSimplify(): Tree = { | ||||||
|  |     (this match { | ||||||
|  |       case Sum(l, r) => Sum(l.fullSimplify(), r.fullSimplify()) | ||||||
|  |       case Power(x, y) => Power(x.fullSimplify(), y.fullSimplify()) | ||||||
|  |       case Product(x, y) => Product(x.fullSimplify(), y.fullSimplify()) | ||||||
|  |       case _ => this | ||||||
|  |     }).simplify() | ||||||
|  |   } | ||||||
|  |   def derive(s: String): Tree = { | ||||||
|  |     val simplified = this.fullSimplify() | ||||||
|  |     (simplified match { | ||||||
|  |       case Const(_) => Const(0) | ||||||
|  |       case Product(c: Const, other) => Product(c, other.derive(s)) | ||||||
|  |       case Product(other, c: Const) => Product(other.derive(s), c) | ||||||
|  |       case Sum(l, r) => Sum(l.derive(s), r.derive(s)) | ||||||
|  |  | ||||||
|  |       // Additional | ||||||
|  |       case Product(l, r) => Sum( | ||||||
|  |         Product(l.derive(s), r), | ||||||
|  |         Product(l, r.derive(s)) | ||||||
|  |       ) | ||||||
|  |       case Power(b, Const(e)) => Product(Const(e), Power(b, Const(e - 1))) | ||||||
|  |       case Power(b, e) => Product(Product(e, Power(b, Sum(e, Const(-1)))), e.derive(s)) | ||||||
|  |       case Var(n) if n == s => Const(1) | ||||||
|  |       // End | ||||||
|  |  | ||||||
|  |       case _ => simplified | ||||||
|  |     }).fullSimplify() | ||||||
|  |   } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | case class Sum(l: Tree, r: Tree) extends Tree { | ||||||
|  |   override def toString(): String = | ||||||
|  |     l.toString() + "+" + r.toString() | ||||||
|  |  | ||||||
|  |   // Additional | ||||||
|  |   override def toTree(indent: String = ""): String = { | ||||||
|  |     (indent + "Sum(\n" | ||||||
|  |       + l.toTree(indent + "  ") + ",\n" | ||||||
|  |       + r.toTree(indent + "  ") + "\n" | ||||||
|  |       + indent + ")") | ||||||
|  |   } | ||||||
|  |   // End | ||||||
|  | } | ||||||
|  | case class Var(n: String) extends Tree { | ||||||
|  |   override def toString() = n | ||||||
|  |  | ||||||
|  |   // Additional | ||||||
|  |   override def toTree(indent: String = ""): String = { | ||||||
|  |     indent + "Var(" + n + ")" | ||||||
|  |   } | ||||||
|  |   // End | ||||||
|  | } | ||||||
|  | case class Const(v: Double) extends Tree { | ||||||
|  |   override def toString() = v.toString | ||||||
|  |   // Additional | ||||||
|  |   override def toTree(indent: String = ""): String = { | ||||||
|  |     indent + "Const(" + v + ")" | ||||||
|  |   } | ||||||
|  |   // End | ||||||
|  | } | ||||||
|  | case class Power(x: Tree, y: Tree) extends Tree { | ||||||
|  |   override def toString() = x + "^" + y | ||||||
|  |   // Additional | ||||||
|  |   override def toTree(indent: String = ""): String = { | ||||||
|  |     (indent + "Power(\n" | ||||||
|  |       + x.toTree(indent + "  ") + ",\n" | ||||||
|  |       + y.toTree(indent + "  ") + "\n" | ||||||
|  |       + indent + ")") | ||||||
|  |   } | ||||||
|  |   // End | ||||||
|  | } | ||||||
|  | case class Product(x: Tree, y: Tree) extends Tree { | ||||||
|  |   override def toString() = x + "*" + y | ||||||
|  |   // Additional | ||||||
|  |   override def toTree(indent: String = ""): String = { | ||||||
|  |     (indent + "Sum(\n" | ||||||
|  |       + x.toTree(indent + "  ") + ",\n" | ||||||
|  |       + y.toTree(indent + "  ") + "\n" | ||||||
|  |       + indent + ")") | ||||||
|  |   } | ||||||
|  |   // End | ||||||
|  | } | ||||||
|  |  | ||||||
|  | val p = Product( | ||||||
|  |   Sum( | ||||||
|  |     Const(3), | ||||||
|  |     Const(-3) | ||||||
|  |   ), | ||||||
|  |   Const(10) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | p.eval() | ||||||
|  | p.fullSimplify() | ||||||
|  |  | ||||||
|  | // 23x^3 + 6x^2 -268x + pi | ||||||
|  | val p = Sum( | ||||||
|  |   Sum( | ||||||
|  |     Sum( | ||||||
|  |       Product( | ||||||
|  |         Power( | ||||||
|  |           Var("x"), | ||||||
|  |           Const(3) | ||||||
|  |         ), | ||||||
|  |         Const(23), | ||||||
|  |       ), | ||||||
|  |       Product( | ||||||
|  |         Const(6), | ||||||
|  |         Power( | ||||||
|  |           Var("x"), | ||||||
|  |           Const(2) | ||||||
|  |         ) | ||||||
|  |       ) | ||||||
|  |     ), | ||||||
|  |     Product( | ||||||
|  |       Const(-268), | ||||||
|  |       Var("x") | ||||||
|  |     ) | ||||||
|  |   ), | ||||||
|  |   Const(Math.PI) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | p.toTree() | ||||||
|  |  | ||||||
|  | p.derive("x").toTree() | ||||||
|  |  | ||||||
|  | p.derive("x") | ||||||
|  |  | ||||||
|  | // (23x^3 + 6x^2 -268x + pi)' = 69x^2 + 12x - 268 | ||||||
		Reference in New Issue
	
	Block a user