Home > Software design >  list and recursion in Python
list and recursion in Python

Time:09-22

I dealt with a problem during a test, but I do not understand why my answer does not work. According to the statement one gives two numbers for each number the number which follows it is the same number plus the sum of the digits of this number. For this purpose, although the two sequences are with different starting points, they can end up joining. And I'm being asked to find the juncture. [![enter image description here][1]][1]

So this is my source code:

def compute_join_point(s_1, s_2):
    v_1 = s_1   sum([int(i) for i in str(s_1)])
    v_2 = s_2   sum([int(i) for i in str(s_2)])
    if v_1 == v_2:
        return v_1
    else:
        compute_join_point(v_1, v_2)

execution

s_1 = [471, 483, 498]
s_2 = [480, 492, 507]
rslt = 0
for i in s_1:
   for j in s_2: 
       rslt = compute_join_point(i, j)
print(rslt)

but I don't know how to resolve this problem [1]: https://i.stack.imgur.com/KYuh0.png

CodePudding user response:

The only problem I see in your code is that you are missing a return keyword to propagate your result when the function collapses:

def compute_join_point(s_1, s_2):
    v_1 = s_1   sum([int(i) for i in str(s_1)])
    v_2 = s_2   sum([int(i) for i in str(s_2)])
    if v_1 == v_2:
        return v_1
    else:
        return compute_join_point(v_1, v_2)
  • Related