Travis: Python 1

From Computational Statistics (CSE383M and CS395T)
Jump to navigation Jump to search

Before class problems

Problems and 1 and 2 were trivial and required simple commands typed directly into the command window, so I will begin posting my solutions starting with problem 3.

3: Strings and Lists

Given: A string s of length at most 200 letters and four integers a, b, c and d.

Return: The slice of this string from indices a through b and c through d (with space in between), inclusively.

s =  'pKuZOvlZ0te6PQJXvNMnRdfjhhvkcLLcLystrophislSW0p1zSob7WgOzbJg7ZleYmycterizansMAPfnHuDJ8p4bGPzoTtTT3QXCsrXq3zvszmcv
Rm3NIK5ZFdEykIGEntemYeurzVOZg6FSgEEohgY9seh2xYypBiVBXAIHZJEIKUEJNqVV8Vrm7.'
a = 32
b = 41
c = 65
d = 75
news = s[a:b+1] + ' ' + s[c:d+1]
print news
Output: Lystrophis mycterizans

4: Conditions and Loops

Given: Two positive integers a and b (a<b<10000).

Return: The sum of all odd integers from a through b, inclusively.

b = 9157
a = 4750
i = a
sum = 0 
while i <= b:
   if i%2 == 1:
      sum += i
   i += 1
print sum
Ouput: 15326616

5: Working with Files

Given: A file containing at most 1000 lines.

Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.

f = open('/Users/travissanders/Desktop/Rosalind/rosalind_ini5.txt','r')
count = 0
for line in f:
        count += 1
        if count%2 == 0:
                  print line.strip()
Output: Some things in life are bad, they can really make you mad
Other things just make you swear and curse
When you're chewing on life's gristle, don't grumble give a whistle
This will help things turn out for the best
Always look on the bright side of life
Always look on the right side of life
If life seems jolly rotten, there's something you've forgotten
And that's to laugh and smile and dance and sing
When you're feeling in the dumps, don't be silly, chumps
Just purse your lips and whistle, that's the thing
So, always look on the bright side of death
Just before you draw your terminal breath
Life's a counterfeit and when you look at it
Life's a laugh and death's the joke, it's true
You see, it's all a show, keep them laughing as you go
Just remember the last laugh is on you
Always look on the bright side of life
And always look on the right side of life
Always look on the bright side of life
And always look on the right side of life

In Class Problems

Counting DNA Nucleotides

Given: A DNA string s of length at most 1000 nt.

Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in s.

dna='GGGCTGCTGACGTGAGGAAAACGCTTCCTCGCATTAAGACTTAGAGTGACCTACCATTGCACAACAGGGTGGCCCGACGCCCGACTGATCGTTCCTCGTAGGGATATTAACGTAAAACTAATA
TCTGTGGCTTAGCGTACAGAGCGAACCCAAAACGCCAAGGATATTCCGTGGCATTTCTAGCAGACGCAAATGTCGCAACGTCATTATTCGGCGGTTTGTTGCACGTCAATGAGTTGGCTTGAACT
GATCGAGGTGCCACATAAATAAATAACGTTGCCGGCCCCTACTTTAGGCTTGCCAGCTTAAAACTTTCGCACCTGCCAGACACTAAAATCGCTCGTGTATGATGACAGTGTCGACAATCTTTATGATTAA
AGAAGTCGTAGGGGCGATTTACCTAAGCCGCAGTTTTTTCAAAATGTTTGCGCTGTGCAGGCTGATCCGTGATATTATCTCCCCGCTTGGTCACACCACACGGAGCTCAGTATTTCTGCCGTTGGTAGA
AAGGTCTGCGCTGACTGGAACACTCTGTAAACCTATTTCTAGACGCCCGGTGATGCATTGGGGATACTTGGCGTCGTGAAGGATGAAGGGGCCATCTAGGAGGTCAACGACAAATCCTCGTCAGGAT
ACCCCGGAGGACATCCCTGGTATATACAAATAACGACCTAGCCGACTAACGTTCGAAGTCTCACCCGAAGCAAGACTGGAGTGAACCTGCTGTTGGGACTATGATCTCATAACGTGCGGGGTTACTAG
CCCTTGCGACCCGGGAGACTAGCTTGATTCCTGATTTAGCAGGCCATTACAAGCGAACTATCCTATGGTACATGCAGAGTTATCACGGTCTTGGAGGGTCCAAGGCGCCGGTCGACTTTGCTGCTCAA
CCCCTGTTAGGAGTAATAAGCGTCAGCCGAGTGCAATGTCCAACGAATTCCGAGAGTTACGGTATCCTTGACTCCAGTTTCTTTGAGCACT'
acount = 0
ccount = 0
gcount = 0
tcount = 0
for i in range(len(dna)):
       if dna[i] == 'A':
              acount += 1
       elif dna[i] == 'C':
              ccount += 1
       elif dna[i] == 'G':
              gcount += 1 
       elif dna[i] == 'T':
              tcount += 1
