diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..25f7b5b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.testing.unittestArgs": ["-v", "-s", ".", "-p", "test_*.py"], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} diff --git a/__pycache__/main.cpython-313.pyc b/__pycache__/main.cpython-313.pyc new file mode 100644 index 0000000..9f96e48 Binary files /dev/null and b/__pycache__/main.cpython-313.pyc differ diff --git a/__pycache__/test_main.cpython-313.pyc b/__pycache__/test_main.cpython-313.pyc new file mode 100644 index 0000000..6d8b2fd Binary files /dev/null and b/__pycache__/test_main.cpython-313.pyc differ diff --git a/main.py b/main.py index 56e2fb3..989efa5 100644 --- a/main.py +++ b/main.py @@ -11,9 +11,21 @@ n = 3 return [0, 1, 1] """ -def fibonacci(n: int) -> list[int]: - - return [] +def fibonacci(num: int) -> list[int]: # what does this line do: + first_fibonacci = 0 + second_fibonacci = 1 + list_fibo = [first_fibonacci, second_fibonacci] + + + if num <= 0: #checks if the nth the is not less then 0 + return [] + elif num == 1: #if the nth term is 1 you must output first_fibonacci + return [first_fibonacci] + else: + while len(list_fibo) < num: + n= list_fibo[- 1] + list_fibo[-2] + list_fibo.append(n) + return list_fibo """ @@ -25,7 +37,16 @@ def fibonacci(n: int) -> list[int]: return [2, 4, 8, 22, 50, 98] """ def even(nums: list[int]) -> list[int]: - return [] + count = 0 + even_list = [] + for i in nums: + if i % 2 == 0: + even_list.append(i) + count = count + 1 + even_list.sort() + return even_list + + """ @@ -37,8 +58,14 @@ def even(nums: list[int]) -> list[int]: return [27, 17, 3, 1] """ def odd(nums: list[int]) -> list[int]: - - return [] + count = 0 + odd_list = [] + for i in nums: + if i % 2 == 1: + odd_list.append(i) + count = count + 1 + odd_list.sort() + return odd_list[::-1] """ @@ -54,7 +81,38 @@ def odd(nums: list[int]) -> list[int]: sum(odd) = 4, sum(even) = 2 → returns 'Odd' """ def even_vs_odd(nums: list[int]) -> str: - return "" + count = 0 + sum_evens = 0 + sum_odds = 0 + list_even = [] + list_odds = [] + empty_list = [] + for i in nums: + if i % 2 == 0: + list_even.append(i) + sum_evens = sum(list_even) + count = count + 1 + + for i in nums: + if i % 2 == 1: + list_odds.append(i) + sum_odds = sum(list_odds) + count = count + 1 + + + if sum_evens > sum_odds: + result = 'Even' + elif sum_odds > sum_evens : + result = 'Odd' + else: + return 'Tie' + + if list_even == empty_list and list_odds == empty_list: + result = 'Tie' + + return result + + """ @@ -70,7 +128,16 @@ def even_vs_odd(nums: list[int]) -> str: is_prime(-3) → raises ValueError """ def is_prime(n): - return False + if isinstance(n, (list, dict, str)) or n < 0: + raise ValueError + else: + if n % 2 == 1: + result = True + elif n == 2: + result = True + else: + result = False + return result """ @@ -86,4 +153,21 @@ def is_prime(n): - Look at the pattern of the example carefully. """ def generate_email(fullname: str, year: str, campus: str) -> str: - return "" + *name, surname = fullname.split() + name_1 = name[0] + if name_1 == name[0]: + return f'{name_1[0:2].lower()}{surname.lower()[0:3]}{campus}{year[1:4]}@student.wethinkcode.co.za' + + + return f'{name[0:2]}{surname.lower()[0:3]}{campus}{year[1:4]}@student.wethinkcode.co.za' + + + + + +print(even([16,9,4,8,12,4])) +# print(odd([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])) +# print(fibonacci(-1)) +# print(even_vs_odd([5,3,8])) +# print(generate_email('Oriel Kopano Dibakoane','2027','cpt')) +print(is_prime(0)) \ No newline at end of file diff --git a/tests/test_main.py b/tests/test_main.py index 8b66f19..82ded96 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,5 +1,6 @@ -from main import * import unittest +from main import * + class TestFibonacci(unittest.TestCase): @@ -98,7 +99,8 @@ def test_generate_email_edge(self): - +if __name__ == '__main__': + unittest.main()