-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (51 loc) · 1.93 KB
/
main.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
import time
# constant
decimal = 256
prime_number = 1117
hashes, queries = [],[]
def main():
# inputs
base_string = input()
number_of_queries , query_length = list(map(int, input().split()))
base_string_length = len(base_string)
queries = [input() for i in range(number_of_queries)]
hash_function(base_string,query_length, base_string_length)
# print(hashes[3])
for i in range (number_of_queries):
query = queries[i]
index = does_dataset_have(query,query_length,base_string)
if (index != -1):
print(f"The sequence: {query} found at the position: {index + 1}")
else:
print(f"The sequence: {query} not found !")
def hash_function(text: str,query_length, base_string_length) -> None:
total_hash = 0
hash1 = 1
for i in range(query_length - 1):
hash1 = (hash1 * decimal) % prime_number
for i in range(query_length):
total_hash = (decimal * total_hash + ord(text[i])) % prime_number
hashes.append(total_hash)
for i in range((base_string_length - query_length)):
total_hash = (decimal * (total_hash - ord(text[i]) * hash1) + ord(text[i + query_length])) % prime_number
if (total_hash < 0):
total_hash += prime_number
hashes.append(total_hash)
def does_dataset_have(query: str, query_length,base_string) -> int:
local_hash = 0
for i in range(query_length):
local_hash = (decimal * local_hash + ord(query[i])) % prime_number
for i in range(len(hashes)):
if (local_hash == hashes[i]):
j = 0
while j < query_length:
if (base_string[i + j] != query[j]):
break
j+=1
if (j == query_length):
return i
return -1
if __name__ == "__main__":
start_time = time.time()
main()
print( "The program execution time in ms: " + str(int((time.time() - start_time) * 1000)))