Skip to content
Open
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
86 changes: 86 additions & 0 deletions tdd_practice_001.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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 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]
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:
char = char.lower()
occurance[char] = string.lower().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) 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


def is_number(s):
try:
float(s)
return True
except ValueError:
return False


def count_unique(text):
'''
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 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)
107 changes: 107 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from unittest import TestCase, main
from tdd_practice_001 import (
increment_string,
count_letters,
sum_consecutives,
count_unique,
)


class TestIcrementString(TestCase):

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")

def test_none(self): ...


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 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__":
main(verbosity=2)