0

Given the following code, I've been asked to calculate how many different tuples (aliasing tuples are to be excluded) are in the returned tuple of the function:

def tuple_maker(k):
    result = tuple()
    length = len(result)
    for i in range(k):
        result = (i,result * i)
        length += len(result)
    print(length)
    return result

res = tuple_maker(4)

This is the value of res variable:

(3, (2, (1, (0, ()), 1, (0, ())), 2, (1, (0, ()), 1, (0, ())), 2, (1, (0, ()), 1, (0, ()))))

The given answer is 5 different tuples of which I was correct, but the second question was to calculate the total length of the tuple in res variable. The code above prints 8, while the answer is 14.

What do I miss here?

NiceGuy
  • 11
  • 1
  • adjust your lenght calculation inside the for loop to: length += len(result)+i – tetris programming Jan 21 '23 at 14:06
  • Care to elaborate why ? – NiceGuy Jan 21 '23 at 18:12
  • actually i am not sure from where you get the value 14? Do you have further examples/results. For example for tuple_maker(5) ? – tetris programming Jan 21 '23 at 18:17
  • Sadly, I only have the numeric answer for the question with '4' as input. I see that adding 'i' to calculation will fix the problem but I can't seem to understand the why – NiceGuy Jan 21 '23 at 18:30
  • mightve been just a coincidence. unfortunatly i cannot comprehend why the result has to be 14. because when i count all the inputs in your output it equals to 23 :) If you can give me a further explanation why the result has to be 14 then i can dig into it. otherwise further help wont be possible – tetris programming Jan 21 '23 at 18:39

0 Answers0