I have two batches of code that solve different problems, within both batches of code, we have functions within the same class that call one another, however one does it using 'return'
, while the other does not use 'return'
.
My question is simply, when do we use return to call functions within the same class, and when do we not?
Notice in the slide() function how the other functions are called without the use of 'return'
:
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())
Now, for the following, notice how we MUST use 'return' when calling on the one_counter() function, in order to execute the permutation_poss() function. Why is this?
def findDifferentBinaryString(nums):
def permutation_poss(poss):
permutationss = permutations(poss,len(nums[0]))
for i in permutationss:
if "".join(i) not in nums:
return "".join(i)
def one_counter(nums):
n = len(nums[0])
res = "0"*n
if res not in nums:
return res
else:
for i in range(1,len(res)+1):
res = list(res)
res[-i] = "1"
return permutation_poss("".join(res))
return one_counter(nums)