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

Write a program in Python to print all possible combinations of phone numbers. The length of...
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
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...
Write a python function (Without using external libraries) to calculate the 1). determinate of a 2...
Write a python function (Without using external libraries) to calculate the 1). determinate of a 2 x 2 matrix 2). Calculate the inverse of a 2 x 2 matrix.
Give the value of x (x is an integer and assume integer arithmetic for all operations)....
Give the value of x (x is an integer and assume integer arithmetic for all operations). ________ 1.             x = 12 - 4 * 2 - 8; ________ 2.             x = 24 / 2 + 1 * 4; ________ 3.             x = 12 - 9 + 4 * 2 + 3 ^ 4; ________ 4.             x = 24/(3 + 2) * 4; ________ 5.             x = 2 * (5 - 3) + 7; ________ 6.             x = 12 - (15/5...
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
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT