-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
76 lines (56 loc) · 2.03 KB
/
sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pygame
import sys
import time
# Pygame initialization
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 400
WHITE = (255, 255, 255)
RED = (255, 0, 0)
FONT = pygame.font.Font(None, 36)
# Create the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rabin-Karp Algorithm Simulation")
def rabin_karp(text, pattern):
n = len(text)
m = len(pattern)
matches = []
# Calculate hash of the pattern
pattern_hash = sum(ord(pattern[i]) for i in range(m))
for i in range(n - m + 1):
# Calculate hash of the current substring of text
text_hash = sum(ord(text[i + j]) for j in range(m))
if text_hash == pattern_hash and text[i:i + m] == pattern:
matches.append(i)
return matches
def draw_text(text, x, y, color=WHITE):
text_surface = FONT.render(text, True, color)
screen.blit(text_surface, (x, y))
def main():
text = "ABABDABACDABABCABAB"
pattern = "ABABCABAB"
pattern_hash = sum(ord(pattern[i]) for i in range(len(pattern)))
running = True
i = 0
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
current_text = text[i:i + len(pattern)]
current_hash = sum(ord(current_text[j]) for j in range(len(current_text)))
draw_text(f"Text: {text}", 20, 20)
draw_text(f"Pattern: {pattern}", 20, 60)
draw_text(f"Checking text from position {i} to {i + len(pattern)}: {current_text}", 20, 100)
draw_text(f"Text Hash: {current_hash}", 20, 140)
if current_hash == pattern_hash and current_text == pattern:
draw_text("Pattern found!", 20, 180, RED)
pygame.display.flip()
time.sleep(1) # Sleep for 1 second to simulate the animation
i += 1
if i > len(text) - len(pattern):
i = 0
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()