Overview | Schedule | Resources | Assignments | Home |
Complete the following Scala exercises, which we will start in class. Upload a file (.scala or .kojo) to Moodle containing your solutions.
def total(nums: List[Int]): Int = nums match { case Nil => case head :: tail => }
doubleAll
that takes a list of Ints and returns a list where each item has been doubled.
For example, doubleAll(3 :: 1 :: 4 :: 1 :: Nil)
should return 6 :: 2 :: 8 :: 2 :: Nil
(which
may also be written List(6, 2, 8, 2)
).filterOdd
that takes a list of Ints and returns a list of only the odd items from
the list. For example, filterOdd(List(3, 1, 4, 1, 6))
should return List(3, 1, 1)
.isLowerBound(n: Int, nums: List[Int]): Bool
that returns true if
n
is less than or equal to all of the numbers in nums
. For example,
isLowerBound(1, List(3, 1, 4))
should return true, while isLowerBound(2, List(3, 1, 4))
should
return false.splitMin(nums: List[Int]): (Int, List[Int])
that returns a pair consisting
of the smallest item from nums
and a list of all of the remaining values from nums
. For example,
splitMin(List(3, 1, 4, 1, 6))
might return (1, List(3, 4, 1, 6))
. You may assume that
nums
is non-empty; if splitMin
is called on an empty list, you may call the error
function to throw an exception: error("splitMin of an empty list")
.splitMin
function to write a selection sort: selSort(List(3, 1, 4, 1, 6))
should return
List(1, 1, 3, 4, 6)
.insert(n: Int, nums: List[Int]): List[Int]
that returns the list
nums
with n
inserted into the correct sorted position (assuming nums
was sorted).
For example, insert(3, List(1, 4, 6))
should return List(1, 3, 4, 6)
.insert
function to write an insertion sort.applyAll(nums: List[Int], fun: Int => Int): List[Int]
that returns a list where each of the items from
nums
has had fun
applied to it. For example, given def double(n: Int): Int = 2 * n
,
the call applyAll(nums, double)
should return the same as doubleAll(nums)
.filterPred(nums: List[Int], pred: Int => Bool): List[Int]
that
returns a list of only those items from nums
for which pred
(the "predicate") returns true. For
example, given def odd(n: Int): Bool = n % 2 == 1
, the call filterPred(nums, odd)
should return
the same as filterOdd(nums)
.Complete the five exercises at the end of Command2.kojo
, then
upload your resulting .kojo file. Be sure that the tests
pass for the first two exercises (and consider writing additional tests yourself).
Do the following exercises in class:
sealed trait Tree case object Leaf extends Tree case class Node(left: Tree, n: Int, right: Tree) extends Tree def insert(x: Int, t: Tree): Tree = t match { case Leaf => Node(Leaf, x, Leaf) case Node(left, n, right) => if (x > n) Node(insert(x, left), n, right) else Node(left, n, insert(x, right)) } def buildTree(nums: List[Int]): Tree = nums match { case Nil => Leaf case head :: tail => insert(head, buildTree(tail)) }
size(t: Tree): Int
that returns the number of nodes in a tree (do not count the leaves).t
such that size(t)
is N, how many leaves does it have? Prove your answer by
induction.buildTree(nums)
, in terms of the length of nums
buildTree
.inOrder(t: Tree): List[Int]
that returns the values from t
according to an inorder
traversal.inOrder(t)
, in terms of the size of t
?inOrder(buildTree(nums))
, and what is its average running time in terms of the length of
nums
?Overview | Schedule | Resources | Assignments | Home |
DePauw University,
Computer Science Department,
Fall 2011
Maintained by Brian Howard
(bhoward@depauw.edu
).
Last updated