Question

In: Computer Science

Scala: Write and test a program as elegantly as possible You are given a puzzle like...

Scala: Write and test a program as elegantly as possible

You are given a puzzle like this:

7 __ 10 __ 2

Each blank may be filled with a ‘+’ (plus) or ‘-’ (minus), producing a value k. For all combinations of plus and minus, find the value of k that is closest to 0.

In the above case, there are 4 combinations, each producing a different value:

7 + 10 + 2 = 19 7 + 10 - 2 = 15 7 - 10 + 2 = -1 7 - 10 - 2 = -5

Of all these combinations, the value that is closest to zero is ​-1​. So the answer is -​ 1​. If there are more than one number that is closest, print the absolute value.

Sample Input/Output:

Enter digits: 7,10,2 Value close to zero is -1

Enter digits: 1,2,3,4 Value close to zero is 0

Solutions

Expert Solution

Solution:

import scala.math.abs

object Test {
    
    
    def closestToZero(values: Array[Int]): Int = {
        
        val possibleVals = List(values(0) + values(1) + values(2), values(0) + values(1) - values(2), values(0) - values(1) - values(2), values(0) - values(1)  + values(2))
        possibleVals.sortWith((x, y) => (abs(x) < abs(y)))(0)
    }
    
   def main(args: Array[String]) {
        val scanner = new java.util.Scanner(System.in)
        print("Enter digits: ")
        val vals = scanner.nextLine().split(",").map(_.toInt)
        
        print("Closest value is: " + closestToZero(vals))
   }
}

Related Solutions

Scala: Write and test a Program as elegantly as possible Given a piece of text, create...
Scala: Write and test a Program as elegantly as possible Given a piece of text, create a histogram of letter pairs (order from high to low). For instance, for the text, “this is a good thing”, the letter pairs are: th, hi, is, is, go, oo, od, th, hi, in, and ng. (ignore a) The histogram will be: th: 2, is: 2, hi: 2 go: 1, oo: 1, od: 1, in: 1, ng: 1 Sample Input/Output: Enter text: this is...
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
Write a program to check given expression is valid or not.The expression consists of paranthsis like  [{(...
Write a program to check given expression is valid or not.The expression consists of paranthsis like  [{( if valid then convert into postfix expression and after conversion then evaluate postfix expession(using stack) and do not use build in stack. Use the c++ laguage.
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A*...
Write a Python program to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is a puzzle invented and popularized by Noyes Palmer Chapman in the 1870s. It is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order, using as few moves as possible. You are permitted to slide blocks...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves from an initial board...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Draw all possible border pieces of a puzzle, each having a different shape. Border-pieces are puzzle...
Draw all possible border pieces of a puzzle, each having a different shape. Border-pieces are puzzle pieces that have at least one smooth edge.
2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of...
2. (WordValue.scala) Write a Scala function, to compute the value of a word. The value of a word is defined as the sum of the values of the individual letters of the word with value of "a" being 1, "b" being 2 etc. Upper and lower case letters have the same value. You can assume that the word consists of just letters. Here is the signature of the function: def wordValue(s: String): Int = ??? For example, wordValue("Attitude") = 100....
You like cookies and you like chocolate. Draw a diagram to show that it is possible...
You like cookies and you like chocolate. Draw a diagram to show that it is possible for your optimal bundle to consist entirely of chocolate. Would it be correct for someone to conclude that, since you are only buying chocolate, you do not like cookies? Explain your answer carefully.
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT