Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

## Make these badges green

[![Build Status](https://travis-ci.org/bast/python-tdd-exercises.svg?branch=master)](https://travis-ci.org/bast/python-tdd-exercises/builds)
[![Coverage Status](https://coveralls.io/repos/bast/python-tdd-exercises/badge.png?branch=master)](https://coveralls.io/r/bast/python-tdd-exercises?branch=master)

After you fork, edit this `README.md` and rename "bast" to your GitHub username
or namespace to make the badges point to your fork.
[![Build Status](https://travis-ci.org/hassanfa/python-tdd-exercises.svg?branch=master)](https://travis-ci.org/hassanfa/python-tdd-exercises/builds)
[![Coverage Status](https://coveralls.io/repos/hassanfa/python-tdd-exercises/badge.png?branch=master)](https://coveralls.io/r/hassanfa/python-tdd-exercises?branch=master)


## Inspirations
Expand Down
166 changes: 145 additions & 21 deletions exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def reverse_list(l):
"""
Reverses order of elements in list l.
"""
return None
return l[::-1]


def test_reverse_list():
Expand All @@ -16,8 +16,7 @@ def reverse_string(s):
"""
Reverses order of characters in string s.
"""
return None

return s[::-1]

def test_reverse_string():
assert reverse_string("foobar") == "raboof"
Expand All @@ -30,7 +29,10 @@ def is_english_vowel(c):
Returns True if c is an english vowel
and False otherwise.
"""
return None
cVowel=False
if c.lower() in "aeiyuo":
cVowel=True
return cVowel


def test_is_english_vowel():
Expand All @@ -57,7 +59,11 @@ def count_num_vowels(s):
"""
Returns the number of vowels in a string s.
"""
return None
out=0
for c in s:
if c.lower() in "aeiuyo":
out+=1
return out


def test_count_num_vowels():
Expand All @@ -79,7 +85,13 @@ def histogram(l):
"""
Converts a list of integers into a simple string histogram.
"""
return None
out=""
for n in list(range(len(l))):
for m in list(range(l[n])):
out+="#"
if n<len(l)-1:
out+="\n"
return out


def test_histogram():
Expand All @@ -93,7 +105,19 @@ def get_word_lengths(s):
Returns a list of integers representing
the word lengths in string s.
"""
return None
wordIndex=0
out=[]
for c in list(range(len(s))):
if s[c]==" ":
if wordIndex==0:
out.append(c-wordIndex)
wordIndex=c
else:
out.append(c-wordIndex-1)
wordIndex=c
if wordIndex<=c:
out.append(len(s)-wordIndex-1)
return out


def test_get_word_lengths():
Expand All @@ -108,7 +132,23 @@ def find_longest_word(s):
Returns the longest word in string s.
In case there are several, return the first.
"""
return None
wordIndex=0
longestWord=""
out=[]
for c in list(range(len(s))):
if s[c]==" ":
if wordIndex==0:
out.append(s[wordIndex:c])
wordIndex=c
else:
out.append(s[wordIndex+1:c])
wordIndex=c
if wordIndex<=c:
out.append(s[wordIndex+1:len(s)])
for w in out:
if len(w)>len(longestWord):
longestWord=w
return longestWord


def test_find_longest_word():
Expand All @@ -125,7 +165,11 @@ def validate_dna(s):
Return True if the DNA string only contains characters
a, c, t, or g (lower or uppercase). False otherwise.
"""
return None
out=True
for c in s:
if c.lower() not in 'ctga':
out=False
return out


def test_validate_dna():
Expand All @@ -141,7 +185,12 @@ def base_pair(c):
of the base pair. If the base is not recognized,
return 'unknown'.
"""
return None
out='unknown'
refbp={"a":"t","t":"a","c":"g","g":"c"}
if c.lower() in list(refbp.keys()):
if c.lower() == refbp[refbp[c.lower()]]:
out=refbp[c.lower()]
return out


def test_base_pair():
Expand All @@ -164,7 +213,13 @@ def transcribe_dna_to_rna(s):
Return string s with each letter T replaced by U.
Result is always uppercase.
"""
return None
out=""
for c in s:
if c.upper()=="T":
out+="U"
else:
out+=c.upper()
return out


def test_transcribe_dna_to_rna():
Expand All @@ -179,7 +234,14 @@ def get_complement(s):
Return the DNA complement in uppercase
(A -> T, T-> A, C -> G, G-> C).
"""
return None
out=""
refbp={"a":"t","t":"a","c":"g","g":"c"}
for c in s:
if c.lower() in list(refbp.keys()):
if c.lower() == refbp[refbp[c.lower()]]:
out+=refbp[c.lower()]
out=out.upper()
return out


def test_get_complement():
Expand All @@ -194,7 +256,15 @@ def get_reverse_complement(s):
Return the reverse complement of string s
(complement reversed in order).
"""
return None
out=""
refbp={"a":"t","t":"a","c":"g","g":"c"}
for c in s:
if c.lower() in list(refbp.keys()):
if c.lower() == refbp[refbp[c.lower()]]:
out+=refbp[c.lower()]
out=out.upper()
out=out[::-1]
return out


def test_get_reverse_complement():
Expand All @@ -208,8 +278,8 @@ def remove_substring(substring, string):
"""
Returns string with all occurrences of substring removed.
"""
return None

out = string.replace(substring,"")
return out

def test_remove_substring():
assert remove_substring('GAA', 'CCGGAAGAGCTTACTTAG') == 'CCGGAGCTTACTTAG'
Expand All @@ -226,7 +296,11 @@ def get_position_indices(triplet, dna):
in a DNA sequence. We start counting from 0
and jump by 3 characters from one position to the next.
"""
return None
out=[]
for n in range(0,len(dna),3):
if dna[n:n+3]==triplet:
out.append(n//3)
return out


def test_get_position_indices():
Expand All @@ -245,7 +319,24 @@ def get_3mer_usage_chart(s):
The list is alphabetically sorted by the name
of the 3-mer.
"""
return None
triplet=[]
for n in list(range(len(s))):
if n<=len(s)-3:
triplet.append(s[n:n+3])
triplet = list(set(triplet))
triplet = sorted(triplet)

tripcountOut=[]
for t in triplet:
tmpS = s
tmpS = tmpS.replace(t,'X')
tripcount=0
for c in tmpS:
if c=='X':
tripcount+=1
tripcountOut.append(tripcount)
out=list(zip(triplet,tripcountOut))
return out


def test_get_3mer_usage_chart():
Expand Down Expand Up @@ -276,7 +367,15 @@ def read_column(file_name, column_number):
Reads column column_number from file file_name
and returns the values as floats in a list.
"""
return None
import csv
fieldHandle=open(file_name, 'r')
out=[]
for line in fieldHandle:
fields=line.split()
if len(fields)!=0:
out.append(float(fields[column_number-1]))
fieldHandle.close()
return out


def test_read_column():
Expand Down Expand Up @@ -315,7 +414,31 @@ def character_statistics(file_name):
Use the isalpha() method to figure out
whether the character is in the alphabet.
"""
return None
import csv
import string
import operator
fieldHandle=open(file_name, 'r')
out=[]
for line in fieldHandle:
fields=line.split()
out+=fields
fieldHandle.close()
charCount=[]
out=''.join(out)
out = out.lower()
refString = string.ascii_lowercase
print(refString)
for c in list(refString):
charCount.append(out.count(c))
print(charCount)
index, value = max(enumerate(charCount), key=operator.itemgetter(1))
firstMax= refString[index]
index, value = min(enumerate(charCount), key=operator.itemgetter(1))
secondMax= refString[index]
refString2nd = refString[:index] + refString[index+1:]
#del charCount[index]
#secondMax= refString2nd[index]
return (firstMax,secondMax)


def test_character_statistics():
Expand All @@ -335,7 +458,7 @@ def test_character_statistics():
Devoutly to be wish'd. To die, to sleep;
To sleep: perchance to dream: ay, there's the rub;
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
When jjj xxx we have shuffled off this mortal coil,
Must give us pause: there's the respect
That makes calamity of so long life;
For who would bear the whips and scorns of time,
Expand Down Expand Up @@ -393,4 +516,5 @@ def pythagorean_triples(n):
# ------------------------------------------------------------------------------

def test_pythagorean_triples():
pass # so far we do not test anything, check also test coverage
assert pythagorean_triples(20) == [(3, 4, 5), (6, 8, 10), (5, 12, 13), (9, 12, 15), (8, 15, 17), (12, 16, 20)]
pass # so far we do not test anything, check also test coverage