-
Notifications
You must be signed in to change notification settings - Fork 0
/
FasterSymbolArray
40 lines (32 loc) · 1.25 KB
/
FasterSymbolArray
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
# Input: Strings Genome and symbol
# Output: FasterSymbolArray(Genome, symbol)
def FasterSymbolArray(Genome, symbol):
array = {}
# your code here
n = len(Genome)
ExtendedGenome = Genome + Genome[0:n//2]
# look at the first half of Genome to compute first array value
array[0] = PatternCount(symbol, Genome[0:n//2])
for i in range(1, n):
# start by setting the current array value equal to the previous array value
array[i] = array[i-1]
# the current array value can differ from the previous array value by at most 1
if ExtendedGenome[i-1] == symbol:
array[i] = array[i]-1
if ExtendedGenome[i+(n//2)-1] == symbol:
array[i] = array[i]+1
return array
# Input: Strings Text and Pattern
# Output: The number of times Pattern appears in Text
# HINT: This code should be identical to when you last implemented PatternCount
def PatternCount(symbol, ExtendedGenome):
# type your code here
count = 0
for i in range(len(ExtendedGenome) - len(symbol)+1):
if (ExtendedGenome[i:i+len(symbol)] == symbol):
count += 1
return count
### DO NOT MODIFY THE CODE BELOW THIS LINE ###
import sys
lines = sys.stdin.read().splitlines()
print(FasterSymbolArray(lines[0],lines[1]))