0

Here is my code:

def sliding_window(seq, size, step):
    l = []
    return sliding_window_rec(seq, size, step, l)

def sliding_window_rec(seq, size, step, l):
    if len(seq) < size:
        return l
    else:
        l.append(seq[:size])
        seq = seq[step:]
        sliding_window_rec(seq, size, step, l)
    
print(sliding_window(seq, size, step))

This prints None.

If I add print(l) inside the function sliding_window_rec, it prints a list. So why does it return None and not a list when I do return l?

  • You call `sliding_window_rec` recursively, but don't return what *it* returns. – chepner Oct 23 '21 at 12:05
  • This type of recursion isn't considered good style in Python. Making the function tail-recursive isn't useful, because Python doesn't do tail-call optimization. – chepner Oct 23 '21 at 12:06
  • inside `else:` you runs `sliding_window_rec(...)` but it should be with `return` like `return sliding_window_rec(...)` – furas Oct 23 '21 at 13:55

0 Answers0