diff --git a/README.md b/README.md index 803b3d6..6af9eef 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ ## 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/olovsv/python-tdd-exercises.svg?branch=master)](https://travis-ci.org/olovsv/python-tdd-exercises/builds) +[![Coverage Status](https://coveralls.io/repos/olovsv/python-tdd-exercises/badge.png?branch=master)](https://coveralls.io/r/olovsv/python-tdd-exercises?branch=master) -After you fork, edit this `README.md` and rename "bast" to your GitHub username +After you fork, edit this `README.md` and rename "olovsv" to your GitHub username or namespace to make the badges point to your fork. @@ -22,7 +22,7 @@ or namespace to make the badges point to your fork. - Login to [Travis CI](https://travis-ci.org) with your GitHub account and activate this repo for testing. - Login to [Coveralls](https://coveralls.io) with your GitHub account and activate this repo for code coverage analysis. - Implement missing functions to make the unit tests pass (run tests either locally or let Travis run them for you each time you push changes). -- Increase the test coverage to 100% by making [all lines](https://coveralls.io/r/bast/python-tdd-exercises?branch=master) green. +- Increase the test coverage to 100% by making [all lines](https://coveralls.io/r/olovsv/python-tdd-exercises?branch=master) green. ## How to run tests locally (Linux or Mac OS X) diff --git a/exercises.py b/exercises.py index 38ef8b7..0e70d8f 100644 --- a/exercises.py +++ b/exercises.py @@ -3,7 +3,10 @@ def reverse_list(l): """ Reverses order of elements in list l. """ - return None + new_l = l + new_l.reverse() + + return new_l def test_reverse_list(): @@ -16,7 +19,13 @@ def reverse_string(s): """ Reverses order of characters in string s. """ - return None + string_list = list(s) + reverse_string_list = string_list.reverse() + reverse_string = "" + for i in string_list: + reverse_string = reverse_string+i + + return reverse_string def test_reverse_string(): @@ -30,8 +39,17 @@ def is_english_vowel(c): Returns True if c is an english vowel and False otherwise. """ - return None + c = c.lower() + vowels = ['a','e','i','o','u','y'] + vowel_test = False + for i in vowels: + if(c == i): + vowel_test = True + if vowel_test == True: + return True + else: + return False def test_is_english_vowel(): assert is_english_vowel('a') @@ -57,7 +75,16 @@ def count_num_vowels(s): """ Returns the number of vowels in a string s. """ - return None + + s = s.lower() + s_list = list(s) + vowels = ['a','e','i','o','u','y'] + vowel_count = 0 + for i in s_list: + for j in vowels: + if(i == j): + vowel_count += 1 + return vowel_count def test_count_num_vowels(): @@ -79,7 +106,21 @@ def histogram(l): """ Converts a list of integers into a simple string histogram. """ - return None + + histogram_list = [] + + for k in l: + i = 0 + j = 0 + string_to_add = "" + while(i < k): + string_to_add = string_to_add + "#" + i += 1 + histogram_list.append(string_to_add) + + histogram_string = "\n".join(histogram_list) + + return histogram_string def test_histogram(): @@ -93,7 +134,14 @@ def get_word_lengths(s): Returns a list of integers representing the word lengths in string s. """ - return None + + string_list = s.split(" ") + word_lenghts = [] + for k in string_list: + word_lenght = len(k) + word_lenghts.append(word_lenght) + + return word_lenghts def test_get_word_lengths(): @@ -108,7 +156,21 @@ def find_longest_word(s): Returns the longest word in string s. In case there are several, return the first. """ - return None + + string_list = s.split(" ") + + word_length = {} + + highest_value = 0 + highest_key = "" + + for k in string_list: + word_length[k] = len(k) + if(word_length[k] > highest_value): + highest_value = word_length[k] + highest_key = k + + return highest_key def test_find_longest_word(): @@ -125,7 +187,15 @@ 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 + + s = s.lower() + + DNA = True + + for i in s: + if i != 'a' and i != 't' and i != 'g' and i != 'c': + DNA = False + return DNA def test_validate_dna(): @@ -141,7 +211,21 @@ def base_pair(c): of the base pair. If the base is not recognized, return 'unknown'. """ - return None + + c = c.lower() + + to_return = 'unknown' + + if(c == 'a'): + to_return = 't' + elif(c == 't'): + to_return = 'a' + elif(c == 'g'): + to_return = 'c' + elif(c == 'c'): + to_return = 'g' + + return to_return def test_base_pair(): @@ -164,7 +248,15 @@ def transcribe_dna_to_rna(s): Return string s with each letter T replaced by U. Result is always uppercase. """ - return None + + s = s.upper() + new_string = "" + + for i in s: + if(i == 'T'): + i = 'U' + new_string = new_string + i + return new_string def test_transcribe_dna_to_rna(): @@ -179,7 +271,22 @@ def get_complement(s): Return the DNA complement in uppercase (A -> T, T-> A, C -> G, G-> C). """ - return None + + s = s.upper() + + new_string = "" + + for i in s: + if(i == 'A'): + new_string = new_string + 'T' + elif(i == 'T'): + new_string = new_string + 'A' + elif(i == 'G'): + new_string = new_string + 'C' + elif(i == 'C'): + new_string = new_string + 'G' + + return new_string def test_get_complement(): @@ -194,7 +301,25 @@ def get_reverse_complement(s): Return the reverse complement of string s (complement reversed in order). """ - return None + + s = s.upper() + + string_list = [] + + for i in s: + if(i == 'A'): + string_list.append('T') + elif(i == 'T'): + string_list.append('A') + elif(i == 'G'): + string_list.append('C') + elif(i == 'C'): + string_list.append('G') + + reversed_string_list = string_list[::-1] + reversed_string = "".join(reversed_string_list) + + return reversed_string def test_get_reverse_complement(): @@ -208,7 +333,10 @@ def remove_substring(substring, string): """ Returns string with all occurrences of substring removed. """ - return None + + new_string = string.replace(substring,"") + + return new_string def test_remove_substring(): @@ -226,7 +354,22 @@ 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 + + index_list = [] + kmer_list = [] + + while(len(dna) >= 3): + kmer_list.append(dna[0:3]) + dna = dna[3:] + + i = 0 + for k in kmer_list: + if(triplet == k): + index_list.append(i) + i = i + 1 + + + return index_list def test_get_position_indices(): @@ -245,7 +388,29 @@ def get_3mer_usage_chart(s): The list is alphabetically sorted by the name of the 3-mer. """ - return None + + kmers = [] + unique_kmers = [] + kmer_occurence = {} + kmer_taken = {} + while(len(s) >= 3): + kmer = s[0:3] + kmers.append(s[0:3]) + s = s[1:] + kmer_occurence[kmer] = 0 + + + for i in kmers: + kmer_occurence[i] += 1 + + kmers = list(set(kmers)) + kmers = sorted(kmers) + kmers_list = [] + + for k in kmers: + kmers_list.append((k,kmer_occurence[k])) + + return kmers_list def test_get_3mer_usage_chart(): @@ -276,7 +441,18 @@ 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 + + column_file = open(file_name) + columns = [] + columns2return = [] + column_number -=1 + #column_number -= 1 + for line in column_file: + column_list = line.split() + if len(column_list)>0: + columns2return.append(float(column_list[column_number])) + + return columns2return def test_read_column(): @@ -315,7 +491,36 @@ def character_statistics(file_name): Use the isalpha() method to figure out whether the character is in the alphabet. """ - return None + + this_file = open(file_name) + all_characters = [] + character_abundance = {} + for line in this_file: + line = line.lower() + while(len(line) > 0): + character = line[:1] + character_abundance[character] = 0 + if(character.isalpha()): + all_characters.append(character) + line = line[1:] + + for k in all_characters: + character_abundance[k] += 1 + + all_characters = list(set(all_characters)) + + lowest_character = all_characters[0] + highest_character = all_characters[0] + + for j in all_characters: + if(character_abundance[j] < character_abundance[lowest_character]): + lowest_character = j + if(character_abundance[j] > character_abundance[highest_character]): + highest_character = j + + returning_tuple = (highest_character,lowest_character) + + return returning_tuple def test_character_statistics(): @@ -393,4 +598,10 @@ def pythagorean_triples(n): # ------------------------------------------------------------------------------ def test_pythagorean_triples(): - pass # so far we do not test anything, check also test coverage + + testing_list = [5,10,20] + assert(pythagorean_triples(5)) == [(3, 4, 5)] + assert(pythagorean_triples(10)) == [(3, 4, 5), (6, 8, 10)] + 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