From e3b217122c8f94c18f792296bd096db283436d6d Mon Sep 17 00:00:00 2001 From: Kar0r404 Date: Thu, 2 Oct 2025 20:03:07 +0200 Subject: [PATCH 1/3] fc --- tdd_practice_001.py | 69 +++++++++++++++++++++++++++++++++++++++++++++ tests.py | 25 ++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tdd_practice_001.py create mode 100644 tests.py diff --git a/tdd_practice_001.py b/tdd_practice_001.py new file mode 100644 index 0000000..7858a3a --- /dev/null +++ b/tdd_practice_001.py @@ -0,0 +1,69 @@ +def increment_string(string): + """ + Description: Increment the numeric suffix of a string. + + If the string ends with a number, increment that number by 1. + If the string does not end with a number, append 1. + Examples: + + "foo" -> "foo1" + "foobar23" -> "foobar24" + "foo0042" -> "foo0043" + "foo9" -> "foo10" + """ + num = "" + if not string[-1].isdigit() or len(string) == 0: + return string + str(1) + for i in range(len(string)): + if string[len(string) - 1 - i].isalpha(): + break + idx = len(string) - 1 - i + num += string[len(string) - 1 - i] + print(num) + return f"{string[:idx]}{int(num[::-1]) + 1:0{len(num)}d}" + + +def count_letters(string): + """ + Description: Count the occurrences of each letter in a string. + + Example: + + "aba" -> {"a": 2, "b": 1} + """ + + occurance = {} + for char in string.lower(): + occurance[char] = string.count(char) + return occurance + + +def sum_consecutives(s): + """ + Description: Return a list of sums of each consecutive pair in a list. + + Example: + + [1, 2, 3] -> [3, 5] # 1+2=3, 2+3=5 + """ + sum = [] + for idx, num in enumerate(s): + if idx + 1 < len(s): + sum.append(num + s[idx + 1]) + return sum + + +print(sum_consecutives([1, 2, 3])) + + +def count_unique(string): + ''' + Description: Count the number of unique words in a string. + + Should raise a ValueError if the input is not a string. + + Example: + + "no example ;)"''' + if isinstance(string, str): + raise ValueError("passed param must be of str type.") diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..1ed934b --- /dev/null +++ b/tests.py @@ -0,0 +1,25 @@ +from unittest import TestCase, main +from tdd_practice_001 import ( + increment_string, + count_letters, + sum_consecutives, + count_unique, +) + + +class TestIcrementString(TestCase): + + def test_increment(self): ... + + +class TestCountLetters(TestCase): ... + + +class TestSumConsecutives(TestCase): ... + + +class TestCountUnique(TestCase): ... + + +if __name__ == "__main__": + main() From 46ba0f1d53b355824532eb784a3b37305e3bdf1d Mon Sep 17 00:00:00 2001 From: Kar0r404 Date: Sun, 5 Oct 2025 19:24:54 +0200 Subject: [PATCH 2/3] tests --- tdd_practice_001.py | 20 +++++++++----- tests.py | 65 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/tdd_practice_001.py b/tdd_practice_001.py index 7858a3a..05d08d1 100644 --- a/tdd_practice_001.py +++ b/tdd_practice_001.py @@ -11,15 +11,15 @@ def increment_string(string): "foo0042" -> "foo0043" "foo9" -> "foo10" """ + num = "" - if not string[-1].isdigit() or len(string) == 0: + if len(string) == 0 or not string[-1].isdigit(): return string + str(1) for i in range(len(string)): if string[len(string) - 1 - i].isalpha(): break idx = len(string) - 1 - i num += string[len(string) - 1 - i] - print(num) return f"{string[:idx]}{int(num[::-1]) + 1:0{len(num)}d}" @@ -27,14 +27,15 @@ def count_letters(string): """ Description: Count the occurrences of each letter in a string. - Example: + Example:: "aba" -> {"a": 2, "b": 1} """ occurance = {} - for char in string.lower(): - occurance[char] = string.count(char) + for char in string: + char = char.lower() + occurance[char] = string.lower().count(char) return occurance @@ -48,12 +49,19 @@ def sum_consecutives(s): """ sum = [] for idx, num in enumerate(s): + if idx + 1 < len(s) and not (is_number(num) and is_number(s[idx + 1])): + raise ValueError("The list must be numerical list.") if idx + 1 < len(s): sum.append(num + s[idx + 1]) return sum -print(sum_consecutives([1, 2, 3])) +def is_number(s): + try: + float(s) + return True + except ValueError: + return False def count_unique(string): diff --git a/tests.py b/tests.py index 1ed934b..5eecc9b 100644 --- a/tests.py +++ b/tests.py @@ -9,17 +9,74 @@ class TestIcrementString(TestCase): - def test_increment(self): ... + def test_append_one(self): + string1 = "" + string2 = "abcd" + self.assertEqual(increment_string(string1), "1") + self.assertEqual(increment_string(string2), "abcd1") + def test_add_one(self): + string1 = "abcde123" + string2 = "0123" + self.assertEqual(increment_string(string1), "abcde124") + self.assertEqual(increment_string(string2), "0124") -class TestCountLetters(TestCase): ... + def test_none(self): ... -class TestSumConsecutives(TestCase): ... +class TestCountLetters(TestCase): + + def test_count_empty(self): + """count letter in an empty string""" + self.assertDictEqual(count_letters(""), {}) + + def test_count_letters(self): + string = "karabo" + string1 = "string is None, " + self.assertDictEqual( + count_letters(string), {"k": 1, "a": 2, "r": 1, "b": 1, "o": 1} + ) + self.assertDictEqual( + count_letters(string1), + { + "s": 2, + "t": 1, + "r": 1, + "n": 3, + "g": 1, + "i": 2, + "o": 1, + "e": 1, + " ": 3, + ",": 1, + }, + ) + + def test_type_return(self): + "Testing if it return a dictionary" + self.assertIsInstance(count_letters(""), dict) + + +class TestSumConsecutives(TestCase): + + def test_empty_list(self): + self.assertListEqual(sum_consecutives([]), []) + + def test_returned_type(self): + "Testing if it return a list" + self.assertIsInstance((sum_consecutives([])), list) + + def test_mixed_list(self): + with self.assertRaises(ValueError): + sum_consecutives([1, 23.23, "gmail"]) + + def test_num_list(self): + self.assertListEqual(sum_consecutives([2, 3, 5, 6]), [5, 8, 11]) + self.assertListEqual(sum_consecutives([0.5, 1]), [1.5]) class TestCountUnique(TestCase): ... if __name__ == "__main__": - main() + main(verbosity=2) From be6057d2f9ffc30e5e68ad1421dd27b3b545b62b Mon Sep 17 00:00:00 2001 From: Kar0r404 Date: Sun, 5 Oct 2025 19:47:35 +0200 Subject: [PATCH 3/3] refined count unique --- tdd_practice_001.py | 15 ++++++++++++--- tests.py | 27 ++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tdd_practice_001.py b/tdd_practice_001.py index 05d08d1..86be0f4 100644 --- a/tdd_practice_001.py +++ b/tdd_practice_001.py @@ -64,7 +64,7 @@ def is_number(s): return False -def count_unique(string): +def count_unique(text): ''' Description: Count the number of unique words in a string. @@ -73,5 +73,14 @@ def count_unique(string): Example: "no example ;)"''' - if isinstance(string, str): - raise ValueError("passed param must be of str type.") + + if not isinstance(text, str): + raise ValueError("Input must be a string") + + words = text.lower().split() + # unique_words = set(words) + unique_words = [] + for word in words: + if len(word) > 1 and word not in unique_words and word.isalpha(): + unique_words.append(word) + return len(unique_words) diff --git a/tests.py b/tests.py index 5eecc9b..878ddfe 100644 --- a/tests.py +++ b/tests.py @@ -75,7 +75,32 @@ def test_num_list(self): self.assertListEqual(sum_consecutives([0.5, 1]), [1.5]) -class TestCountUnique(TestCase): ... +class TestCountUniqueWords(TestCase): + + def test_basic_example(self): + self.assertEqual(count_unique("no example ;)"), 2) + + def test_case_insensitivity(self): + self.assertEqual(count_unique("Hello hello world"), 2) + + def test_empty_string(self): + self.assertEqual(count_unique(""), 0) + + def test_single_word(self): + self.assertEqual(count_unique("Python"), 1) + + def test_multiple_unique_words(self): + self.assertEqual(count_unique("one two three four"), 4) + + def test_repeated_words(self): + self.assertEqual(count_unique("test test test"), 1) + + def test_non_string_input(self): + with self.assertRaises(ValueError): + count_unique(12345) + + def test_string_with_punctuation(self): + self.assertEqual(count_unique("word, word"), 2) if __name__ == "__main__":