Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / slicing.py
Created August 31, 2020 11:50
To the end, Slice!
hello: str = 'Hello World!'
assert hello[6:] == 'World!'
@raeq
raeq / slicing.py
Created August 31, 2020 11:54
Ungreedy slice.
hello: str = 'Hello World!'
assert hello[:5] == 'Hello'
hello: str = 'Hello World!'
assert hello[-6:-1] == 'World'
@raeq
raeq / slicing.py
Created August 31, 2020 12:09
Working backwards.
hello: str = 'Hello World!'
assert hello[10:5:-1] == 'dlroW'
@raeq
raeq / slicing.py
Created August 31, 2020 12:14
Mix and Match
hello: str = 'Hello World!'
assert hello[-2:5:-1] == 'dlroW'
@raeq
raeq / slicing.py
Created August 31, 2020 12:19
Using a slice object.
hello: str = 'Hello World!'
sl: slice = slice(-2, 5, -1)
assert hello[sl] == 'dlroW'
@raeq
raeq / slicing.py
Created August 31, 2020 12:22
Slice objects
hello: str = 'Hello World!'
sl: slice = slice(-2, 5, -1)
print(sl.start)
print(sl.stop)
print(sl.step)
print(hello[sl])
@raeq
raeq / slicing.py
Created August 31, 2020 12:24
Slice Object
hello: str = 'Hello World!'
sl: slice = slice(-2, 5, -1)
print(f'Start: {sl.start}')
print(f'Stop: {sl.stop}')
print(f'Start: {sl.start}')
print(f'Step: {sl.step}')
print(f'Slice: {sl}')
print(hello[sl])
@raeq
raeq / slicing.py
Created August 31, 2020 12:24
Slice Object
hello: str = 'Hello World!'
sl: slice = slice(-2, 5, -1)
print(f'Start: {sl.start}')
print(f'Stop: {sl.stop}')
print(f'Step: {sl.step}')
print(f'Slice: {sl}')
print(hello[sl])
@raeq
raeq / fibonacci.py
Created September 5, 2020 08:19
Iterative solution for calculating a member of the Fibonacci sequence.
def fibonacci_iterative(target: int) -> int:
"""
This version uses iteration to start at the beginning of the sequence and work up to the target value.
It is in this way similar to fibonacci_bottomup
Luckily, the iterative version doesn't have the memory space issues, there are no deeply nested call chains here.
Unfortunately this method is slow, very slow.
>>> fibonacci_iterative(80)
23416728348467685