Question

In: Computer Science

using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that...

using python without external libaries

Using integer arithmetic operators '+' and '-',

print all combinations that sum up to 'sum' by inserting the operators between digits in 'number'.

example for 'number=123456789' and 'sum = 0'

Print the output using the terminal:

Output should be exactly like this from 1 - 22

1 : +1+2-34-56+78+9=0

2 : +1-2-34+5+6+7+8+9=0

3 : +1-23-4-56-7+89=0

...

12 : -1+2+34-5-6-7-8-9=0

13 : -1+23+4+56+7-89=0

14 : -1-2+34+56-78-9=0

...

22 : -12-34+56+7-8-9=0

Solutions

Expert Solution

Code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab9;

import java.util.Scanner;

/**
*
* @author miracle
*/
public class Lab9 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter digit");
String number=sc.next();
System.out.println("Enter sum");
int sum=sc.nextInt();
insertOperators(number,sum);
}

static void verify(double sum, double previous, String number, int target, String expr) {
if (number.length() == 0) {
if (sum + previous == target) {
System.out.println(expr + " = " + target);
}
} else {
for (int i = 1; i <= number.length(); i++) {
int current = Integer.parseInt(number.substring(0, i));
String remaining = number.substring(i);
verify(sum + previous, current, remaining, target, expr + " + " + current);
verify(sum + previous, -current, remaining, target, expr + " - " + current);
}
}
}

static void insertOperators(String number, int target) {
for (int i = 1; i <= number.length(); i++) {
String current = number.substring(0, i);
verify(0, Double.parseDouble(current), number.substring(i), target, current);
}
}
}

Screenshot:


Related Solutions

C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators...
C++ OOP Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token in the expression is an operator, If the operator stack is empty or the priority of the operator is greater...
On Python a) Use format() method to print an integer value entered by the user and...
On Python a) Use format() method to print an integer value entered by the user and its cube root with two decimal places. b) Print the same values as part (a) using format() function with keyword arguments and labels number and cubeRoot as in: format(number=n,cubeRoot=cr) c) Switch the order of keyword arguments and show that this has no effect on the output.
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest): 5 *, /, % 4 +, - 3 <, <=, >, >=, !=, == 2 not 1 and 0 or 1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the...
Using the descriptions below, write a scanner for numbers, symbols, comments, arithmetic operators, and parenthesis in...
Using the descriptions below, write a scanner for numbers, symbols, comments, arithmetic operators, and parenthesis in Racket or scheme. - a number is one or more digits | zero of more digits followed by a decimal point followed by one or more digits | one or more digits followed by a decimal point followed by zero or more digits - a symbol is      one or more characters from the set [_A-Za-z] followed by zero or more characters from the...
PYTHON Let n denote an integer entered by the user. Write a program to print n...
PYTHON Let n denote an integer entered by the user. Write a program to print n multiples of 5 in the descending order, with the last number being 5. Print the average of those n multiples
Using Python def specialAverage():    Print the number of 0s and then return the average of...
Using Python def specialAverage():    Print the number of 0s and then return the average of either the positive values in the list (if the optional parameter has value 1) or the negative values in the list (if the optional parameter has value 0).    Arguments: a list of numbers : the list can contain any numeric values an integer : this integer is an optional parameter and only takes value 0 or 1, and defaults to 1 if the...
Using python. 1. How to create a dictionary that has an integer as a key and...
Using python. 1. How to create a dictionary that has an integer as a key and a byte as a value? the dictionary keys must contain integers from 0 to 255. It should be similar to UTF-8. When we enter an integer, it should return 1 byte. 2. Create a function when a user gives 1 byte, it should return the key from the previous dictionary.
Statement: For a given integer N, print all the squares of positive integers where the square...
Statement: For a given integer N, print all the squares of positive integers where the square is less than or equal to N, in ascending order. Programming Tasks: Prompt the user to input the value of N Output to the screen all squares of positive integers <= N Tests: Item Test 1 Test 2 Test 3 Inputs: 50 9 100 Outputs: 1 4 9 16 25 36 49 1 4 9 1 4 9 16 25 36 49 64 81...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT