Answer the question
In order to leave comments, you need to log in
How to identify dependencies between elements of a list?
Suppose there is a list [24, 22, 81, 4, 72, 2355, 2, 3, 2358],
How to check that there is a relationship between its elements? (sum, difference. product..)
Ie . 24 * 3 = 72
2358 = 2355 + 3?
Answer the question
In order to leave comments, you need to log in
Inefficient solution
from itertools import combinations
import operator
operators = [
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-"),
(operator.truediv, "/")
]
numbers = [24, 22, 81, 4, 72, 2355, 2, 3, 2358]
for n1, n2 in combinations(numbers, 2):
for oper in operators:
result = oper[0](n1, n2)
if result in numbers:
print("{}{}{}={}".format(n1, oper[1], n2, result))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question