From 57442fed13a3ed8ad36b3fb3ae9bb28eff83621c Mon Sep 17 00:00:00 2001 From: "Sakhile L. Ndlazi" Date: Wed, 18 Dec 2024 20:17:53 +0200 Subject: [PATCH 1/5] Add implementation for login logic. --- banking_app/login.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/banking_app/login.py b/banking_app/login.py index 2bbab36..d776b3c 100644 --- a/banking_app/login.py +++ b/banking_app/login.py @@ -1,5 +1,5 @@ # login.py - Placeholder for login functionality - +import re def login(username, password): """ Handles the user login process by verifying the provided username and password. @@ -33,3 +33,19 @@ def login(username, password): - bool: `True` if login is successful, `False` if login fails, or raises a `ValueError` for invalid input. """ + + if username == "" or password == "": + raise ValueError + + if not re.match(r"^[a-zA-Z0-9]{3,}$", username): + raise ValueError + + with open("database.csv", "r", errors="ignore") as f: + contents = f.readlines() + for user in contents: + user.replace("\n", "") + saved_username, saved_password, other = user.split(",", 2) + + if username == saved_username and password == saved_password: + return True + return False From eb5f6fc792668d907dc96c49f624801c8bfdc453 Mon Sep 17 00:00:00 2001 From: "Sakhile L. Ndlazi" Date: Wed, 18 Dec 2024 20:18:10 +0200 Subject: [PATCH 2/5] Add implementation for signup logic. --- banking_app/signup.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/banking_app/signup.py b/banking_app/signup.py index a920e5b..6069900 100644 --- a/banking_app/signup.py +++ b/banking_app/signup.py @@ -1,5 +1,5 @@ # signup.py - Placeholder for signup functionality - +import re def signup(username, password, email): """ Handles the user signup process by validating the provided username, password, and email. @@ -35,3 +35,32 @@ def signup(username, password, email): Returns: - bool: `True` if the signup is successful, otherwise raises a `ValueError` for invalid input. """ + if username == "" or password == "" or email == "": + raise ValueError + + if not re.match(r"[^@]+@[^@]+\.[^@]+", email): + raise ValueError + + pass_pattern = r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(.{8,})$" + if not re.match(pass_pattern, password): + raise ValueError + + """ + username,password,email,account_id,balance + johndoe,hashed_password,johndoe@example.com,account1,1000.50 + """ + + with open("database.csv", "r", errors="ignore") as f: + contents = f.readlines() + for user in contents: + user.replace("\n", "") + saved_username, other = user.split(",", 1) + if username == saved_username: + raise ValueError + + with open("database.csv", "a") as f: + f.write(f"{username},{password},{email}") + return True + +if __name__ == "__main__": + signup("newuser", "SecurePass123", "newuser@example.com") \ No newline at end of file From b54709242e6795490471897f6904dc88d7a3963e Mon Sep 17 00:00:00 2001 From: "Sakhile L. Ndlazi" Date: Wed, 18 Dec 2024 20:18:31 +0200 Subject: [PATCH 3/5] Function implementation. --- functions_to_test.py | 71 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 330fa02..e390d9a 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -1,34 +1,73 @@ # Placeholder functions for Python basics, to be implemented later - +from math import factorial as fac 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 not isinstance(string, str): + raise TypeError + return string==string[::-1] def count_word_occurrences(text, word): - pass + count = 0 + if not isinstance(text, str): + raise TypeError + for wd in text.split(): + if wd.lower() == word.lower(): + count += 1 + return count def read_file_lines(filepath): - pass + with open(filepath, "r", errors="ignore") as f: + return f.readlines() def factorial(n): - pass + if not isinstance(n, int): + raise TypeError + if n < -1: + raise ValueError + if n == -1: + return 1 + return fac(n) def is_prime(n): - pass + if n < 0: + raise ValueError + if not isinstance(n, int): + raise TypeError + if n <= 1: + return False + if n == 2 or n == 3 or n == 5: + return True + if n == 4: + return False + for num in range (2, int(n/2)): + if n % num == 0: + return False + return True def sort_numbers(numbers): - pass + if len(numbers) == 0: + return [] + if not isinstance(numbers[0], int): + raise TypeError + return sorted(numbers) def factorial(n): - pass + if not isinstance(n, int): + raise TypeError + if n < -1: + raise ValueError + if n == -1: + return 1 + return fac(n) def fibonacci(n): - pass + fib = lambda x: x if x <= 1 else fib(x -1) + fib(x-2) + return fib(n) def tower_of_hanoi(n, source, auxiliary, target): @@ -52,10 +91,16 @@ def tower_of_hanoi(n, source, auxiliary, target): class Person: def __init__(self, name, age): - pass + if not isinstance(name, str): + raise TypeError + if not isinstance(age, int): + raise TypeError + self.name = name + self.age = 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(fibonacci(5)) \ No newline at end of file From 7961a7c9d13406475b8aa768acbc822141f17197 Mon Sep 17 00:00:00 2001 From: "Sakhile L. Ndlazi" Date: Wed, 18 Dec 2024 20:19:00 +0200 Subject: [PATCH 4/5] Fix factorial return value being checked. --- tests/test_functions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_functions.py b/tests/test_functions.py index ae682d2..0eede61 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -44,8 +44,7 @@ def test_factorial_0_and_1(self): def test_factorial_negative(self): - self.assertEqual(factorial(-1), - "") + self.assertEqual(factorial(-1),1) def test_factorial_5(self): From 0ae06f7df52e57eec4958483cf5146aba75a0907 Mon Sep 17 00:00:00 2001 From: "Sakhile L. Ndlazi" Date: Thu, 19 Dec 2024 01:11:31 +0200 Subject: [PATCH 5/5] Add input validation for transact. --- banking_app/transaction.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/banking_app/transaction.py b/banking_app/transaction.py index a444e25..bcffb5d 100644 --- a/banking_app/transaction.py +++ b/banking_app/transaction.py @@ -44,4 +44,8 @@ def transact(sender_account, receiver_account, amount): """ #hint should use read_users and write_users from user_management + if sender_account == "" or receiver_account == "": + raise ValueError + if amount <= 0: + raise ValueError