0

Recursively implement the function halves that takes two positive integers a and b, and returns a list containing the value a (converted to type float) and all successive halves of a that are greater than b. I tried like this but it's returning an empty list and I don't understand what's going on:

def metades(a, b):
    if a < b: return []
    if a > b:

        lst = []
        a = float(a/2) 
        lst.append(a)

        return lst and metades(a,b)

print(metades(100,3))

Should return:

[100.0, 50.0, 25.0, 12.5, 6.25, 3.125]

Return:

[]

Tumes
  • 23
  • 4

2 Answers2

2

To handle a list in a recursive function, you must take it into the function's arguments:

def metades(a, b, res = None):

    res = res or []

    if a <= b: return res
    if a > b:
        res.append(a)  # put first append and then division to retrieve also first value of 'a'
        a = float(a / 2)

        return metades(a, b, res)

print(metades(100,2))

output will be:

[100, 50.0, 25.0, 12.5, 6.25, 3.125]
Giuseppe La Gualano
  • 1,491
  • 1
  • 4
  • 24
  • 2
    `a / 2` is already a `float`, no need to convert it explicitly. You can also pass it directly to `metades(a / 2, b, res)` – Yevhen Kuzmovych Nov 14 '22 at 13:42
  • 2
    Also, there's [Mutable Default Arguments](https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments) issue here. Try calling function second time – Yevhen Kuzmovych Nov 14 '22 at 13:43
  • ok nice, but now another problem has arisen, when the input is: print(metades(32,2) Should return: [32.0, 16.0, 8.0, 4.0] Return: [32, 16.0, 8.0, 4.0, 2.0] – Tumes Nov 14 '22 at 13:50
  • [@Yevhen Kuzmovych](https://stackoverflow.com/users/4727702/yevhen-kuzmovych) thank you very much! As the code is written now, if we do not cast to float, the first element will be an int. Apart from that, by putting an extra 'if to is None:' in a recursive function, aren't we extending the execution time (albeit marginally)? – Giuseppe La Gualano Nov 14 '22 at 13:50
  • you shouldn't pass empty list as default agrument in function defination – sahasrara62 Nov 14 '22 at 13:51
  • Your first element is `int` anyway as you "cast" to float (you are not actually casting, you are converting float to float) after adding the first element. As for extra `if res is None`, yes you increase execution time, but otherwise, you have an issue described in a post I've shared. Again, try calling your function a second time. – Yevhen Kuzmovych Nov 14 '22 at 13:55
  • Not an issue, but a nice syntactic sugar for this list initialization is `res = res or []` instead of the whole `if res is None:...` – Yevhen Kuzmovych Nov 14 '22 at 13:59
  • OK thank you, I edited the reply implementing the fix regarding the argument. As for the conversion I understand, it is indeed just a matter of graphical output for this question. Thanks again for the explanations. @Tumes There was an error in the direction of inequality. You can check it now. – Giuseppe La Gualano Nov 14 '22 at 14:08
  • I don't think you get what I'm saying (sorry, not trying to be disrespectful), `a / 2` is already a float, calling `float` on it is completely redundant. Your first element in the output is still an integer as you don't convert it to float before adding it to `res`. Again there is no need of `float` around `a/2`, you can have `float` around `a` when adding it to `res` (`res.append(float(a))`) so the first element is also float – Yevhen Kuzmovych Nov 15 '22 at 10:19
0
# your code goes here
def metades(a, b):
        result = []
        if a >= b:
            result.append(float(a))
            result.extend(metades(a/2, b))
        return result
    
print(metades(100,3))

output

[100, 50.0, 25.0, 12.5, 6.25, 3.125]
sahasrara62
  • 10,069
  • 3
  • 29
  • 44