-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patha2(completed).py
More file actions
177 lines (105 loc) · 3.06 KB
/
a2(completed).py
File metadata and controls
177 lines (105 loc) · 3.06 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def get_length(dna):
''' (str) -> int
Return the length of the DNA sequence dna.
>>> get_length('ATCGAT')
6
>>> get_length('ATCG')
4
'''
return len(dna)
def is_longer(dna1, dna2):
''' (str, str) -> bool
Return True if and only if DNA sequence dna1 is longer than DNA sequence
dna2.
>>> is_longer('ATCG', 'AT')
True
>>> is_longer('ATCG', 'ATCGGA')
False
'''
return dna1 > dna2
def count_nucleotides(dna, nucleotide):
''' (str, str) -> int
Return the number of occurrences of nucleotide in the DNA sequence dna.
>>> count_nucleotides('ATCGGC', 'G')
2
>>> count_nucleotides('ATCTA', 'G')
0
'''
return dna.count(nucleotide)
def contains_sequence(dna1, dna2):
''' (str, str) -> bool
Return True if and only if DNA sequence dna2 occurs in the DNA sequence
dna1.
>>> contains_sequence('ATCGGC', 'GG')
True
>>> contains_sequence('ATCGGC', 'GT')
False
'''
return dna2 in dna1
def is_valid_sequence(dna):
'''(str) -> bool
Return true if and only if the DNA sequence is valid. The sequence must
only contain the characters 'A', 'T', 'C', and 'G'. Lower case characters
are not valid.
>>> is_valid_sequence('ATCG')
True
>>> is_valid_sequence('AAGCTT')
True
>>> is_valid_sequence('ATcG')
False
>>> is_valid_sequence('CTGAX')
False
'''
num_nucleotides = True
for char in dna:
if char in 'BDEFHIJKLMNOPQRSUVWXYZabcdefghijklmnopqrstuvwxyz':
num_nucleotides = False
return num_nucleotides
def insert_sequence(dna1, dna2, num):
'''(str, str, int) -> str
Return the DNA sequence obtained by inserting dna2 into dna1 at the
given index of num.
>>> insert_sequence('CCGG', 'AT', 2)
'CCATGG'
>>> insert_sequence('ATCTAGCC', 'CAT', 5)
'ATCTACATGCC'
>>>insert_sequence('ATGGC', 'TA', 1)
'ATATGGC'
'''
return dna1[:num] + dna2 + dna1[num:]
def get_complement(nucleotide):
'''(str) -> str
Return the nucletides complement.
Precondition A complements T, C compliments G
>>> get_complement('A')
'T'
>>> get_complement('C')
'G'
>>> get_complement('T')
'A'
'''
if nucleotide == 'A':
return 'T'
elif nucleotide == 'T':
return 'A'
elif nucleotide == 'C':
return 'G'
elif nucleotide == 'G':
return 'C'
def get_complementary_sequence(dna):
'''(str) -> str
Return the DNA sequence that is complimentary to the given
DNA sequence.
Precondition A complements T, C complements G
>>> get_complementary_sequence('AT')
'TA'
>>> get_complementary_sequence('TAGC')
'ATCG'
>>> get_complementary_sequence('ATCGA')
'TAGCT'
'''
dna_complement = ''
for char in dna:
if char in 'ATCG':
dna_complement = dna_complement + get_complement(char)
return dna_complement