Last active
October 11, 2018 01:37
-
-
Save jfthuong/5ef7f9def46d06e902253b578695f19e to your computer and use it in GitHub Desktop.
WPE - Week 02 - Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from typing import Dict, Generator, List | |
Range3 = Generator[int, None, None] | |
def myrange3(start: int, stop: int = None, step: int = 1) -> Range3: | |
if stop is None: | |
current, stop = 0, start | |
else: | |
current = start | |
# range returns a ValueError if step = 0 | |
if step == 0: | |
raise ValueError("myrange() step argument must not be 0") | |
# We return nothing if step is such that nothing could be reached | |
if (current < stop and step < 0) or (current > stop and step > 0): | |
return | |
while (step > 0 and current < stop) or (step < 0 and current > stop): | |
yield current | |
current += step | |
# = Solution from Reuven = | |
# def myrange3(first, second=None, step=1): | |
# if second is None: | |
# current = 0 | |
# end = first | |
# else: | |
# current = first | |
# end = second | |
# while current < end: | |
# yield current | |
# current += step | |
def myrange2(start: int, stop: int = None, step: int = 1) -> List[int]: | |
return list(myrange3(start, stop, step)) | |
def myrange2_list(start: int, stop: int = None, step: int = None) -> List[int]: | |
if stop is None: | |
start, stop = 0, start | |
if step is None: | |
step = 1 | |
list_range = list() # type: List[int] | |
while True: | |
if start < stop: | |
list_range.append(start) | |
start += step | |
else: | |
break | |
return list_range | |
def myrange2_rec(start: int, stop: int = None, step: int = None) -> List[int]: | |
# NOTE: maximum recursion depth reached for 998 calls in my case | |
if stop is None: | |
start, stop = 0, start | |
if step is None: | |
step = 1 | |
if start < stop: | |
return [start] + myrange2_rec(start + step, stop, step) | |
else: | |
return [] | |
if __name__ == "__main__": | |
for size_range, nb_times in [(100, int(1E5)), (1000, int(1E4)), (10000, 1000), (int(1E6), 10)]: | |
timing = timeit.Timer(f"for _ in range({size_range}): pass").timeit(nb_times) | |
print(f"Python3 range ({size_range}) * {nb_times} times: {timing:.4f}s") | |
timing = dict() # type: Dict[str, int] | |
for fn in ["myrange3", "myrange2", "myrange2_list", "myrange2_rec"]: | |
try: | |
t = timeit.Timer(f"for _ in {fn}({size_range}): pass", f"from solution import {fn}") | |
timing[fn] = t.timeit(nb_times) | |
print(f"{fn:>15} ({size_range}) * {nb_times} times: {timing[fn]:.4f}s") | |
except RecursionError: | |
print(f"{fn:>15} ({size_range}) * {nb_times} times: RECURSION ERROR") | |
winner = min(timing, key=timing.get) | |
print(f"==> WINNER = {winner} ({timing[winner]})") | |
print("-" * 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Got the following timings: