Skip to content

Commit a3f1573

Browse files
committed
Merge branch 'master' of https://github.com/ben-xD/leet
2 parents c6e5e99 + 0b14e23 commit a3f1573

File tree

7 files changed

+192
-1
lines changed

7 files changed

+192
-1
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@ Exiting Python solutions can be found [officially here](https://github.com/caree
44

55
### Current progress
66
- Chapter 1: 9/9
7-
- Chapter 2: some/8
7+
- Chapter 2: 6/8
8+
- Chapter 3: 8/12
9+
- Chapter 4: 8/8
10+
- Chapter 5: 1/10
11+
- Chapter 6: 0/12
12+
- Chapter 7: 9/14
13+
- Chapter 8: 0/8
14+
- Chapter 9: 2/11
815
- Chapter 10: 2/11
16+
- Chapter 11: 0/6
17+
- Chapter 12: 0/11
18+
- Chapter 13: 0/8
19+
- Chapter 14: 0/7
20+
- Chapter 15: 0/7
21+
- Chapter 16: 2/26
22+
- Chapter 17: 4/26
23+
924

1025
### Schedule
1126
- Week 1: Chapter 4: Graphs and Trees

chapter_10/p10_11.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List
2+
3+
def peaks_and_valeys(arr: List[int]) -> List[int]:
4+
for i in range(len(arr)-2):
5+
# print(f"looking at {arr[i:i+3]}")
6+
if arr[i] >= arr[i+1]:
7+
if arr[i+2] < arr[i+1]:
8+
arr[i+1], arr[i+2] = arr[i+2], arr[i+1]
9+
else:
10+
if arr[i+1] < arr[i+2]:
11+
arr[i+1], arr[i+2] = arr[i+2], arr[i+1]
12+
return arr
13+
14+
if __name__ == "__main__":
15+
exs = [[1,2,4,5,6,7], [7,6,5,4,3,2,1], [9,10,4,8,7]]
16+
for ex in exs:
17+
print(f"Peaked arr {ex}\tis\t{peaks_and_valeys(ex)}")

chapter_6/p6_1.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import random
2+
import math
3+
from typing import List
4+
5+
class Bottle:
6+
def __init__(self, pill_weight):
7+
self.pill = pill_weight
8+
9+
def populate_pills(bottle_num:int ) -> List[Bottle]:
10+
special_bottle = random.randint(0,bottle_num)
11+
print(f"Special bottle will be {special_bottle+1} (1 indexed)")
12+
return [Bottle(1.0 if i != special_bottle else 1.1) for i in range(bottle_num)]
13+
14+
def detect_special_bottle(bottles:List[Bottle]) -> int:
15+
expected_sum =0
16+
pill_pile = 0
17+
for i in range(len(bottles)):
18+
pill_pile += (i+1)* bottles[i].pill * 10
19+
expected_sum += (i+1) * 10
20+
21+
outstanding_pill = pill_pile - expected_sum
22+
# print(outstanding_pill)
23+
return math.ceil(outstanding_pill)
24+
25+
if __name__ == "__main__":
26+
number_of_bottles =20
27+
bottles = populate_pills(number_of_bottles)
28+
print(f"Special bottle is {detect_special_bottle(bottles)}")

chapter_6/p6_10.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from typing import List
2+
import random
3+
4+
BOTTLES_NUMBER = 100
5+
TEST_STRIP_NUMBER = 10
6+
MAGIC_INFECTED_BOTTLE_NUM = random.randint(0, BOTTLES_NUMBER)
7+
8+
9+
class TestStrip:
10+
11+
def __init__(self):
12+
self.samples = set()
13+
self.positive = False
14+
15+
def add_sample(self, sample_id: int):
16+
self.samples.add(sample_id)
17+
18+
19+
def test_strip(strip: TestStrip):
20+
if MAGIC_INFECTED_BOTTLE_NUM in strip.samples:
21+
strip.positive = True
22+
23+
24+
def detect_poison_bottle(bottles: List[int], tests: List[TestStrip]) -> int:
25+
if len(bottles) > len(tests) ** 2:
26+
raise Exception("Cannot test in one pass")
27+
28+
for i, bottle_id in enumerate(bottles):
29+
# Get the 10bit representation of i
30+
# use set bits to figure out which test to add_sample on
31+
for test_num, bit_mask_indicator in enumerate(map(int,f"{i:010b}")):
32+
if bit_mask_indicator:
33+
tests[test_num].add_sample(bottle_id)
34+
35+
for strip in tests:
36+
test_strip(strip)
37+
38+
# Reconstruct number from positive tests:
39+
# Pythonist one-liner
40+
# int("".join(map(str,[int(strip.positive) for strip in tests])),2)
41+
42+
# Strong C-style for loop
43+
bottle_index = 1 if tests[0].positive else 0
44+
for strip in tests[1:]:
45+
bottle_index<<=1
46+
if strip.positive:
47+
bottle_index+=1
48+
49+
return bottle_index
50+
51+
52+
if __name__ == "__main__":
53+
soda_bottles = list(range(0, BOTTLES_NUMBER))
54+
test_strips = [TestStrip() for _ in range(TEST_STRIP_NUMBER)]
55+
print(
56+
f"Poison bottle is {detect_poison_bottle(soda_bottles,test_strips)},"
57+
f" actual {MAGIC_INFECTED_BOTTLE_NUM}")

chapter_6/p6_4.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
def ants_collision_chance(num_vertex: int):
3+
# Ants not going the same way => 2 options (either left of right)
4+
# Ants are equally likely to go either way
5+
good_cases =2
6+
all_cases = 2**num_vertex
7+
8+
return (all_cases - good_cases)/ all_cases
9+
10+
11+
if __name__ == "__main__":
12+
for i in range(3,10):
13+
print(f"Chance of ant collision on {i}-vertex polygon"
14+
f" is {ants_collision_chance(i):10}, compared with {1- (1/2)**(i-1):10}")

chapter_6/p6_5.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Jug:
2+
3+
def __init__(self, capacity_liters):
4+
super().__init__()
5+
self.capacity_liters = capacity_liters
6+
self.current_fill = 0
7+
8+
def empty(self):
9+
self.current_fill = 0
10+
11+
def fill(self):
12+
self.current_fill = self.capacity_liters
13+
14+
def fill_from(self, other_jug):
15+
if self.capacity_liters - self.current_fill > other_jug.current_fill:
16+
# Pour the whole other jug
17+
self.current_fill += other_jug.current_fill
18+
other_jug.current_fill = 0
19+
else:
20+
# Just top off the current
21+
other_jug.current_fill -= self.capacity_liters - self.current_fill
22+
self.current_fill = self.capacity_liters
23+
24+
25+
if __name__ == "__main__":
26+
jug_3l = Jug(3)
27+
jug_5l = Jug(5)
28+
29+
# Create a fill of 2L in small one
30+
jug_5l.fill()
31+
jug_3l.fill_from(jug_5l)
32+
jug_3l.empty()
33+
34+
# Fill 5 in the big one, top off the small one with 1L
35+
jug_3l.fill_from(jug_5l)
36+
jug_5l.fill()
37+
jug_3l.fill_from(jug_5l)
38+
39+
print(f"Now in the big jug, we have {jug_5l.current_fill}")
40+
41+
# Progress on water quantities in the jugs
42+
# 5 - 0
43+
# 2 - 3
44+
# 2 - 0
45+
# 0 - 2
46+
# 5 - 2
47+
# 4 - 0

chapter_6/p6_9.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def shift_lockers(n:int):
2+
locker_status = [False] *n
3+
for i in range(n):
4+
for j in range(i, n, i+1):
5+
locker_status[j] = not locker_status[j]
6+
7+
# print(locker_status)
8+
for i in range(n):
9+
if locker_status[i]:
10+
print(f"Locker {i+1} still open")
11+
12+
if __name__ == "__main__":
13+
shift_lockers(100)

0 commit comments

Comments
 (0)