Last active
December 23, 2015 05:39
-
-
Save drummer777/6588522 to your computer and use it in GitHub Desktop.
I just wanted to share my work! Thanks to everybody for helping me in this assignment =)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def seconds_difference(time_1, time_2): | |
""" (number, number) -> number | |
Return the number of seconds later that a time in seconds | |
time_2 is than a time in seconds time_1. | |
>>> seconds_difference(1800.0, 3600.0) | |
1800.0 | |
>>> seconds_difference(3600.0, 1800.0) | |
-1800.0 | |
>>> seconds_difference(1800.0, 2160.0) | |
360.0 | |
>>> seconds_difference(1800.0, 1800.0) | |
0.0 | |
""" | |
return (time_2 - time_1) | |
def hours_difference(time_1, time_2): | |
""" (number, number) -> float | |
Return the number of hours later that a time in seconds | |
time_2 is than a time in seconds time_1. | |
>>> hours_difference(1800.0, 3600.0) | |
0.5 | |
>>> hours_difference(3600.0, 1800.0) | |
-0.5 | |
>>> hours_difference(1800.0, 2160.0) | |
0.1 | |
>>> hours_difference(1800.0, 1800.0) | |
0.0 | |
""" | |
return (time_2 - time_1)/3600 | |
def to_float_hours(hours, minutes, seconds): | |
""" (int, int, int) -> float | |
Return the total number of hours in the specified number | |
of hours, minutes, and seconds. | |
Precondition: 0 <= minutes < 60 and 0 <= seconds < 60 | |
>>> to_float_hours(0, 15, 0) | |
0.25 | |
>>> to_float_hours(2, 45, 9) | |
2.7525 | |
>>> to_float_hours(1, 0, 36) | |
1.01 | |
""" | |
return(hours/1 + minutes/60 + seconds/3600) | |
def to_24_hour_clock(hours): | |
""" (number) -> number | |
hours is a number of hours since midnight. Return the | |
hour as seen on a 24-hour clock. | |
Precondition: hours >= 0 | |
>>> to_24_hour_clock(24) | |
0 | |
>>> to_24_hour_clock(48) | |
0 | |
>>> to_24_hour_clock(25) | |
1 | |
>>> to_24_hour_clock(4) | |
4 | |
>>> to_24_hour_clock(28.5) | |
4.5 | |
""" | |
return hours % 24 | |
def get_hours(seconds): | |
"""(int) -> int | |
Return the number of hours that have elapsed since midnight. | |
Precondition: seconds >= 0 | |
>>>get_hours(14400) | |
4 | |
>>>get_hours(10800) | |
3 | |
>>>get_hours(7200) | |
2 | |
>>>get_hours(3600) | |
1 | |
""" | |
return (seconds//3600)%24 | |
def get_minutes(seconds): | |
"""(int) -> int | |
Return the number of minutes that have elapsed | |
since midnight as seen on a clock. | |
Precondition: seconds >= 0 | |
>>>get_minutes(3600) | |
60 | |
>>>get_minutes(1800) | |
30 | |
>>>get_minutes(900) | |
15 | |
""" | |
return (seconds//60)%60 | |
def get_seconds(seconds): | |
""" (int) -> int | |
Return the number of seconds that have elapsed since midnight | |
as seen on a clock. | |
Precondition: seconds >= 0 | |
>>>get_seconds(3600) | |
0 | |
>>>get_seconds(60) | |
0 | |
>>>get_seconds(45) | |
45 | |
>>>get_seconds(20) | |
20 | |
""" | |
return (seconds)%60 | |
def time_to_utc(utc_offset, time): | |
""" (number, float) -> float | |
Return time at UTC+0, where utc_offset is the number of hours away from | |
UTC+0. | |
>>> time_to_utc(+0, 12.0) | |
12.0 | |
>>> time_to_utc(+1, 12.0) | |
11.0 | |
>>> time_to_utc(-1, 12.0) | |
13.0 | |
>>> time_to_utc(-11, 18.0) | |
5.0 | |
>>> time_to_utc(-1, 0.0) | |
1.0 | |
>>> time_to_utc(-1, 23.0) | |
0.0 | |
""" | |
return (-utc_offset + time)%24 | |
def time_from_utc(utc_offset, time): | |
""" (number, float) -> float | |
Return UTC time in time zone utc_offset. | |
>>> time_from_utc(+0, 12.0) | |
12.0 | |
>>> time_from_utc(+1, 12.0) | |
13.0 | |
>>> time_from_utc(-1, 12.0) | |
11.0 | |
>>> time_from_utc(+6, 6.0) | |
12.0 | |
>>> time_from_utc(-7, 6.0) | |
23.0 | |
>>> time_from_utc(-1, 0.0) | |
23.0 | |
>>> time_from_utc(-1, 23.0) | |
22.0 | |
>>> time_from_utc(+1, 23.0) | |
0.0 | |
""" | |
return (utc_offset + time)%24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
""" | |
mod_dn1=(dna1.count('A')+ dna1.count('T')+ dna1.count('C')+ dna1.count('G')) | |
mod_dn2=(dna2.count('A')+ dna2.count('T')+ dna2.count('C')+ dna2.count('G')) | |
if mod_dn1>mod_dn2: | |
return True | |
else: | |
return False | |
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 | |
""" | |
if dna2 in dna1: | |
return True | |
if dna2 not in dna1: | |
return False | |
def is_valid_sequence(dna1): | |
'''(str) -> bool | |
Return True if and only if the DNA sequence is vaild. | |
>>> is_valid_sequence('ATCG') | |
True | |
>>> is_valid_sequence('FDES') | |
False | |
''' | |
count = 0 | |
z = ['A','T','C','G'] | |
for elem in dna1: | |
if elem not in z: | |
count = count + 1 | |
return count == 0 | |
def insert_sequence(dna1, dna2, int1): | |
'''(str, str, int) -> str | |
Return the DNA sequence obtained by inserting the second DNA sequence | |
into the first DNA sequence at the given index. | |
>>> insert_sequence ('ATGC', 'TGCA', 2) | |
ATTGCAGC | |
>>> insert_sequence ('ATGC', 'TGCA', 0) | |
TGCAATGC | |
''' | |
return dna1[:int1] + dna2 + dna1[int1:] | |
def get_complement(dna): | |
'''(str) -> str | |
Return the nucleotide's complement. | |
>>> get_complement('A') | |
T | |
>>> get_complement('C') | |
G | |
''' | |
if dna == 'A': | |
return 'T' | |
elif dna == 'G': | |
return 'C' | |
if dna == 'T': | |
return 'A' | |
elif dna == 'C': | |
return 'G' | |
def get_complementary_sequence(dna): | |
'''(str) -> str | |
Return the DNA sequence that is complementary to the given DNA | |
sequence. | |
>>> get_complementary_sequence('ATA') | |
TAT | |
>>> get_complementary_sequence('CGC') | |
GCG | |
''' | |
r = "" | |
for i in dna: | |
r= r+ get_complement(i) | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment