Skip to content

Commit 8d44f86

Browse files
committed
first upload of code for HoT First Match
1 parent b7765d4 commit 8d44f86

File tree

9 files changed

+2246
-0
lines changed

9 files changed

+2246
-0
lines changed

python-first/chart.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from timeit import timeit
2+
3+
import matplotlib.pyplot as plt
4+
5+
from generator import find_match_gen
6+
from looping import find_match_loop
7+
from test_cases import build_list
8+
9+
TIMEIT_TIMES = 10_000
10+
LIST_SIZE = 1_000
11+
POSITION_INCREMENT = 10
12+
13+
looping_times = []
14+
generator_times = []
15+
positions = []
16+
17+
for position in range(0, LIST_SIZE, POSITION_INCREMENT):
18+
print(
19+
f"Progress {position/LIST_SIZE:.0%}",
20+
end=f"{3 * ' '}\r", # Clear previous characters and reset cursor
21+
)
22+
23+
positions.append(position)
24+
25+
list_to_search = build_list(
26+
LIST_SIZE, "Fake Python", "Real Python", position
27+
)
28+
29+
looping_times.append(
30+
timeit(
31+
"find_match_loop(list_to_search, 'Real Python', None)",
32+
globals=globals(),
33+
number=TIMEIT_TIMES,
34+
)
35+
)
36+
generator_times.append(
37+
timeit(
38+
"find_match_gen(list_to_search, 'Real Python', None)",
39+
globals=globals(),
40+
number=TIMEIT_TIMES,
41+
)
42+
)
43+
44+
print("Progress 100%")
45+
46+
fig, ax = plt.subplots()
47+
48+
plot = ax.plot(positions, looping_times)
49+
plot = ax.plot(positions, generator_times)
50+
51+
plt.xlim([0, LIST_SIZE])
52+
plt.ylim([0, max(max(looping_times), max(generator_times))])
53+
54+
plt.show()

python-first/find_first_match.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name_list = ["Ainslie", "Dieter", "Gabey", "Carena", "Allina", "Aila"]
2+
3+
SEARCHING_FOR = "Carena"
4+
5+
for name in name_list:
6+
if name == SEARCHING_FOR:
7+
print(f"Found {name}")
8+
break
9+
else:
10+
print("Not found")

python-first/from_file.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pathlib import Path
2+
3+
value = "Florina"
4+
5+
with open(Path("names.txt"), mode="r", encoding="utf-8") as f:
6+
while value not in (line := f.readline()) and line != "":
7+
pass
8+
if line != "":
9+
print(f"Found: {line.strip()}")
10+
else:
11+
print("Not found")

python-first/generator.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from test_cases import name_lists
2+
3+
4+
def find_match_gen(iterable, value, default=None):
5+
if iterable is None:
6+
return default
7+
return next((val for val in iterable if val == value), default)
8+
9+
10+
if __name__ == "__main__":
11+
target_match = "Florina"
12+
default = None
13+
for name_list in name_lists:
14+
print(find_match_gen(target_match, name_list))

python-first/looping.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from test_cases import name_lists
2+
3+
4+
def find_match_loop(iterable, value, default=None):
5+
if iterable is None:
6+
return default
7+
for val in iterable:
8+
if val == value:
9+
return val
10+
return default
11+
12+
13+
if __name__ == "__main__":
14+
target_match = "Florina"
15+
default = None
16+
for name_list in name_lists:
17+
print(find_match_loop(target_match, name_list, default))

python-first/massive_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)