From 3dccaa7972f68ddb82154392203a1a6cde8cb9e8 Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Wed, 18 Dec 2024 20:33:23 +0200 Subject: [PATCH 1/7] ... --- banking_app/login.py | 27 ++++++++++++++++++++ functions_to_test.py | 61 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/banking_app/login.py b/banking_app/login.py index 2bbab36..8483b2a 100644 --- a/banking_app/login.py +++ b/banking_app/login.py @@ -1,3 +1,4 @@ +import string # login.py - Placeholder for login functionality def login(username, password): @@ -33,3 +34,29 @@ def login(username, password): - bool: `True` if login is successful, `False` if login fails, or raises a `ValueError` for invalid input. """ + special_char = string.punctuation + trigger = 1 + while trigger == 1: + if special_char in username: + raise ValueError("Incorrect username or password!") + if len(username) == 0 or len(password) == 0: + raise ValueError("Incorrect username or password!") + else: + trigger == 0 + try + db_password = "" + with open("database.csv", "r") as file: + db = file.readlines(1) + + for l in db: + if username in l: + db_password == l[1] + return True + + if db_password == password: + return True + else: + return False + + except FileNotFoundError: + print("File does not exist!1") diff --git a/functions_to_test.py b/functions_to_test.py index 330fa02..c5af179 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -1,28 +1,63 @@ # Placeholder functions for Python basics, to be implemented later 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 + if type(string) == type("stirng"): + if string == string[::-1]: + return True + else: + return False + else: + raise TypeError("Invalid input!") def count_word_occurrences(text, word): - pass + if type(text) == type(word): + text = text.upper() + word = word.upper() + count = text.count(word) + return count + else: + raise TypeError("Invalid type entered") def read_file_lines(filepath): - pass + try: + with open(filepath, "r") as f: + file = f.readlines() + except FileNotFoundError as e: + print(e) + + def factorial(n): - pass + answer = 1 + for i in range(1, n + 1): + if i != n: + print(i) + answer *= i * i + 1 + else: + answer *= n def is_prime(n): - pass - + try: + if type(n) == type(0): + outcome = is_prime(n) + return outcome + except TypeError as e: + print(e) + except RecursionError as e: + print(e) + def sort_numbers(numbers): - pass + try: + if type(numbers) == type([]): + return sorted(numbers) + except TypeError: + print("invalid type") def factorial(n): pass @@ -52,7 +87,13 @@ def tower_of_hanoi(n, source, auxiliary, target): class Person: def __init__(self, name, age): - pass + self.name = name + self.age = age + + if type(name) != type("string"): + raise TypeError + if type(age) != type(0): + raise TypeError if __name__ == "__main__": From 3f4b10b8662081ea09a3150c5e0f553db54d1d34 Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Wed, 18 Dec 2024 22:04:33 +0200 Subject: [PATCH 2/7] fibonacci complete --- functions_to_test.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index c5af179..7f62f03 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -34,13 +34,7 @@ def read_file_lines(filepath): def factorial(n): - answer = 1 - for i in range(1, n + 1): - if i != n: - print(i) - answer *= i * i + 1 - else: - answer *= n + pass def is_prime(n): try: @@ -63,7 +57,15 @@ def factorial(n): pass def fibonacci(n): - pass + fibo_list = [0, 1] + + for i in range(n - 1): + fibo_list.append(fibo_list[len(fibo_list) - 2] + fibo_list[len(fibo_list) -1]) + + return fibo_list[n] + + +print(fibonacci(15)) def tower_of_hanoi(n, source, auxiliary, target): From ef8d8f7581334067fcca57f10021fd45224e2300 Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Wed, 18 Dec 2024 22:39:24 +0200 Subject: [PATCH 3/7] complete sorted func --- functions_to_test.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 7f62f03..f6ebbac 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -31,8 +31,6 @@ def read_file_lines(filepath): except FileNotFoundError as e: print(e) - - def factorial(n): pass @@ -47,11 +45,13 @@ def is_prime(n): print(e) def sort_numbers(numbers): - try: - if type(numbers) == type([]): + if numbers == []: + return [] + for i in numbers: + if type(i) == type(0): return sorted(numbers) - except TypeError: - print("invalid type") + else: + raise TypeError("Invalid list elements") def factorial(n): pass @@ -63,9 +63,6 @@ def fibonacci(n): fibo_list.append(fibo_list[len(fibo_list) - 2] + fibo_list[len(fibo_list) -1]) return fibo_list[n] - - -print(fibonacci(15)) def tower_of_hanoi(n, source, auxiliary, target): From 8754bd50a5ea2bd79e7636e1c157d39fc44c973a Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Wed, 18 Dec 2024 23:05:23 +0200 Subject: [PATCH 4/7] fixed is prime func --- functions_to_test.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index f6ebbac..8d6e51c 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -3,9 +3,11 @@ def add_numbers(a, b): return a + b + def find_maximum(a, b, c): return max(a, b, c) + def is_palindrome(string): if type(string) == type("stirng"): if string == string[::-1]: @@ -14,6 +16,7 @@ def is_palindrome(string): return False else: raise TypeError("Invalid input!") + def count_word_occurrences(text, word): if type(text) == type(word): @@ -23,6 +26,7 @@ def count_word_occurrences(text, word): return count else: raise TypeError("Invalid type entered") + def read_file_lines(filepath): try: @@ -31,19 +35,28 @@ def read_file_lines(filepath): except FileNotFoundError as e: print(e) + def factorial(n): pass + def is_prime(n): - try: - if type(n) == type(0): - outcome = is_prime(n) - return outcome - except TypeError as e: - print(e) - except RecursionError as e: - print(e) - + count = 0 + if type(n) == type(0): + if n == 1: + return False + elif n < 0: + raise ValueError("Prime not defined for negative numbers") + for i in range(1, n + 1): + if n % i == 0: + count += 1 + if count > 2: + return False + else: + return True + elif type(n) != type(0): + raise TypeError("Invalid input!") + def sort_numbers(numbers): if numbers == []: return [] @@ -53,7 +66,7 @@ def sort_numbers(numbers): else: raise TypeError("Invalid list elements") -def factorial(n): +def factorial(n): ##?? pass def fibonacci(n): From 59526916e104255e4bdccd0d62aa924cf2a208e8 Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Wed, 18 Dec 2024 23:53:09 +0200 Subject: [PATCH 5/7] passed all but 1 test case for factorial func, the negative test case! --- functions_to_test.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 8d6e51c..5e422c7 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -37,8 +37,20 @@ def read_file_lines(filepath): def factorial(n): - pass + fact = n + + if type(n) != type(0): + raise TypeError("Invalid type!") + elif n == 0: + return 1 + elif n < 0: + raise ValueError("Invalid number!") + for i in range(1, n): + fact = fact * i + return fact + +print(factorial(0)) def is_prime(n): count = 0 @@ -57,6 +69,7 @@ def is_prime(n): elif type(n) != type(0): raise TypeError("Invalid input!") + def sort_numbers(numbers): if numbers == []: return [] @@ -66,8 +79,6 @@ def sort_numbers(numbers): else: raise TypeError("Invalid list elements") -def factorial(n): ##?? - pass def fibonacci(n): fibo_list = [0, 1] From 53041dc498f00f17f9471bca110f81f97ecb92f3 Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Thu, 19 Dec 2024 10:25:25 +0200 Subject: [PATCH 6/7] read_file_lines func complete --- banking_app/login.py | 2 +- functions_to_test.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/banking_app/login.py b/banking_app/login.py index 8483b2a..21ede14 100644 --- a/banking_app/login.py +++ b/banking_app/login.py @@ -43,7 +43,7 @@ def login(username, password): raise ValueError("Incorrect username or password!") else: trigger == 0 - try + try: db_password = "" with open("database.csv", "r") as file: db = file.readlines(1) diff --git a/functions_to_test.py b/functions_to_test.py index 5e422c7..f289d74 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -29,11 +29,12 @@ def count_word_occurrences(text, word): def read_file_lines(filepath): - try: + if filepath: with open(filepath, "r") as f: file = f.readlines() - except FileNotFoundError as e: - print(e) + return file + else: + raise FileNotFoundError("File not found") def factorial(n): @@ -50,8 +51,6 @@ def factorial(n): return fact -print(factorial(0)) - def is_prime(n): count = 0 if type(n) == type(0): @@ -88,6 +87,7 @@ def fibonacci(n): return fibo_list[n] + def tower_of_hanoi(n, source, auxiliary, target): """ @@ -106,6 +106,7 @@ def tower_of_hanoi(n, source, auxiliary, target): >>> tower_of_hanoi(3, 'A', 'B', 'C') [('A', 'C'), ('A', 'B'), ('C', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('A', 'C')] """ + pass class Person: @@ -122,4 +123,5 @@ 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 + print(read_file_lines("nkulu.txt")) \ No newline at end of file From 978a91efdd6956cb17c54dfe94c1fd38620ab73d Mon Sep 17 00:00:00 2001 From: NkululekoNtuli Date: Thu, 19 Dec 2024 23:59:50 +0200 Subject: [PATCH 7/7] tried to fix the problem caursing checking errors on github --- banking_app/login.py | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/banking_app/login.py b/banking_app/login.py index 21ede14..2bbab36 100644 --- a/banking_app/login.py +++ b/banking_app/login.py @@ -1,4 +1,3 @@ -import string # login.py - Placeholder for login functionality def login(username, password): @@ -34,29 +33,3 @@ def login(username, password): - bool: `True` if login is successful, `False` if login fails, or raises a `ValueError` for invalid input. """ - special_char = string.punctuation - trigger = 1 - while trigger == 1: - if special_char in username: - raise ValueError("Incorrect username or password!") - if len(username) == 0 or len(password) == 0: - raise ValueError("Incorrect username or password!") - else: - trigger == 0 - try: - db_password = "" - with open("database.csv", "r") as file: - db = file.readlines(1) - - for l in db: - if username in l: - db_password == l[1] - return True - - if db_password == password: - return True - else: - return False - - except FileNotFoundError: - print("File does not exist!1")