added final exam preparation
This commit is contained in:
parent
5365eabadd
commit
ec0e1e8ae4
@ -97,6 +97,9 @@
|
||||
[Files](src/Lesson9)
|
||||
- DSL
|
||||
|
||||
### Final exam preparation
|
||||
[Files](src/FinalPrep1)
|
||||
|
||||
## Assignments
|
||||
|
||||
### Assignment 1 - Square root
|
||||
|
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
|
Loading…
x
Reference in New Issue
Block a user