diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e9e6a80 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + "./tests", + "-p", + "test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/__pycache__/main.cpython-310.pyc b/__pycache__/main.cpython-310.pyc new file mode 100644 index 0000000..98aea52 Binary files /dev/null and b/__pycache__/main.cpython-310.pyc differ diff --git a/main.py b/main.py index 56e2fb3..55a0b2d 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +from typing import List +import math """ The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. @@ -12,8 +14,18 @@ return [0, 1, 1] """ def fibonacci(n: int) -> list[int]: + if not isinstance(n, int): + raise ValueError("n must be int") + if n <= 0: + return [] + seq = [0] + if n == 1: + return seq + seq.append(1) + while len(seq) < n: + seq.append(seq[-1] + seq[-2]) - return [] + return seq """ @@ -24,8 +36,9 @@ def fibonacci(n: int) -> list[int]: nums = [1, 3, 2, 27, 50, 17, 8, 4, 98, 22] return [2, 4, 8, 22, 50, 98] """ -def even(nums: list[int]) -> list[int]: - return [] +def even(nums: list[int]) -> list[int]: + ev=[ x for x in nums if isinstance(x, int) and x % 2 == 0] + return sorted(ev) """ @@ -37,8 +50,8 @@ def even(nums: list[int]) -> list[int]: return [27, 17, 3, 1] """ def odd(nums: list[int]) -> list[int]: - - return [] + od = [x for x in nums if isinstance(x, int) and x % 2 != 0] + return sorted(od, reverse=True) """ @@ -54,7 +67,13 @@ def odd(nums: list[int]) -> list[int]: sum(odd) = 4, sum(even) = 2 → returns 'Odd' """ def even_vs_odd(nums: list[int]) -> str: - return "" + ev_sum = sum(even(nums)) + od_sum = sum(odd(nums)) + if ev_sum > od_sum: + return "Even" + if od_sum > ev_sum: + return "Odd" + return "Tie" """ @@ -69,8 +88,24 @@ def even_vs_odd(nums: list[int]) -> str: is_prime(10) → False is_prime(-3) → raises ValueError """ -def is_prime(n): - return False +def is_prime(n)->bool: + if not isinstance(n,int): + raise ValueError("n must be an integer") + if n < 0: + raise ValueError("n must be non-negative") + if n < 2: + return False + if n ==2: + return True + if n % 2==0: + return False + i = 3 + limit=int(math.sqrt(n))+1 + while i <= limit: + if n % i == 0: + return False + i += 2 + return True """ @@ -86,4 +121,17 @@ def is_prime(n): - Look at the pattern of the example carefully. """ def generate_email(fullname: str, year: str, campus: str) -> str: - return "" + fullname_ = fullname.split(" ") + year_=year[1:] + name=fullname_[0][:2] + name = name.lower() + surname=fullname_[-1][:3] + surname=surname.lower() + email=name + surname + campus + year_ + "@student.wethinkcode.co.za" + return email + + + + + + diff --git a/tests/__pycache__/test_main.cpython-310.pyc b/tests/__pycache__/test_main.cpython-310.pyc new file mode 100644 index 0000000..0d2bd49 Binary files /dev/null and b/tests/__pycache__/test_main.cpython-310.pyc differ diff --git a/tests/test_main.py b/tests/test_main.py index 8b66f19..b0c8684 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -87,10 +87,10 @@ class TestGenerateEmail(unittest.TestCase): def test_generate_email(self): self.assertEqual(generate_email('Auston Mtabane','2024','jhb'), 'aumtajhb024@student.wethinkcode.co.za') self.assertEqual(generate_email('Neo Phukubye','2025','jhb'), 'nephujhb025@student.wethinkcode.co.za') - self.assertEqual(generate_email('Kwenzakele Shongwe','2025','jhb'), 'kwshojhb025@student.wethinkcode.co.za') + self.assertEqual(generate_email('Kwenzakele Shongwe','2025','jhb'), 'kwshojhb025@student.wethinkcode.co.za') self.assertEqual(generate_email('Khatisani Mongwe','2025','jhb'), 'khmonjhb025@student.wethinkcode.co.za') self.assertEqual(generate_email('Agnes Chauke','2025','jhb'), 'agchajhb025@student.wethinkcode.co.za') - self.assertEqual(generate_email("Karabo Mahlare",'2025','jhb'), 'kamahjhb025@student.wethinkcode.co.za') + self.assertEqual(generate_email("Karabo Mahlare",'2025','jhb'), 'kamahjhb025@student.wethinkcode.co.za') def test_generate_email_edge(self): self.assertEqual(generate_email('Oriel Kopano Dibakoane','2027','cpt'), 'ordibcpt027@student.wethinkcode.co.za')