Compare commits

..

7 Commits

3 changed files with 152 additions and 0 deletions

90
src/Assignment3/IntSet.sc Normal file
View File

@@ -0,0 +1,90 @@
abstract class IntSet() {
def add(x: Int): IntSet
def contains(x: Int): Boolean
def foreach(f: Int => Unit): Unit
def union(other: IntSet): IntSet
def intersect(other: IntSet): IntSet
def excl(x: Int): IntSet
def +(x: Int): IntSet = this.add(x)
def -(x: Int): IntSet = this.excl(x)
}
class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet() {
def add(x: Int): IntSet = {
if (x < elem) new NonEmpty(elem, left add x, right)
else if (x > elem) new NonEmpty(elem, left, right add x)
else this
}
def contains(x: Int): Boolean =
if (x < elem) left contains x
else if (x > elem) right contains x
else true
override def toString = "(" + left + "|" + elem + "|" + right + ")"
def foreach(f: Int => Unit): Unit = {
left.foreach(f)
f(elem)
right.foreach(f)
}
def union(other: IntSet): IntSet =
this.left.union(right)
.union(other)
.add(this.elem)
def intersect(other: IntSet): IntSet = {
val base = if (other.contains(this.elem)) Empty.add(this.elem)
else Empty
base.union(this.left.intersect(other))
.union(this.right.intersect(other))
}
def excl(x: Int): IntSet = {
if (x < elem) new NonEmpty(elem, this.left.excl(x), this.right)
else if (x > elem) new NonEmpty(elem, this.left, this.right.excl(x))
else this.left.union(this.right)
}
}
object Empty extends IntSet() {
def add(x: Int): IntSet = new NonEmpty(x, Empty, Empty)
def contains(x: Int): Boolean = false
override def toString = "-"
def foreach(f: Int => Unit): Unit = {}
def union(other: IntSet): IntSet = other
def intersect(other: IntSet): IntSet = Empty
def excl(x: Int): IntSet = this
}
val t1 = Empty
val t2 = t1 add 3
val t3 = t1 add 4 add 5 add 2 add 6
t3 contains 4
println(Empty) // prints -
println(Empty.add(3)) // prints (-|3|-)
println(Empty.add(3).add(2)) // prints ((-|2|-)|3|-)
val s = Empty.add(3).add(2).add(7).add(1)
s.foreach(println)
(Empty.add(3).add(2).add(6).add(1)) foreach (x => print(x+1 + ", "))
// 2, 3, 4, 7,
// Because a BST is always sorted
val s2 = Empty.add(3).add(4).add(6).add(2)
s.union(s2)
s.intersect(s2)
s2.excl(0)
s2.excl(6)
s2.excl(2)
s2.excl(4)
val o1 = Empty + 3 + 4 + 12 + 5
val o2 = (o1 - 3 - 4)
o2 // ((-|5|-)|12|-)

21
src/Lesson3/Rational.sc Normal file
View File

@@ -0,0 +1,21 @@
class Rational(n: Int, d: Int) {
def num = n
def denom = d
}
def add(x: Rational, y: Rational): Rational =
new Rational(
x.num * y.denom + x.denom * y.num,
x.denom * y.denom
)
def stringVersion(x: Rational) = x.num + "/" + x.denom
val r1 = new Rational(1, 2)
r1.num
r1.denom
val r2 = new Rational(3, 4)
val r3 = add(r1, r2)
stringVersion(r3)

41
src/Lesson3/Rational2.sc Normal file
View File

@@ -0,0 +1,41 @@
import scala.annotation.tailrec
class Rational(n: Int, d: Int) {
require(d != 0)
@tailrec
private def gcd(x: Int, y: Int): Int =
if (y == 0) x else gcd(y, x % y)
private val g: Int = gcd(n, d)
def num: Int = n / g
def denom: Int = d / g
def +(that: Rational): Rational = new Rational(
this.num * that.denom + this.denom * that.num,
this.denom * that.denom
)
def unary_- : Rational = new Rational(-num, denom)
def <(that: Rational): Boolean = this.num * that.denom < that.num * this.denom
def max(that: Rational): Rational = if (this < that) that else this
override def toString = if (math.abs(denom) == 1) "" + denom else num + "/" + denom
}
val r1 = new Rational(1, 3)
val r2 = new Rational(2, 3)
val r3 = r1 + r2
r3
-r2
r2 < r1
r2 < r3
r2.max(r1)
r2.max(r3)