print acount, ccount, gcount, tcount
Output:249 241 246 246

Transcribing DNA into RNA

Given: A DNA string t having length at most 1000 nt.

Return: The transcribed RNA string of t.

dna='GCAGGGCCTAGACAGACCGGAAGTCAATGTAGTATTTAGCCTTGGTAAGATTACTTACGGACTGGTTGGACTAGCATGGACTCAGTCGGGCCCTAATAGTGGACTCTTTGGCTTTGACACGC
GAGATAGCGGCCCAAGCCGTTAGAGCGAGACAAACAACCAGCGCTTCCCTGCAGGGGTTACGTACTTTCTTCGCTCTGCGGATATTATCGCTGCGGAACTAGGCATGATGACTGCCAAATCGGGCA
GGAACCCTGGGAGGTATATCATACTGCCCGCCTCAGTCCGAAACAATCCGTGCATAGGTTTCTTTACGTCCCTTTGTATCCTTGAGTGTATGCTCGCAAGAGCCAGACGACATCCAGAGATAACCTA
GCCTCGCTCCCCGCGACTATCCACAAGTCTAATGGTGCGGCCAAAAAAAGAGCAGTTCATAGCGCAGGGGTTGCCATGTAGCCCCGCCCCCAGGTTAGAGACAATCGTTTGAGTATAGACAGTTCT
ACCCACTAAGACGCCGGGGCCATTGCTCGGGCCACATTGCGAGTGACATAATTATGGGCTATGAACCAAAATGAGTCAACGCCCTTGTGAGAAGAGGCATCTGCTAGTAGCGTCTGGACTTTACAC
GTAAACCGGGGGCCCGCTGGGAGAAATTGTTTCCCATCATCATAGAATCACTTAGTAACTCGTCGACCCGTTCGCTGATATCGCGGCACAATCTCGAACCTGAGCATCTGGTGAAACCGTGAAATGA
CTCTTAGACCTACCGTGACCACAGCGGGTACTGATGATCCGGGGAACGATTATAGTAGTTTCTCCATGTAGCGACTTTATAGTGCTCCCCGGTGGACGTCACTAGAAGATCATCCCACCATGCGGGA
CAGAGTTAAGCCTGCTGGCGGCCGTTAGGGTAGTGCTCG'
i = 0
rna = 
for i in range(0,len(dna)):
       if dna[i] is not 'T':
               rna += (dna[i])
       else:
               rna += 'U'
print rna
Output:
GCAGGGCCUAGACAGACCGGAAGUCAAUGUAGUAUUUAGCCUUGGUAAGAUUACUUACGGACUGGUUGGACUAGCAUGGACUCAGUCGGGCCCUAAUAGUGGACUCUUUGGCUUUGACACGCGA
GAUAGCGGCCCAAGCCGUUAGAGCGAGACAAACAACCAGCGCUUCCCUGCAGGGGUUACGUACUUUCUUCGCUCUGCGGAUAUUAUCGCUGCGGAACUAGGCAUGAUGACUGCCAAAUCGGGCA
GGAACCCUGGGAGGUAUAUCAUACUGCCCGCCUCAGUCCGAAACAAUCCGUGCAUAGGUUUCUUUACGUCCCUUUGUAUCCUUGAGUGUAUGCUCGCAAGAGCCAGACGACAUCCAGAGAUAAC
CUAGCCUCGCUCCCCGCGACUAUCCACAAGUCUAAUGGUGCGGCCAAAAAAAGAGCAGUUCAUAGCGCAGGGGUUGCCAUGUAGCCCCGCCCCCAGGUUAGAGACAAUCGUUUGAGUAUAGACA
GUUCUACCCACUAAGACGCCGGGGCCAUUGCUCGGGCCACAUUGCGAGUGACAUAAUUAUGGGCUAUGAACCAAAAUGAGUCAACGCCCUUGUGAGAAGAGGCAUCUGCUAGUAGCGUCUGGAC
UUUACACGUAAACCGGGGGCCCGCUGGGAGAAAUUGUUUCCCAUCAUCAUAGAAUCACUUAGUAACUCGUCGACCCGUUCGCUGAUAUCGCGGCACAAUCUCGAACCUGAGCAUCUGGUGAAAC
CGUGAAAUGACUCUUAGACCUACCGUGACCACAGCGGGUACUGAUGAUCCGGGGAACGAUUAUAGUAGUUUCUCCAUGUAGCGACUUUAUAGUGCUCCCCGGUGGACGUCACUAGAAGAUCAUC
CCACCAUGCGGGACAGAGUUAAGCCUGCUGGCGGCCGUUAGGGUAGUGCUCG

