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 Prolog code which can solve any given 9x9 Sudoku puzzle. Test your implementation with at...
Write Prolog code which can solve any given 9x9 Sudoku puzzle. Test your implementation with at least 2 querries and show the results in README. Writing README carries 1 point.
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.
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
You should use Visual Studio to write and test the program from this problem.   Write a...
You should use Visual Studio to write and test the program from this problem.   Write a complete program with a for loop that (5 pts) uses proper variable types. (10 pts) uses a for loop to read in a real numbers exactly 8 times (10 pts) adds the number read in the loop to a running sum. (10 pts) Computes the average of the 8 numbers as a real number. (5 pts) Prints the correct result with 2 decimal places,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT