Skip to content

Commit 0dd8ad9

Browse files
committed
fix merge conflicts
2 parents 0fccba8 + f602377 commit 0dd8ad9

File tree

3 files changed

+170
-37
lines changed

3 files changed

+170
-37
lines changed

find_repeated_dna_sequences.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from time import perf_counter_ns
22

33

4-
def add_hash(ans, hash_set, hash_val, repeated_sequence):
5-
if hash_val in hash_set and repeated_sequence not in ans:
6-
ans.add(repeated_sequence)
7-
else:
8-
hash_set.add(hash_val)
4+
def create_hash(s, nucleotide_mapping, hash_val):
5+
for i in range(10):
6+
hash_val = (hash_val * 4) + nucleotide_mapping[s[i]]
7+
return {hash_val}
98

109

1110
def build_hash(s, nucleotide_mapping, start_index):
@@ -17,23 +16,31 @@ def build_hash(s, nucleotide_mapping, start_index):
1716

1817
def loop_through_hash_set(s, ans, lst_len, nucleotide_mapping, hash_set):
1918
for i in range(1, lst_len - 9):
20-
repeated_sequence = s[i : i + 10]
21-
hash_val = build_hash(s, nucleotide_mapping, i - 1)
22-
add_hash(ans, hash_set, hash_val, repeated_sequence)
19+
hash_val = (
20+
(hash_val - nucleotide_mapping[s[i - 1]] * base) * 4
21+
) + nucleotide_mapping[s[i + 9]]
22+
23+
if hash_val in hash_set:
24+
repeated_sequence = s[i : i + 10]
25+
ans.add(repeated_sequence)
26+
else:
27+
hash_set.add(hash_val)
2328

2429

2530
def find_repeated_dna_sequences(s):
2631
ans = set()
2732
lst_len = len(s)
28-
2933
if lst_len < 10:
30-
return list(ans)
34+
return []
3135

32-
nucleotide_mapping = {"A": 1, "C": 2, "G": 3, "T": 4}
36+
nucleotide_mapping = {"A": 0, "C": 1, "G": 2, "T": 3}
37+
hash_val = 0
3338

34-
hash_val = build_hash(s, nucleotide_mapping, 0)
35-
hash_set = {hash_val}
36-
loop_through_hash_set(s, ans, lst_len, nucleotide_mapping, hash_set)
39+
# Initial hash computation for first 10 characters
40+
hash_set = create_hash(s, nucleotide_mapping, hash_val)
41+
42+
# Compute rolling hash for the rest of the string
43+
compute_rolling_hash(s, lst_len, nucleotide_mapping, hash_set, ans)
3744

3845
return list(ans)
3946

@@ -43,11 +50,11 @@ def main():
4350
s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
4451
print("Benchmarking find_repeated_dna_sequences...")
4552
start_time = perf_counter_ns()
53+
4654
for _ in range(1000):
4755
ans = find_repeated_dna_sequences(s)
4856
print(ans)
4957

50-
print()
5158
end_time = perf_counter_ns()
5259
total_time = end_time - start_time
5360
print("Total time taken for 1000 iterations:", total_time, "nanoseconds")

help_queue.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
import threading
22
from typing import Optional
3+
from dataclasses import dataclass, field
34

45

6+
@dataclass
57
class Node:
6-
def __init__(self, value: int) -> None:
7-
self._padding: bytes = b"\x00" * (64 - (8 + 8))
8-
self.value: int = value
9-
self.next: Optional[Node] = None
10-
self.mutex: threading.Lock = threading.Lock()
8+
value: int
9+
next: Optional["Node"] = None
10+
mutex: threading.Lock = field(default_factory=threading.Lock, init=False)
11+
_padding: bytes = field(default=b"\x00" * (64 - (8 + 8)), init=False)
1112

1213

14+
@dataclass
1315
class Queue:
14-
def __init__(self) -> None:
15-
dummy_value: int = 0
16-
self.dummy_node: Node = Node(dummy_value)
17-
self.head: Node = self.dummy_node
18-
self.tail: Node = self.dummy_node
19-
self.mutex: threading.Lock = threading.Lock()
16+
dummy_node: Node = field(default_factory=lambda: Node(0), init=False)
17+
head: Node = field(init=False)
18+
tail: Node = field(init=False)
19+
mutex: threading.Lock = field(default_factory=threading.Lock, init=False)
2020

21-
22-
def try_remove_front(queue: Queue, front: int) -> bool:
23-
with queue.mutex:
24-
head: Node = queue.head
25-
if head.next is not None:
26-
with head.mutex:
27-
next_node: Node = head.next
28-
if next_node.value == front:
29-
head.next = next_node.next
30-
return True
31-
return False
21+
def __post_init__(self):
22+
self.head = self.dummy_node
23+
self.tail = self.dummy_node
3224

3325

3426
def help_finish_enq(queue: Queue) -> None:
@@ -47,6 +39,18 @@ def enqueue(queue: Queue, value: int) -> None:
4739
help_finish_enq(queue)
4840

4941

42+
def try_remove_front(queue: Queue, front: int) -> bool:
43+
with queue.mutex:
44+
head: Node = queue.head
45+
if head.next is not None:
46+
with head.mutex:
47+
next_node: Node = head.next
48+
if next_node.value == front:
49+
head.next = next_node.next
50+
return True
51+
return False
52+
53+
5054
def main() -> None:
5155
q: Queue = Queue()
5256
front_value: int = 10

pong.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import pygame
2+
import sys
3+
from dataclasses import dataclass
4+
5+
6+
@dataclass(frozen=True)
7+
class Settings:
8+
# Constants
9+
WIDTH, HEIGHT = 640, 480
10+
WHITE = (255, 255, 255)
11+
BLACK = (0, 0, 0)
12+
PADDLE_WIDTH, PADDLE_HEIGHT = 10, 100
13+
BALL_SIZE = 15
14+
PADDLE_SPEED = 10
15+
16+
17+
# Initialize Pygame
18+
def init_pygame():
19+
pygame.init()
20+
21+
22+
def setup_display():
23+
# Set up the display
24+
screen = pygame.display.set_mode((Settings.WIDTH, Settings.HEIGHT))
25+
pygame.display.set_caption("Pong Game")
26+
return screen
27+
28+
29+
# Create initial ball and players positions
30+
def create_ball_and_players():
31+
left_paddle = pygame.Rect(
32+
30,
33+
(Settings.HEIGHT - Settings.PADDLE_HEIGHT) // 2,
34+
Settings.PADDLE_WIDTH,
35+
Settings.PADDLE_HEIGHT,
36+
)
37+
right_paddle = pygame.Rect(
38+
Settings.WIDTH - 40,
39+
(Settings.HEIGHT - Settings.PADDLE_HEIGHT) // 2,
40+
Settings.PADDLE_WIDTH,
41+
Settings.PADDLE_HEIGHT,
42+
)
43+
ball = pygame.Rect(
44+
Settings.WIDTH // 2,
45+
Settings.HEIGHT // 2,
46+
Settings.BALL_SIZE,
47+
Settings.BALL_SIZE,
48+
)
49+
50+
return left_paddle, right_paddle, ball
51+
52+
53+
def get_keys():
54+
return pygame.key.get_pressed()
55+
56+
57+
# Function to handle player movement
58+
def player_movement(left_paddle, right_paddle):
59+
60+
keys = get_keys()
61+
62+
if keys[pygame.K_w] and left_paddle.top > 0:
63+
left_paddle.y -= Settings.PADDLE_SPEED
64+
65+
elif keys[pygame.K_s] and left_paddle.bottom < Settings.HEIGHT:
66+
left_paddle.y += Settings.PADDLE_SPEED
67+
68+
if keys[pygame.K_UP] and right_paddle.top > 0:
69+
right_paddle.y -= Settings.PADDLE_SPEED
70+
71+
elif keys[pygame.K_DOWN] and right_paddle.bottom < Settings.HEIGHT:
72+
right_paddle.y += Settings.PADDLE_SPEED
73+
74+
75+
def set_ball_speed():
76+
ball_speed_x, ball_speed_y = 10, 10
77+
return ball_speed_x, ball_speed_y
78+
79+
80+
# Function to draw objects on screen
81+
def draw_screen(left_paddle, right_paddle, ball, screen):
82+
screen.fill(Settings.BLACK)
83+
pygame.draw.rect(screen, Settings.WHITE, left_paddle)
84+
pygame.draw.rect(screen, Settings.WHITE, right_paddle)
85+
pygame.draw.ellipse(screen, Settings.WHITE, ball)
86+
pygame.display.flip()
87+
pygame.time.delay(16)
88+
89+
90+
# Main game loop
91+
def main():
92+
init_pygame()
93+
screen = setup_display()
94+
95+
left_paddle, right_paddle, ball = create_ball_and_players()
96+
ball_speed_x, ball_speed_y = set_ball_speed() # Initial ball speeds
97+
while True:
98+
for event in pygame.event.get():
99+
if event.type == pygame.QUIT:
100+
pygame.quit()
101+
sys.exit()
102+
player_movement(left_paddle, right_paddle)
103+
104+
ball.x += ball_speed_x
105+
ball.y += ball_speed_y
106+
107+
if ball.top <= 0 or ball.bottom >= Settings.HEIGHT:
108+
ball_speed_y = -ball_speed_y
109+
110+
if ball.colliderect(left_paddle) or ball.colliderect(right_paddle):
111+
ball_speed_x = -ball_speed_x
112+
113+
if ball.left <= 0 or ball.right >= Settings.WIDTH:
114+
ball_speed_x = -ball_speed_x
115+
ball.x = Settings.WIDTH // 2
116+
ball.y = Settings.HEIGHT // 2
117+
118+
draw_screen(left_paddle, right_paddle, ball, screen)
119+
120+
121+
if __name__ == "__main__":
122+
main()

0 commit comments

Comments
 (0)