Homework 2 done
This commit is contained in:
parent
d92e0cb72a
commit
6bd3d5ff85
@ -41,13 +41,28 @@ case class Leaf(from: Int, until: Int, maxPrevious: Float) extends Tree
|
||||
object LineOfSight extends LineOfSightInterface {
|
||||
|
||||
def lineOfSight(input: Array[Float], output: Array[Float]): Unit = {
|
||||
???
|
||||
output(0) = 0
|
||||
var i = 1
|
||||
var maxSoFar = input(0);
|
||||
while(i < input.length){
|
||||
val tanOfAngle = input(i) / i;
|
||||
if(tanOfAngle > maxSoFar) maxSoFar = tanOfAngle;
|
||||
output(i) = maxSoFar;
|
||||
i = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
/** Traverses the specified part of the array and returns the maximum angle.
|
||||
*/
|
||||
def upsweepSequential(input: Array[Float], from: Int, until: Int): Float = {
|
||||
???
|
||||
def helper(i: Int, maxAngle: Float): Float = {
|
||||
if (i < until) {
|
||||
val newMaxAngle = scala.math.max(maxAngle, input(i) / i)
|
||||
helper(i + 1, newMaxAngle)
|
||||
}
|
||||
else maxAngle
|
||||
}
|
||||
helper(from, -1)
|
||||
}
|
||||
|
||||
/** Traverses the part of the array starting at `from` and until `end`, and
|
||||
@ -60,7 +75,15 @@ object LineOfSight extends LineOfSightInterface {
|
||||
*/
|
||||
def upsweep(input: Array[Float], from: Int, end: Int,
|
||||
threshold: Int): Tree = {
|
||||
???
|
||||
if(end-from <= threshold) Leaf(from, end, upsweepSequential(input, from , end))
|
||||
else {
|
||||
val mid = (end + from)/2
|
||||
val (lt, rt) = parallel(
|
||||
upsweep(input, from, mid, threshold),
|
||||
upsweep(input, mid, end, threshold))
|
||||
|
||||
Node(lt, rt)
|
||||
}
|
||||
}
|
||||
|
||||
/** Traverses the part of the `input` array starting at `from` and until
|
||||
@ -69,7 +92,13 @@ object LineOfSight extends LineOfSightInterface {
|
||||
*/
|
||||
def downsweepSequential(input: Array[Float], output: Array[Float],
|
||||
startingAngle: Float, from: Int, until: Int): Unit = {
|
||||
???
|
||||
var maxSoFar = startingAngle
|
||||
var i = from;
|
||||
while(i < until){
|
||||
maxSoFar = Math.max(input(i) / i, maxSoFar)
|
||||
output(i) = maxSoFar
|
||||
i = i + 1
|
||||
}
|
||||
}
|
||||
|
||||
/** Pushes the maximum angle in the prefix of the array to each leaf of the
|
||||
@ -78,12 +107,20 @@ object LineOfSight extends LineOfSightInterface {
|
||||
*/
|
||||
def downsweep(input: Array[Float], output: Array[Float], startingAngle: Float,
|
||||
tree: Tree): Unit = {
|
||||
???
|
||||
tree match{
|
||||
case Leaf(from, until, maxPrevious) => downsweepSequential(input, output, startingAngle, from, until)
|
||||
case Node(lt, rt) => parallel(
|
||||
downsweep(input, output, startingAngle, lt),
|
||||
downsweep(input, output, Math.max(startingAngle, lt.maxPrevious), rt)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Compute the line-of-sight in parallel. */
|
||||
def parLineOfSight(input: Array[Float], output: Array[Float],
|
||||
threshold: Int): Unit = {
|
||||
???
|
||||
}
|
||||
}
|
||||
val t = upsweep(input, 1, output.length, threshold)
|
||||
downsweep(input, output, 0, t)
|
||||
//output(0) = 0;
|
||||
}
|
||||
}
|
||||
@ -51,7 +51,14 @@ object ParallelCountChange extends ParallelCountChangeInterface {
|
||||
* coins for the specified amount of money.
|
||||
*/
|
||||
def countChange(money: Int, coins: List[Int]): Int = {
|
||||
???
|
||||
if(money < 0) 0
|
||||
else if(money == 0) 1
|
||||
else{
|
||||
coins match{
|
||||
case Nil => 0
|
||||
case c::cs => countChange(money-c, coins) + countChange(money, cs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Threshold = (Int, List[Int]) => Boolean
|
||||
@ -60,20 +67,30 @@ object ParallelCountChange extends ParallelCountChangeInterface {
|
||||
* specified list of coins for the specified amount of money.
|
||||
*/
|
||||
def parCountChange(money: Int, coins: List[Int], threshold: Threshold): Int = {
|
||||
???
|
||||
if(threshold(money, coins) || money <= 0) countChange(money, coins)
|
||||
else {
|
||||
coins match{
|
||||
case Nil => 0
|
||||
case c::cs => {
|
||||
val (a, b) = parallel(parCountChange(money-c, coins, threshold), parCountChange(money, cs, threshold))
|
||||
a+b
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Threshold heuristic based on the starting money. */
|
||||
def moneyThreshold(startingMoney: Int): Threshold =
|
||||
???
|
||||
(x, _) => x <= (startingMoney * 2) / 3
|
||||
|
||||
/** Threshold heuristic based on the total number of initial coins. */
|
||||
def totalCoinsThreshold(totalCoins: Int): Threshold =
|
||||
???
|
||||
(_, cs) => cs.length <= (2 * totalCoins) / 3
|
||||
|
||||
|
||||
/** Threshold heuristic based on the starting money and the initial list of coins. */
|
||||
def combinedThreshold(startingMoney: Int, allCoins: List[Int]): Threshold = {
|
||||
???
|
||||
(money, remCoins) => money * remCoins.length <= (startingMoney * allCoins.length)/2
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -40,25 +40,52 @@ object ParallelParenthesesBalancing extends ParallelParenthesesBalancingInterfac
|
||||
/** Returns `true` iff the parentheses in the input `chars` are balanced.
|
||||
*/
|
||||
def balance(chars: Array[Char]): Boolean = {
|
||||
???
|
||||
var i = 0;
|
||||
var count = 0;
|
||||
while(i < chars.length){
|
||||
if(chars(i) == '(') count = count + 1
|
||||
else if(chars(i) == ')') count = count - 1
|
||||
|
||||
if(count < 0) return false
|
||||
i = i + 1
|
||||
}
|
||||
if(count != 0) return false else true
|
||||
}
|
||||
|
||||
/** Returns `true` iff the parentheses in the input `chars` are balanced.
|
||||
*/
|
||||
def parBalance(chars: Array[Char], threshold: Int): Boolean = {
|
||||
|
||||
def traverse(idx: Int, until: Int, arg1: Int, arg2: Int) /*: ???*/ = {
|
||||
???
|
||||
def traverse(idx: Int, until: Int, unmatchedOpening: Int, unmatchedClosing: Int) : (Int, Int) = {
|
||||
if(idx >= until) (unmatchedOpening, unmatchedClosing)
|
||||
else chars(idx) match {
|
||||
case '(' => traverse(idx+1, until, unmatchedOpening + 1, unmatchedClosing)
|
||||
case ')' =>
|
||||
if(unmatchedOpening != 0) traverse(idx+1, until, unmatchedOpening - 1, unmatchedClosing)
|
||||
else traverse(idx+1, until, unmatchedOpening, unmatchedClosing+1)
|
||||
case _ => traverse(idx+1, until, unmatchedOpening, unmatchedClosing)
|
||||
}
|
||||
}
|
||||
|
||||
def reduce(from: Int, until: Int) /*: ???*/ = {
|
||||
???
|
||||
def reduce(from: Int, until: Int) : (Int,Int) = {
|
||||
|
||||
if(until - from <= threshold) traverse(from, until, 0, 0)
|
||||
else{
|
||||
val mid = (from + until) / 2
|
||||
val ((auo, auc), (buo, buc)) = parallel(
|
||||
reduce(from, mid),
|
||||
reduce(mid, until)
|
||||
)
|
||||
|
||||
val canMatch = Math.min(auo, buc)
|
||||
(auo+buo-canMatch, auc + buc - canMatch)
|
||||
}
|
||||
}
|
||||
|
||||
reduce(0, chars.length) == ???
|
||||
reduce(0, chars.length) == (0, 0)
|
||||
}
|
||||
|
||||
// For those who want more:
|
||||
// Prove that your reduction operator is associative!
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user