0

Thanks for the comments all- I managed to get it working by adding 'return' to the one_counter() function. One follow on question if I may- do we always need to 'return' something for every function? Because I came across some other code that doesn't do this, here it is:

def maxSlidingWindow(nums: List[int], k: int) -> List[int]:
    d = deque()  # keep this monotonic
    
    def eat(i):
        # kick from the back all indeces of smaller numbers that came before
        while d and nums[d[-1]] <= nums[i]:  
            d.pop()
        d.append(i)
    
    def spit(i):
        # kick index out the front if still present
        if i == d[0]:  
            d.popleft()
    
    for i in range(k-1):
        # first, fill sliding window except one
        eat(i)
        
    def slide():
        # then: consume next slot -> yield max -> eject last
        for i in range(k-1, len(nums)):
            eat(i)
            yield nums[d[0]]
            spit(i-k+1)
    
    return list(slide())

Can someone please briefly explain when we use return and when we don't need it like in the above? Thanks!

Specifically when calling another function from within the same class- when do we need to have 'return' and when do we not?

if fhhf
  • 119
  • 6
  • put return before that: permutation_poss("".join(res)) – Cristofor Aug 22 '21 at 09:55
  • Because ```one_counter(nums)``` doesn't return anything. *See the else part* – Ram Aug 22 '21 at 09:58
  • ```return``` is used in the function only if ```res not in nums```. ```permutation_poss("".join(res))``` returns a string which you are not using –  Aug 22 '21 at 09:58
  • permutation_poss() will return None (implicitly) if/when permutationss is an empty iterable –  Aug 22 '21 at 10:02

0 Answers0