The problem is about writing a recursive function, "def EhMensuravel (target, weight)" that determines whether it is possible to measure the value desired target with a given set of weights on a twin-pan balance. The weights available are stored in the list "weights". Remebering that the weights can be placed on the plate opposite that is occupied by the target weight, on the same plate of the target weight or be not used.
The code I made is that, but something is wrong.
weights = [1, 2, 3]
matrix = []
def createaMatrix():
for i in range(len(weights)):
matrix.append([])
for j in range(3):
for i in range(len(weights)):
if j==0:
matrix[j].append(weights[i])
if j==1:
matrix[j].append(-weights[i])
if j==2:
matrix[j].append(0)
createMatrix()
def EhMensuravel(entry, weights, final_weight=0):
if final_weight == entry:
return True
for j in range(3):
for i in range(len(weight)):
final_weight += matrix[i][j]
return EhMensuravel(entry, weight[1:], final_weight)
EDIT: For example, When I try print EhMensuravel(4, weights), the output is:
>>>
1
2
3
None
>>>
forloops in yourEhMensuravelfunctions. This means it will only ever usej = 0andi = 0(unlesslen(weights)is 0, in which case the function will returnNoneas the inner loop will not run).