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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ install:
script:
- py.test -vv exercises.py --cov exercises
- coveralls
notifications:
email: false
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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)
[![Build Status](https://travis-ci.org/joannahard/python-tdd-exercises.svg?branch=master)](https://travis-ci.org/joannahard/python-tdd-exercises/builds)
[![Coverage Status](https://coveralls.io/repos/joannahard/python-tdd-exercises/badge.png?branch=master)](https://coveralls.io/r/joannahard/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.
Expand Down
139 changes: 117 additions & 22 deletions exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ def reverse_list(l):
"""
Reverses order of elements in list l.
"""
return None
return list(reversed(l))


def test_reverse_list():
assert reverse_list([1, 2, 3, 4, 5]) == [5, 4, 3, 2, 1]



# ------------------------------------------------------------------------------

def reverse_string(s):
"""
Reverses order of characters in string s.
"""
return None
return s[::-1]




def test_reverse_string():
Expand All @@ -30,7 +33,9 @@ def is_english_vowel(c):
Returns True if c is an english vowel
and False otherwise.
"""
return None
return (c == "a") or (c == "A") or (c == "e") or (c == "E") or (c == "i")\
or (c == "I") or (c == "o") or (c == "O") or (c == "u") or (c == "U") or (c == "y") or (c == "Y")\



def test_is_english_vowel():
Expand All @@ -49,15 +54,19 @@ def test_is_english_vowel():
assert not is_english_vowel('k')
assert not is_english_vowel('z')
assert not is_english_vowel('?')


# ------------------------------------------------------------------------------

#vowels = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
def count_num_vowels(s):
"""
Returns the number of vowels in a string s.
"""
return None
n = 0
for c in s:
if is_english_vowel(c):
n += 1
return n


def test_count_num_vowels():
Expand All @@ -79,9 +88,12 @@ def histogram(l):
"""
Converts a list of integers into a simple string histogram.
"""
return None

s= []
for i in l:
s.append("#"*i)
return '\n'.join(s)


def test_histogram():
assert histogram([2, 5, 1]) == '##\n#####\n#'

Expand All @@ -93,7 +105,12 @@ def get_word_lengths(s):
Returns a list of integers representing
the word lengths in string s.
"""
return None
l = []
for word in s.split():
l.append(len(word))
return l




def test_get_word_lengths():
Expand All @@ -108,7 +125,13 @@ def find_longest_word(s):
Returns the longest word in string s.
In case there are several, return the first.
"""
return None


longest = ''
for w in s.split():
if len(w) > len(longest):
longest = w
return longest


def test_find_longest_word():
Expand All @@ -125,7 +148,13 @@ 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

for letter in s:
if not letter in "agtcAGTC":
return False
return True




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



def test_base_pair():
Expand All @@ -164,7 +198,10 @@ def transcribe_dna_to_rna(s):
Return string s with each letter T replaced by U.
Result is always uppercase.
"""
return None



return s.upper().replace('T', 'U')


def test_transcribe_dna_to_rna():
Expand All @@ -179,8 +216,13 @@ def get_complement(s):
Return the DNA complement in uppercase
(A -> T, T-> A, C -> G, G-> C).
"""
return None



my_dictionary = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
s_comp= ''
for c in s:
s_comp += base_pair(c)
return s_comp.upper()

def test_get_complement():
assert get_complement('CCGGAAGAGCTTACTTAG') == 'GGCCTTCTCGAATGAATC'
Expand All @@ -194,7 +236,9 @@ def get_reverse_complement(s):
Return the reverse complement of string s
(complement reversed in order).
"""
return None

return reverse_string(get_complement(s))



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



return string.replace(substring, '')


def test_remove_substring():
Expand All @@ -226,7 +273,13 @@ 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


l = []
for i in range(len(dna)//3):
if triplet == dna[i*3:i*3+3]:
l.append(i)
return l


def test_get_position_indices():
Expand All @@ -245,7 +298,16 @@ def get_3mer_usage_chart(s):
The list is alphabetically sorted by the name
of the 3-mer.
"""
return None

d = {}
for i in range(len(s) - 2):
kmer = s[i:i+3]
if kmer in d:
d[kmer] += 1
else: d[kmer] = 1
l = list(d.items())
l.sort()
return l


def test_get_3mer_usage_chart():
Expand Down Expand Up @@ -276,7 +338,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
l = []
with open(file_name, 'r') as f:
for line in f:
words= line.split()
if len(words) > 0:
l.append(float(words[column_number -1]))
return l




def test_read_column():
Expand Down Expand Up @@ -315,7 +385,20 @@ def character_statistics(file_name):
Use the isalpha() method to figure out
whether the character is in the alphabet.
"""
return None

d={}
with open(file_name, 'r') as f:
for c in f.read().lower():
if c.isalpha():
if c in d:
d[c] += 1
else:
d[c] = 1
l= list(d.items())
l = sorted(l, key=lambda x: x[1], reverse=True)
most=l[0][0]
least= l[-1 ][0]
return (most, least)


def test_character_statistics():
Expand All @@ -324,7 +407,7 @@ def test_character_statistics():
import os

text = """
To be, or not to be: that is the question:
To be, z or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
Expand Down Expand Up @@ -377,6 +460,9 @@ def test_character_statistics():

def pythagorean_triples(n):
"""



Returns list of all unique pythagorean triples
(a, b, c) where a < b < c <= n.
"""
Expand All @@ -390,7 +476,16 @@ def pythagorean_triples(n):
return l







# ------------------------------------------------------------------------------

def test_pythagorean_triples():
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