Term_2_IP_problem_sheet_6
Term_2_IP_problem_sheet_6
5
4
6.1
3
2
1
Well if N is small, which it should be, this shouldn’t matter.
6.2
6.3
class HashTable {
private class Node(var word : String, var count : Int)
private val table = new Array[Node](MAX)
1
def count(x : String) : Int = {
var h = hash(x)
while (0==0) {
if (table(h).word == x) {return table(h).count}
else if (table(h).word == null) {return 0}
else {h += 1; h = h % MAX}
}
}
2
6.4
if (t == null) {
for (i <- 1 to depth) {print(".")}
println("null")
} else {
for (i <- 1 to depth) {print(".")}
println(t.word)
printTree(t.left, depth +1)
printTree(t.right, depth+1)
}
}
3
}
}
6.5
6.6
4
s.push(t.left)
s.push(t.right)
} //var: T(t) forall t in s
}
return r
}
6.7
(a) I’ve just sent in the scala file to make this easier
This is slow because we have to create every single permutation, many of which are
(b) The longest pair of words I could find using this dictionary1 was electromagnetic and
magnetoelectric.
1
https://people.sc.fsu.edu/~jburkardt/datasets/words/anagram_dictionary.txt