// Exercise set 1 problems. // Write your name, student id and the numbers of the exercises you have solved in variables // right after "object Week1 {". // Write your solutions in place of the ??? methods. Please do not change function names, // parameter types etc. as the exercises are tested automatically. Return a .scala file // that contains object Week1 including your name, student id, solvedExercises array // and the solutions. package tehtävät import scala.util.Random object Week1 { val myName: String = val myStudentid: Int = val solvedExercises: Array[Int] = Array(1,2,3,4,5,6) // for example = Array(1,2,5,6) val exercisePoints = Array(1, 1, 1, 1, 1, 1, 1, 1) // please do not touch // 1. (1p) Write the expression (3+5) * (2*6 + 1) / 2 using Int method call syntax // (for example 1.+(2)) and assign it to val ans1 val ans1: Int = (3.+(5)) * (2.*(6) + 1) / 2 println(ans1) // 2. (1p) Write a function pyth() that returns a Vector of tuples (a,b,c) of Ints such // that a <= limit, b <= limit, c <= limit and a,b,c are lengths of sides of right triangle // (c is the hypotenuse). // Use advanced for and yield - you will get the right kind of collection as result. // Hint: a, b and c must fulfill the Pythagoras identity. // Example: pyth(5) -> Vector((3,4,5), (4,3,5)) def pyth(limit: Int): IndexedSeq[(Int, Int, Int)] = for (a <- 1 to limit; b <- 1 to limit; c <- 1 to limit; if (c*c == a*a + b*b) ) yield(a,b,c) pyth(limit = 10) // 3. (1p) Write functions pow_loop() and pow_rec() that return their parameter b raised to power p. // In pow_loop(), use iteration, or loop, and in pow_rec() use recursion. Let's assume exponent is // non-negative. def pow_loop(b: Int, p: Int): Int = { var res = 1 var i = 0 while (i <= p) { res = res * b i += 1 } res } pow_loop(3,2) def pow_rec(b: Int, p: Int): Int = { def _power(result: Int, p: Int): Int = p match { case 0 => result case _ => _power(result * b, p - 1) } _power(result = 1, p) } pow_rec(3,3) // 4. (1p) Write function myShuffle() that shuffles its argument Array[Int] by looping through all // array positions i from 0 to last and returns a new shuffled array. At each round of the loop, // swap the element with a random element in the array (if parameter swapAll is true, use // Random.between(0, theArray.size), otherwise Random.between(i, theArray.size) where i is the // loop index variable). def myShuffle(numbers: Array[Int], swapAll: Boolean): Array[Int] = { var temp = 0 : Int var swap = 0 : Int var swapped = 0 : Int if (swapAll) { for (i <- 0 to numbers.length) { swap = Random.between(0, numbers.size) swapped = Random.between(0, numbers.size) temp = numbers(swap) numbers(swap) = numbers(swapped) numbers(swapped) = temp } } numbers } myShuffle(numbers = Array(1,2,3,4,5), true) // 5. (1p) Write function mean that returns the mean of elements in its parameter array. // Write also function stdev that returns the sample standard deviation of the elements in its // parameter array. Use your mean() function in the implementation. stdev is calculated by // computing the sum of (m - e(i))^2, divide it with N-1 and take square root. Here m is // the mean, e(i) the ith element and N the number of elements. See for example // https://www.mathsisfun.com/data/standard-deviation-formulas.html for a detailed explanation // (scroll down for sample standard deviation). Hint: use yield to get the squares. //var a = Array[1,2,3,4,5] def mean(a: Array[Double]): Double = { var sum = 0 : Double var length = a.length - 1 var temp = 0 : Double for (i <- 0 to length) { temp += a(i) } sum = temp / a.length sum } mean(a = Array(1,2,3,4,5)) def stdev(a: Array[Double]): Double = { val m = mean(a) val s = (for (i <- a.indices) yield scala.math.pow(a(i) - m, 2)).sum scala.math.sqrt(s) / a.length } stdev(a = Array(1,2,3,4,5)) // 6. (1p) Write function meansForPositions() that shuffles (use your shuffle function) numbers // in range 1..upper (upper included) for iters times and calculates means for each position in range. // Example: If the outcomes of shuffle calls inside the function are: // outcome 1: (3, 2, 6, 1, 4, 5), outcome 2: (2, 3, 5, 1, 4, 6), outcome 3: (1, 4, 5, 2, 6, 3) // then the function should return // (2.0, 3.0, 5.33..., 1.33..., 4.66..., 4.66...) (extra decimals cut in the example). var numbers = Array(1, 2, 3, 4, 5): Array[Int] var tempArray = Array(1, 2, 3, 4, 5): Array[Int] var tempNumbers = Array(0, 0, 0, 0, 0) : Array[Double] var upper = numbers.last def meansForPositions(upper: Int, iters: Int, swapAll: Boolean): Array[Double] = { val means = (for (_ <- Range(1, upper + 1)) yield 0.0).toArray for (i <- 1 to iters) { tempArray = myShuffle(numbers, swapAll) for (k <- tempArray.indices) { means(k) = tempArray(k) + means(k) } println(i) } for (j <- means.indices) { means(j) = means(j) / iters } means } meansForPositions(upper,3, swapAll = true) /* // 7. (1p) Using functions you wrote earlier, compare the position means for the two different // shuffling algorithm variants (swapAll = true/false). Compute the stdev for both. Which one // has lower stdev and gives more uniform result? val swapAllIsBetter: Boolean = ??? // 8. (1p) Write a function numbers(range, n) with one-line expression that can be used to generate // n unique numbers from range in sorted order. You can assume range contains at least n numbers. // Hint: a range can be converted to a list, which can be shuffled, and some numbers taken from // that. Use shuffle() in scala.util.Random for shuffling. def numbers(range: Range, n: Int): List[Int] = ??? */ } object Week1Main extends App { import Week1._ println(myName) println(myStudentid) println(solvedExercises.map(i => exercisePoints(i-1)).sum) // please do not touch // Write code to try out your solutions here. // This part is not graded. // ... println(ans1) }