Skip to content

Instantly share code, notes, and snippets.

@Araly
Created January 19, 2021 16:53
Show Gist options
  • Save Araly/1b6657e6c134e310d42cbbc3fe1317ed to your computer and use it in GitHub Desktop.
Save Araly/1b6657e6c134e310d42cbbc3fe1317ed to your computer and use it in GitHub Desktop.
### Fibonacci function (wrapper and helper) without loops, recusive, in O(n) complexity
def fibonacci(n):
if n < 0:
raise Exception("negative input", n)
else:
return fibonacci_helper(n, 0, 0, 0)
def fibonacci_helper(n, index, term_1, term_2):
if index == n:
return term_1
elif index == 0:
return fibonacci_helper(n, index + 1, 1, 0)
else:
return fibonacci_helper(n, index + 1, term_1 + term_2, term_1)
print(fibonacci(50))
### Merge function
def merge(original, add, delete):
# copying original so it's not edited
result = []
for item in original:
result.append(item)
# adding
for item in add:
result.append(item)
# removing duplicates
result = list(dict.fromkeys(result))
# removing
for item in delete:
result.remove(item)
# sort by length, and by alphabetical
for i in range(len(result)):
result[i] = [len(result[i]), result[i]]
result.sort(key = lambda x: (x[0], x[1]), reverse = True)
# cleanup
for i in range(len(result)):
result[i] = result[i][1]
return result
print(merge(['one', 'two', 'three'], ['one', 'two', 'five', 'six'], ['two', 'five']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment