Skip to content

Commit aa9c977

Browse files
committed
Algo - problem I
1 parent 0a4c501 commit aa9c977

File tree

1 file changed

+52
-0
lines changed
  • python/mipt/mipt_algorithms_course/exam/medium/problem i

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
This problem is in "Normal" group
3+
4+
You are researching different DNAs. DNA is a sequence of nucleotides.
5+
There are 4 types of nucleotides in DNA: cytosine [C], guanine [G], adenine [A] or thymine [T].
6+
7+
During your previous experiment you found out that you should not consider DNAs which contain at least 3 Adenine
8+
nucleotides [A] in a row.
9+
10+
Now you want to calculate amount of different DNAs of N nucleotides which you still need to consider.
11+
(Don't think about complementary sequence, just ignore complementarity rules).
12+
13+
Input format
14+
Input file contains single integer number 0 < N ≤ 30.
15+
16+
Output format
17+
Print single integer number: number of DNAs consisting of N nucleotides which doesn't have 3 As in a row.
18+
"""
19+
20+
DEBUG = True
21+
22+
23+
def func(n):
24+
dp = [[0] * 2, [0] * 2, [0] * 2]
25+
dp[0][1] = 3 # T, G, C
26+
dp[1][1] = 1 # A
27+
for i in range(2, n + 1):
28+
dp[0].append(3 * (dp[0][i - 1] + dp[1][i - 1] + dp[2][i - 1]))
29+
dp[1].append(dp[0][i - 1])
30+
dp[2].append(dp[1][i - 1])
31+
res = dp[0][n] + dp[1][n] + dp[2][n]
32+
return res
33+
34+
35+
if __name__ == '__main__':
36+
if DEBUG:
37+
res = func(1)
38+
act = 4
39+
print(f'{res}, {act}')
40+
assert act == res
41+
42+
res = func(3)
43+
act = 63
44+
print(f'{res}, {act}')
45+
assert act == res
46+
47+
res = func(10)
48+
act = 947808
49+
print(f'{res}, {act}')
50+
assert act == res
51+
else:
52+
print(func(int(input())))

0 commit comments

Comments
 (0)