Complementing a Strand of DNA

Given: A DNA string s of length at most 1000 bp.

Return: The reverse complement sc of s.

dna='AGCTCTGTATCACGATCTCAAACGGACATTGATTCAGCGTAGGCAGACTATAGTTGCTCACAATCCAGTGATTGGCGAGCCTTCGATTTTGAGCCACAAGAAGTAGTGCGGTTAATTGCTTT
GCAGGGGCTTGCGCCCACGGTATGGTCCATTCAGTACAGCTCCCCACTCGTTTGTATTGTCTTAGGTGGCGATGCGTCGCAGCTTTGATTATACGAATTGTCATGTTTCCAGATACCTTTCACTCAT
GTTCGGGGGTGTGGAAAGGCATCATTACCTGTGTTCGGGTGGTACCGATCCCAGAGGATAAGCATAGGACTTCGGCGAGACCGCGGGGAATAGCTGTCTCCACGGTAAGGTGCTTCGTATGGCAG
GTTAATCCGCGCCATATAAAACCTAGAAAGTAGAGGACTTGTGCTTTTCGGGTCCCCGCACTTCTCCTACTACCAACACGGTCCCATATCTTCCCATGTTTGTAAAGACTCTTCTCAAGATCGGGGA
CTTCACTCAAGCTCAAATGTGCACCCGTGTAGATCTCACCCGTGGGACTGATTTCGACCTATATGAGAAAAGGCCGGGGGGCGGGGTTCCATCCAGAGAAAATTCTTAGTAGAAAGCGTTCCTTTC
TATCTATAGAGCGGCGGACATGACGATTAGTAGAACCCCGGGGACCGGCAGCCCACCAGGAACTCTTCTTGACTTCGCTAACATTCCTGGTGACAAGCCCCTCGCTGGATGATGGTGCGGTTAAAC
TAAGTATTCAGCAATCGAGTCAGTAAAGATCCGTCTGGCTGCTCAGCTCTTCCCTTTACCGGGTTATCGTTCCTGTTAGGCTGCGTGTTCATCTAAACAACGATTCCTGTCCTGACACCAGCACATGG
CCCGTCTAAAAGGGAATACGTCTCATGTATGGG'
i = len(dna) -1
rna = 
while i >= 0:
       if dna[i] == 'T':
               rna += 'A'
       if dna[i] == 'A':
               rna += 'T'
       if dna[i] == 'G':
               rna += 'C'
       if dna[i] == 'C':
               rna += 'G'
       i -= 1
print rna
Output:  
CCCATACATGAGACGTATTCCCTTTTAGACGGGCCATGTGCTGGTGTCAGGACAGGAATCGTTGTTTAGATGAACACGCAGCCTAACAGGAACGATAACCCGGTAAAGGGAAGAGCTGAGCAGCCA
GACGGATCTTTACTGACTCGATTGCTGAATACTTAGTTTAACCGCACCATCATCCAGCGAGGGGCTTGTCACCAGGAATGTTAGCGAAGTCAAGAAGAGTTCCTGGTGGGCTGCCGGTCCCCGGGG
TTCTACTAATCGTCATGTCCGCCGCTCTATAGATAGAAAGGAACGCTTTCTACTAAGAATTTTCTCTGGATGGAACCCCGCCCCCCGGCCTTTTCTCATATAGGTCGAAATCAGTCCCACGGGTGAG
ATCTACACGGGTGCACATTTGAGCTTGAGTGAAGTCCCCGATCTTGAGAAGAGTCTTTACAAACATGGGAAGATATGGGACCGTGTTGGTAGTAGGAGAAGTGCGGGGACCCGAAAAGCACAAGTC
CTCTACTTTCTAGGTTTTATATGGCGCGGATTAACCTGCCATACGAAGCACCTTACCGTGGAGACAGCTATTCCCCGCGGTCTCGCCGAAGTCCTATGCTTATCCTCTGGGATCGGTACCACCCGAA
CACAGGTAATGATGCCTTTCCACACCCCCGAACATGAGTGAAAGGTATCTGGAAACATGACAATTCGTATAATCAAAGCTGCGACGCATCGCCACCTAAGACAATACAAACGAGTGGGGAGCTGTAC
TGAATGGACCATACCGTGGGCGCAAGCCCCTGCAAAGCAATTAACCGCACTACTTCTTGTGGCTCAAAATCGAAGGCTCGCCAATCACTGGATTGTGAGCAACTATAGTCTGCCTACGCTGAATCAA
TGTCCGTTTGAGATCGTGATACAGAGCT