From 7992e462e25397d6539984aa9673c68ad7f8f6a0 Mon Sep 17 00:00:00 2001 From: Elisabeth Vonesch Date: Sat, 6 Jul 2019 18:48:15 +0200 Subject: [PATCH 1/6] implement function to find largest number --- src/clean_code/MathHelper.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/clean_code/MathHelper.py b/src/clean_code/MathHelper.py index 9c12beb..268f375 100644 --- a/src/clean_code/MathHelper.py +++ b/src/clean_code/MathHelper.py @@ -1,3 +1,5 @@ +import sys + class MathHelper: def find_largest_number_in_list(self, list_with_numbers): @@ -6,6 +8,7 @@ def find_largest_number_in_list(self, list_with_numbers): :param list_with_numbers: :return: largest number in list, -sys.maxsize -1 for empty list """ - return 0 + max_number = max(list_with_numbers, default=-sys.maxsize-1) + return max_number From 1c102412198edc43f639c63e1e68ec0150b0702f Mon Sep 17 00:00:00 2001 From: Elisabeth Vonesch Date: Sat, 6 Jul 2019 19:36:22 +0200 Subject: [PATCH 2/6] [WIP] implementing tests for StringHelper() --- src/clean_code/StringHelper.py | 1 + src/clean_code/test_StringHelper.py | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/clean_code/StringHelper.py b/src/clean_code/StringHelper.py index ad964cd..25dbcda 100644 --- a/src/clean_code/StringHelper.py +++ b/src/clean_code/StringHelper.py @@ -10,6 +10,7 @@ def strings_are_anagrams(self, first_word, second_word): if len(first_word) != len(second_word): return False for letter in first_word: + #TODO convert to lowercase if letter not in second_word: return False return True diff --git a/src/clean_code/test_StringHelper.py b/src/clean_code/test_StringHelper.py index de29d23..9608c56 100644 --- a/src/clean_code/test_StringHelper.py +++ b/src/clean_code/test_StringHelper.py @@ -1 +1,26 @@ -#TODO: Please write a test for my Anagram function \ No newline at end of file +#TODO: Please write a test for my Anagram function + +import unittest +from src.clean_code.StringHelper import StringHelper + +class Test(unittest.TestCase): + + def setUp(self): + self.string_helper = StringHelper() + + def test_strings_are_anagrams_random_anagram(self): + example_string_1 = "mary" + example_string_2 = "amry" + expected = True + actual = self.string_helper.strings_are_anagrams(example_string_1, example_string_2) + self.assertEqual(expected, actual) + + def test_strings_are_anagrams_empty_string(self): + example_string_1 = "Here comes dots" + example_string_2 = "" + expected = False + actual = self.string_helper.strings_are_anagrams(example_string_1, example_string_2) + self.assertEqual(expected, actual) + + #def test_strings_are_anagrams_anagram_capitalized(self) + From 2beacdba039094daee31bad7c0c2c1defda86ba5 Mon Sep 17 00:00:00 2001 From: Elisabeth Vonesch Date: Sun, 7 Jul 2019 20:41:51 +0200 Subject: [PATCH 3/6] convert arguments to lowercase --- src/clean_code/StringHelper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clean_code/StringHelper.py b/src/clean_code/StringHelper.py index 25dbcda..4cd0f98 100644 --- a/src/clean_code/StringHelper.py +++ b/src/clean_code/StringHelper.py @@ -9,8 +9,8 @@ def strings_are_anagrams(self, first_word, second_word): """ if len(first_word) != len(second_word): return False - for letter in first_word: + for letter in first_word.lower(): #TODO convert to lowercase - if letter not in second_word: + if letter not in second_word.lower(): return False return True From 2df34b0aca99c2c349edd376db2d929da7eea08e Mon Sep 17 00:00:00 2001 From: Elisabeth Vonesch Date: Sun, 7 Jul 2019 20:42:36 +0200 Subject: [PATCH 4/6] implement test for capitalized anagrams --- src/clean_code/test_StringHelper.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/clean_code/test_StringHelper.py b/src/clean_code/test_StringHelper.py index 9608c56..a9b9c9a 100644 --- a/src/clean_code/test_StringHelper.py +++ b/src/clean_code/test_StringHelper.py @@ -7,20 +7,22 @@ class Test(unittest.TestCase): def setUp(self): self.string_helper = StringHelper() + self.anagram1 = "mary" + self.anagram2 = "amry" def test_strings_are_anagrams_random_anagram(self): - example_string_1 = "mary" - example_string_2 = "amry" - expected = True - actual = self.string_helper.strings_are_anagrams(example_string_1, example_string_2) - self.assertEqual(expected, actual) + result = self.string_helper.strings_are_anagrams(self.anagram1, self.anagram2) + self.assertTrue(result) def test_strings_are_anagrams_empty_string(self): example_string_1 = "Here comes dots" example_string_2 = "" - expected = False - actual = self.string_helper.strings_are_anagrams(example_string_1, example_string_2) - self.assertEqual(expected, actual) + result = self.string_helper.strings_are_anagrams(example_string_1, example_string_2) + self.assertFalse(result) + + def test_strings_are_anagrams_anagram_capitalized(self): + anagram_capitalized = "Amry" + result = self.string_helper.strings_are_anagrams(self.anagram1, anagram_capitalized) + self.assertTrue(result) - #def test_strings_are_anagrams_anagram_capitalized(self) From 3d9554bf11a0cf782a7b0dbf8f1ab9ede9b7e730 Mon Sep 17 00:00:00 2001 From: Elisabeth Vonesch Date: Sun, 7 Jul 2019 21:22:24 +0200 Subject: [PATCH 5/6] [WIP] raise TypeError if argument not a string --- src/clean_code/StringHelper.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/clean_code/StringHelper.py b/src/clean_code/StringHelper.py index 4cd0f98..5162b78 100644 --- a/src/clean_code/StringHelper.py +++ b/src/clean_code/StringHelper.py @@ -7,10 +7,13 @@ def strings_are_anagrams(self, first_word, second_word): :param second_word: :return: True if the words are an Anagram, False otherwise """ + if type(first_word) is not str or type(second_word) is not str: + raise TypeError("Not a string") + if len(first_word) != len(second_word): return False + for letter in first_word.lower(): - #TODO convert to lowercase if letter not in second_word.lower(): return False - return True + return True From 322a5aaa773f2ac0e84230b054abcae6cfa9f725 Mon Sep 17 00:00:00 2001 From: Elisabeth Vonesch Date: Mon, 8 Jul 2019 10:56:49 +0200 Subject: [PATCH 6/6] raise TypeError if arg not str & test with int argument --- src/clean_code/StringHelper.py | 2 +- src/clean_code/test_StringHelper.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/clean_code/StringHelper.py b/src/clean_code/StringHelper.py index 5162b78..e33333a 100644 --- a/src/clean_code/StringHelper.py +++ b/src/clean_code/StringHelper.py @@ -8,7 +8,7 @@ def strings_are_anagrams(self, first_word, second_word): :return: True if the words are an Anagram, False otherwise """ if type(first_word) is not str or type(second_word) is not str: - raise TypeError("Not a string") + raise TypeError('Not a string') if len(first_word) != len(second_word): return False diff --git a/src/clean_code/test_StringHelper.py b/src/clean_code/test_StringHelper.py index a9b9c9a..19ed216 100644 --- a/src/clean_code/test_StringHelper.py +++ b/src/clean_code/test_StringHelper.py @@ -25,4 +25,9 @@ def test_strings_are_anagrams_anagram_capitalized(self): result = self.string_helper.strings_are_anagrams(self.anagram1, anagram_capitalized) self.assertTrue(result) + def test_strings_are_anagrams_integer(self): + example_int = 2 + self.assertRaises(TypeError, self.string_helper.strings_are_anagrams, self.anagram1, example_int ) + +