Exercise 1: Transcribing DNA into RNA


In [1]:
def transcribe(dna):
    '''
    This function transcribes DNA to RNA. If characters other than 
    ATGCatgc- are in your DNA, this function raises an ValueError.
    An empty DNA translates to an empty RNA.
    '''
    # Raise a ValueError if invalid characters are in the DNA
    valid_chars = list('atgcATGC-')
    if not all([(char in valid_chars) for char in dna]):
        raise ValueError    
    
    # Keep the case of the DNA but delete gap characters in the RNA
    rna = ''
    rna = dna.translate(str.maketrans('Tt','Uu','-'))
    return rna

In [2]:
# Test the function with several strings
test_dnas =['atgcagaggattcc', 'ATGCATAatAggcA',
            'ATAATTTAGGCCAGATAC', 'GGGGGCCATATA',
            'ga--atg-tgaat--gtga', '', 'This is not DNA']

for dna in test_dnas:
    try:
        print('The Input: {:20} Output: {}'.format(dna, transcribe(dna)))
    except ValueError:
        print('The input "{}" has no valid transcription.'.format(dna))


The Input: atgcagaggattcc       Output: augcagaggauucc
The Input: ATGCATAatAggcA       Output: AUGCAUAauAggcA
The Input: ATAATTTAGGCCAGATAC   Output: AUAAUUUAGGCCAGAUAC
The Input: GGGGGCCATATA         Output: GGGGGCCAUAUA
The Input: ga--atg-tgaat--gtga  Output: gaaugugaauguga
The Input:                      Output: 
The input "This is not DNA" has no valid transcription.

Excercise 2: Palindrome checker


In [3]:
def is_palindrom(text):
    '''
    The function returns True if it's parameter string is a palindrom. 
    If it is not a palindrom it returns False.
    '''
    import string
    # Remove all punctuation characters and whitespace
    text = text.translate(str.maketrans("","",
                                        string.punctuation+
                                        string.whitespace))
    text = text.lower()
    reverse = text[-1::-1]
    if reverse == text:
        return True
    else:
        return False

In [4]:
# Test the function for several palindroms
test_text =['This is not a palindrom', 'No x in Nixon!',
            'UFO tofu?', 'Annagram_is_not_a_Palindrom',
            '...Anna']

for text in test_text:
    #Remove all punctuation characters for a clean output.
    if is_palindrom(text):
        print('"{}" is a palindrom'.format(text))
    else:
        print('"{}" is not a palindrom'.format(text))


"This is not a palindrom" is not a palindrom
"No x in Nixon!" is a palindrom
"UFO tofu?" is a palindrom
"Annagram_is_not_a_Palindrom" is not a palindrom
"...Anna" is a palindrom

Exercise 3: Validate a date


In [5]:
def is_leap_year(year):
    '''
    Enter a year of the gregorian calendar.
    This function returns True if the year is a leap year.
    '''
    if year % 400 == 0:
        return True
    elif year % 100 == 0:
        return False
    elif year % 4 == 0:
        return True
    else:
        return False

In [6]:
def is_valid_date(day, month, year):
    '''
    This function returns True if the parameters given 
    are from a valid date.
    '''
    days_per_month = {1:31,2:28,3:31,4:30,5:31,6:30,
                     7:31,8:31,9:30,10:31,11:30,12:31}
    months = [1,2,3,4,5,6,7,8,9,10,11,12]
    if is_leap_year(year):
        days_per_month[2] = 29
    
    if day == 0 or month == 0:
        return False
    elif month in months and day <= days_per_month[month]:
        return True
    else:
        return False

In [7]:
# Test the functions with the given dates
dates = ['31.4.2016', '0.5.2004', '31.12.1999', '29.2.2004',
        '29.2.2005','29.2.2000', '29.2.1900', '1.1.0', 
        '1.13.2017', '4.0.2016']

results = []
for date in dates:
    day, month, year = [int(x) for x in date.split('.')]
    results.append(is_valid_date(day, month, year))
    
for date, result in zip(dates, results):
    print('{} is a {} date'.format(date, result))


31.4.2016 is a False date
0.5.2004 is a False date
31.12.1999 is a True date
29.2.2004 is a True date
29.2.2005 is a False date
29.2.2000 is a True date
29.2.1900 is a False date
1.1.0 is a True date
1.13.2017 is a False date
4.0.2016 is a False date