H
H
HasHas20152015-10-01 17:33:24
Python
HasHas2015, 2015-10-01 17:33:24

How to solve the problem in the arrangement of arithmetic signs in Python?

Good afternoon met with the following task. I saw a similar code somewhere, but now I can not find it. The essence of the problem is that you specify N numbers (and they may not be integers). The main task is to arrange the signs "+" and "-" so that in the end you get a certain number.
For example, 3 numbers are given for input: 1983, 86, 2032. It is necessary to determine for which combination they will end up with a number: 135. That is, it is necessary to determine the positions of the numbers and the arithmetic signs between them. And the input can be given about 100 numbers. Therefore, a simple enumeration is annoying.
The combination of the solution is 2032 - 1983 + 86 = 135. How can I implement this in Python code? And is it possible?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2015-10-01
@EvilsInterrupt

Recently I watched a JavaScript tutorial on habrahabr.ru. Organized as a set of posts on Habré. There was a similar solution in the section about recursion. See if this is what you need

G
GavriKos, 2015-10-01
@GavriKos

This task is purely algorithmic. Therefore, it can be solved in any language that has Turing completeness. Python is such a language.
How to implement? You draw up an algorithm on paper, then implement it, as always. No one will write the algorithm for you.
If you are confused by exhaustive search - look at genetic algorithms. Once upon a time in the university they still solved this particular problem with general algorithms - it worked great. Well, or in Google, I think there are a lot of algorithms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question