Login and get codingYour task is to write a simple math equation solver. Given a list of operators and an integer result, find all the number combinations that can be used with operators to produce the result.

Numbers must be integers in the range of 1 to 9 and no number may be repeated in a solution. The solver should return a list of solutions, each solution a list of integers which produce the result with the given operators.

Consider the following examples:

>>> # i1 + i2 = 6

>>> list(find_all_solutions(["+"], 6))

[

[1, 5], # 1 + 5 = 6

[2, 4], # 2 + 4 = 6

[4, 2], # 4 + 2 = 6

[5, 1], # 5 + 1 = 6

# 3 + 3 is not valid, as each number can only be used once

]

>>> # i1 - i2 = 5

>>> list(find_all_solutions(["-"], 5))

[

[6, 1], # 6 - 1 = 5

[7, 2], # 7 - 2 = 5

[8, 3], # 8 - 3 = 5

[9, 4], # 9 - 4 = 5

]

>>> # i1 * i2 * i3 + i4 = 181

>>> list(find_all_solutions(["*", "*", "+"], 181))

[

[4, 5, 9, 1], # 4 * 5 * 9 + 1 = 181

[4, 9, 5, 1], # 4 * 9 * 5 + 1 = 181

[5, 4, 9, 1], # 5 * 4 * 9 + 1 = 181

[5, 9, 4, 1], # 5 * 9 * 4 + 1 = 181

[9, 4, 5, 1], # 9 * 4 * 5 + 1 = 181

[9, 5, 4, 1], # 9 * 5 * 4 + 1 = 181

]- Allowed operations are addition

`+`

, subtraction`-`

or multiplication`*`

.- Result is always an

`int`

(positive or negative).-

`i1, i2, i3 [...]`

are`int`

s between 1 and 9 (included) but can only be used once in a solution.## Task

- Write a function that receives a list of

`operators`

&`result`

and returns all possible solutions as a`list`

/ generator of a`list`

of`int`

as shown above.- Make sure to implement the order of operations correctly (multiplication before addition or subtraction).

- Using operators other than

`+`

,`-`

or`*`

should raise a`ValueError`

.- Using non

`int`

as a`result`

should raise`ValueError`

.Good luck and keep calm and code in Python!

Metrics »

25 out of 25 users completed this Bite.

Will you be Pythonista #26 to crack this Bite?

Resolution time: ~87 min. (avg. submissions of 5-240 min.)