Skip to content

Commit 91c6bd1

Browse files
committed
added more snippets
1 parent d345968 commit 91c6bd1

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

week6/snippets/10_comp_intro.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
my_list = [3, 15, 13, 16, 21, 20, 9, 14, 7]
2+
3+
new_list = []
4+
for num in my_list:
5+
new_list.append(num + 100)
6+
7+
print(new_list)

week6/snippets/11_comp_example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
my_list = [3, 15, 13, 16, 21, 20, 9, 14, 7]
2+
3+
new_list = [num + 100 for num in my_list]
4+
5+
print(new_list)

week6/snippets/12_comp_more.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Even numbers from 1 to 50
2+
even_50 = [num for num in range(1, 51, 2)]
3+
print(even_50)
4+
5+
# Combining the first character of each word into a string
6+
compliments = ["Crazy", "Ostentacious", "Marvelous", "Perfect", "Unbelievable", "Treasured", "Incredible", "Noble", "Grand"]
7+
secret_word = "".join([word[0] for word in compliments])
8+
print(secret_word)
9+
10+
# Nested list comprehensions to form times tables
11+
times_tables = [[i * j for j in range(1, 11)] for i in range(1, 11)]
12+
print(times_tables)

week6/snippets/13_comp_if.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cvs = [
2+
"I consider myself to be an epic gamer with only the finest fortnite skills",
3+
"I'm a highly motivated individual with at least two seconds of experience",
4+
"After touching my very first controller, I knew it was in my destiny to play games.",
5+
"I'm literally the daughter of your CEO, you're going to hire me anyways.",
6+
"Life isn't a game. I'm at the peak of my skills, with years of experience in the industry."
7+
]
8+
9+
no_gamer_cvs = [cv for cv in cvs if "game" not in cv]
10+
print(no_gamer_cvs)

week6/snippets/14_spaghetti.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
def x(a, b , c):
2+
ac = 0
3+
bc = 0
4+
cc = 0
5+
i = 0
6+
for d in a:
7+
j = 0
8+
for e in b:
9+
if j != i:
10+
j = j + 1
11+
continue
12+
k = 0
13+
for f in c:
14+
if k != j:
15+
k = k + 1
16+
continue
17+
if k == i:
18+
match y(d, e, f):
19+
case 0:
20+
ac = ac + 1
21+
case 2:
22+
cc = cc + 1 # add one to cc
23+
case _:
24+
bc += 1
25+
else:
26+
k = k + 2
27+
continue
28+
k = k + 1
29+
j = j + 1
30+
i = i + 1
31+
return y(ac, bc, cc)
32+
33+
def y(nOne, nTwo, nThree):
34+
ans = 0
35+
if ((nOne != nTwo) == True):
36+
match (not (nOne < nTwo)):
37+
case True:
38+
if nThree > nOne:
39+
ans = ans + 1
40+
ans += 1
41+
case _:
42+
ans += 1
43+
if nTwo > nThree != False:
44+
return 1
45+
else:
46+
ans += 1
47+
elif True:
48+
return 1 if nOne > nThree else 2
49+
return ans
50+
51+
print(x([5, 8, 0, 3], [4, 7, 2, 4], [9, 4, 6, 1]))
52+
53+
# what did I just read

week6/snippets/15_better.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Accepts a list containing the round scores of all pub quiz players (no dupes)
2+
# Determines the index of the player who won the most rounds (no dupes)
3+
def most_rounds_won(player_breakdown) -> int:
4+
5+
players_in_quiz = len(player_breakdown)
6+
rounds_in_quiz = len(player_breakdown[0])
7+
8+
# Regroup (transpose/zip) the 2D list to compare more easily
9+
# Example: [[1, 2, 3], [4, 5, 6]] into [[1, 4], [2, 5], [3, 6]]
10+
round_breakdown = [
11+
[scores[round_num] for scores in player_breakdown] for round_num in range(rounds_in_quiz)
12+
]
13+
14+
counts = [0 for _ in range(players_in_quiz)] # Used to store the rounds won per player index
15+
16+
for round_scores in round_breakdown:
17+
# Find player index of max value for this round, and count it
18+
max_idx = round_scores.index(max(round_scores))
19+
counts[max_idx] += 1
20+
21+
# Return the index with the most rounds won
22+
return counts.index(max(counts))
23+
24+
25+
print(most_rounds_won([[5, 8, 0, 3], [4, 7, 2, 4], [9, 4, 6, 1]]))

0 commit comments

Comments
 (0)