Section 1

Preview this deck

1. For this question, you need to complete the method as shown below. The method receives a parameter of a nonempty list of integers and returns true if there is a place to split the list so that the sum of the numbers on one side is equal to the sum of the numbers on the other side. For instance, given the list (1, 5, 3, 3) would return true since it is possible to split in the middle as well as 1 + 5 is equal to 3 + 3. Another example is (7, 3, 4), it also should return true because 3 + 4 = 7. Identify correct Scala statements for the lines (1) and (2). HINT) In every iteration, the firsthalf is adding an item and the secondhalf is subtracting the item for conducting the comparison between the firsthalf and the secondhalf. def balanceCheck(list: List[Int]): Boolean = { var firsthalf = 0 var secondhalf = list.sum for (i <- Range(0, list.length)){ _____________________________(1) _____________________________(2) if (firsthalf == secondhalf) { return true } } return false }

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (11)

Section 1

(11 cards)

1. For this question, you need to complete the method as shown below. The method receives a parameter of a nonempty list of integers and returns true if there is a place to split the list so that the sum of the numbers on one side is equal to the sum of the numbers on the other side. For instance, given the list (1, 5, 3, 3) would return true since it is possible to split in the middle as well as 1 + 5 is equal to 3 + 3. Another example is (7, 3, 4), it also should return true because 3 + 4 = 7. Identify correct Scala statements for the lines (1) and (2). HINT) In every iteration, the firsthalf is adding an item and the secondhalf is subtracting the item for conducting the comparison between the firsthalf and the secondhalf. def balanceCheck(list: List[Int]): Boolean = { var firsthalf = 0 var secondhalf = list.sum for (i <- Range(0, list.length)){ _____________________________(1) _____________________________(2) if (firsthalf == secondhalf) { return true } } return false }

Front

firsthalf += list(i) secondhalf -= list(i)

Back

Write a Scala statement (with using match...case stament) that print "odd" or "even" depending on any input values.

Front

a match { case x if (x%2==0) => println("even") case _ => println("odd") }

Back

When you have a list, you wish to add all elements in the list. Write a method (named as "sum") that adds all elements in the list. Please note that you need to use a recursive algorithm. scala> val nums = List(1,2,3,4,5) nums: List[Int] = List(1, 2, 3, 4, 5) scala> sum(nums) res0: Int = 15

Front

def sum(list: List[Int]):Int = list match { case Nil => 0 case n :: rest => n + sum(rest) }

Back

Write a Scala statement that creates 4 x 2 integer Array.

Front

scala> var array = Array.ofDim[Int](4,2) array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0), Array(0, 0), Array(0, 0))

Back

. Complete the following Scala method that sorts the list of values stored in the Array ("data"). You need to use a nested for-loop. def sortItems(data: Array[Int]):Array[Int] = { return (data) } scala> sortItems(Array(2,3,1,9,4)) res0: Array[Int] = Array(1, 2, 3, 4, 9)

Front

def sortItems(data: Array[Int]):Array[Int] = { for (i<- 0 until data.length){ for (j<- i+1 until data.length){ if (data(i) > data(j)) { val tmp = data(i) data(i) = data(j) data(j) = tmp } } } return (data) }

Back

When you have a list, you wish to multiply all elements in the list. Write a method (named as "multiply") that multiplies all elements in the list. Please note that you need to use a recursive algorithm. scala> val nums = List(1,2,3,4,5) nums: List[Int] = List(1, 2, 3, 4, 5) scala> multiply(nums) res0: Int = 120

Front

def multiply(list: List[Int]): Int = list match { case Nil => 1 case n :: rest => n * multiply(rest) }

Back

Complete the Scala method that converts chronological order of month information to alphabetical month information. def getMonth(month: Int):String = month match { } scala> getMonth(1) res29: String = January scala> getMonth(3) res30: String = March scala> getMonth(13) res31: String = Error

Front

def getAlphabetMonth(month: Int):String = month match { case 1 => "January" case 2 => "February" case 3 => "March" case 4 => "April" case 5 => "May" case 6 => "June" case 7 => "July" case 8 => "August" case 9 => "September" case 10 => "October" case 11 => "November" case 12 => "December" case other => "Error" }

Back

Create a Scala statement that prints all items in the Array using foreach method. scala> val temp = Array("Hi", "Hello", "Bye", "Nice") temp: Array[String] = Array(Hi, Hello, Bye, Nice) scala> _______________________________________ Hi Hello Bye Nice

Front

temp.foreach(println)

Back

Convert the following ternary operator example written in C++ to Scala statement. int largest = ((a > b) ? a : b);

Front

val largest = if (a < b) a else b

Back

Complete the following Scala method that determines the highest value from the list of values. def findMaxValue(data: List[Double]):Double = { } scala> findMaxValue(List(2, 3, 4.23, 1.24)) res0: Double = 4.23

Front

def findMaxValue(data: List[Double]):Double = { var maxValue = Double.MinValue for (item <- data) { if (item > maxValue) maxValue = item } maxValue }

Back

When creating a list ("listData") with the values of 1, 2, and 3, you faced an error. Correct it. scala> val listData = 1 :: 2 :: 3 <console>:11: error: value :: is not a member of Int val listData = 1 :: 2 :: 3

Front

val listData = 1 :: 2 :: 3 :: Nil

Back