From 5e8db967a1a34caaa113bf20646b5b7ebc521035 Mon Sep 17 00:00:00 2001 From: Khosi Dlomo Date: Wed, 18 Dec 2024 19:52:10 +0200 Subject: [PATCH 1/5] over to umdali and kyle --- functions_to_test.py | 55 +++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 330fa02..9c09c79 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -1,34 +1,63 @@ # Placeholder functions for Python basics, to be implemented later +import string def add_numbers(a, b): - pass + return a + b def find_maximum(a, b, c): - pass + return max(a,b,c) def is_palindrome(string): - pass + return string == string[::-1] def count_word_occurrences(text, word): - pass + text = text.split() + return text.count(word) def read_file_lines(filepath): - pass + with open(filepath, 'r') as f: + return f.readlines() def factorial(n): - pass + x = 1 + for i in range(1, n+ 1): + x *= i + return x def is_prime(n): - pass + if n < 0: + raise ValueError("Prime not defined for negative numbers") + if n <= 1: + return False + for i in range(2, int(n ** 0.5) + 1): + if n % i == 0: + return False + return True + def sort_numbers(numbers): - pass + numbers = [] + return numbers.sort(reverse = True) + def factorial(n): - pass - + if n == 1 or n == 0: + return 1 + if n < 0: + return "" + if n < 0: + raise ValueError("Invalid input type") + x = 1 + for i in range(1, n+ 1): + x *= i + return x + def fibonacci(n): - pass + if n == 0: + return 0 + if n == 1: + return 1 + return fibonacci(n - 1) + fibonacci(n - 2) def tower_of_hanoi(n, source, auxiliary, target): @@ -52,8 +81,8 @@ def tower_of_hanoi(n, source, auxiliary, target): class Person: def __init__(self, name, age): - pass - + self.name = name + self.age = age if __name__ == "__main__": # Placeholder functions for Python basics, to be implemented later From 25dcdaaafa86fb8705959d27e145cfd1812a88a6 Mon Sep 17 00:00:00 2001 From: Khosi Dlomo Date: Wed, 18 Dec 2024 19:59:41 +0200 Subject: [PATCH 2/5] yoh --- functions_to_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 9c09c79..295461a 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -81,8 +81,8 @@ def tower_of_hanoi(n, source, auxiliary, target): class Person: def __init__(self, name, age): - self.name = name - self.age = age + self.name = str(name) + self.age = int(age) if __name__ == "__main__": # Placeholder functions for Python basics, to be implemented later From 7fab2480664f68f6861dfb747ee6f82d8b02dbe2 Mon Sep 17 00:00:00 2001 From: Khosi Dlomo Date: Fri, 10 Jan 2025 22:48:11 +0200 Subject: [PATCH 3/5] word count occurence --- functions_to_test.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 295461a..a7c8917 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -10,9 +10,16 @@ def find_maximum(a, b, c): def is_palindrome(string): return string == string[::-1] + def count_word_occurrences(text, word): - text = text.split() - return text.count(word) + if type(text) != str or type(word) != str: + raise TypeError("Need to be a string") + + text = text.lower() + word = word.lower() + words = text.split() + + return words.count(word) def read_file_lines(filepath): with open(filepath, 'r') as f: @@ -87,4 +94,4 @@ def __init__(self, name, age): if __name__ == "__main__": # Placeholder functions for Python basics, to be implemented later #to test your functions, you can use the following code - print(add_numbers(3, 5)) #e.g \ No newline at end of file + print(add_numbers(3, 5)) #e.g From a48303059ce0b271f6d285be1dc7f80a8e332d28 Mon Sep 17 00:00:00 2001 From: Khosi Dlomo Date: Fri, 10 Jan 2025 22:49:35 +0200 Subject: [PATCH 4/5] count words --- functions_to_test.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index a7c8917..94bd3c6 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -15,11 +15,10 @@ def count_word_occurrences(text, word): if type(text) != str or type(word) != str: raise TypeError("Need to be a string") - text = text.lower() + text = text.lower().split() word = word.lower() - words = text.split() - - return words.count(word) + + return text.count(word) def read_file_lines(filepath): with open(filepath, 'r') as f: From 707dcedfd49bffd38f1b1e3e8966794634fb0fb1 Mon Sep 17 00:00:00 2001 From: Khosi Dlomo Date: Sat, 11 Jan 2025 12:33:18 +0200 Subject: [PATCH 5/5] class test --- functions_to_test.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 94bd3c6..a95de37 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -17,7 +17,7 @@ def count_word_occurrences(text, word): text = text.lower().split() word = word.lower() - + return text.count(word) def read_file_lines(filepath): @@ -25,6 +25,8 @@ def read_file_lines(filepath): return f.readlines() def factorial(n): + if n == 1 or n == 0: + return 1 x = 1 for i in range(1, n+ 1): x *= i @@ -43,14 +45,14 @@ def is_prime(n): def sort_numbers(numbers): numbers = [] - return numbers.sort(reverse = True) + return numbers.sort(ascii) def factorial(n): - if n == 1 or n == 0: - return 1 if n < 0: return "" + if type(n) != int: + raise TypeError('Invalid input') if n < 0: raise ValueError("Invalid input type") x = 1 @@ -87,8 +89,11 @@ def tower_of_hanoi(n, source, auxiliary, target): class Person: def __init__(self, name, age): - self.name = str(name) - self.age = int(age) + if type(age) != int or type(name) != str: + raise TypeError('Age should only be integer, nae can only be a string') + self.name = name + self.age = age + if __name__ == "__main__": # Placeholder functions for Python basics, to be implemented later