Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

n this assignment, you will write a Go program to solve a simple mathematical puzzle. The program must efficiently find the binary operators that complete

n this assignment, you will write a Go program to solve a simple mathematical puzzle. The program must efficiently find the binary operators that complete the equation. For this problem the only operators you need to consider are addition, subtraction, multiplication, and division (when the divisor is non-zero and the remainder is zero). For example, given 1 O 2 = 3 the only operator that works to put in the circle is "+". So 1 + 2 = 3 solves the problem. For simplicity we will not use the normal operator precedence. Instead there will be no precedence thus expressions are evaluated strictly from left to right. For example, given 1 O 2 O 3 = 1, your goal is to find two operators that satisfy the equation. The solution would be 1 + 2 / 3 = 1. Notice that this is computed as (1+2) / 3 = 1 and not 1 + (2/3). Generating your own example problems is easy. Write an equation (being careful to evaluate it strictly left to right) and then remove the operators and supply that to your program.

Write a Go program to do the following: Reads problems from STDIN as a list of positive integers separated by whitespace, one problem per line.

Output a solution to the problem to STDOUT that makes the equation valid. Each +, -, *, /, and = must be passed by one space on each side.

If no solution is found simply output a blank line to STDOUT. Errors in parsig STDIN exit with a non-zero status code after writing the error to STDERR.

Requirements:

Your program must use recursion to search for solutions. Specifically, your program should break the problem down into a set of smaller problems to solve. This is known as Dynamic Programming. Do not use any library other than the Go standard library. The source code must compile with the most recent version of the Go compiler. The program must not panic under any circumstances. If you program is provided with the flag -all then find all solutions to the problem. When outputting multiple solutions, separate them by a comma followed by a space.

example 1:

sample input: 3 1 4

expected output: 3+1=4

example 2:

sample input: 5 4 2 22

expected output: 5*4+2=22

With multiple solutions it looks like:

go run . -all

7+3-3=7, 7-3+3=7, 7*3/3=7 Note: please post a picture of the output in Chegg. I have seen a question like this in Chegg but that is not working. I already posted this question before but the answer is not working. please provide the correct code with the output screen shot.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions