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-311.pyc b/__pycache__/main.cpython-311.pyc new file mode 100644 index 0000000..835ae91 Binary files /dev/null and b/__pycache__/main.cpython-311.pyc differ diff --git a/main.py b/main.py index 56e2fb3..fdcd4fb 100644 --- a/main.py +++ b/main.py @@ -1,89 +1,76 @@ -""" -The Fibonacci sequence is a series of numbers where each number -is the sum of the two preceding ones, starting from 0 and 1. -The sequence looks like this: -0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... -The function below receives an integer n, which represents the n-th term -of the sequence, and must return a list from the first value up to the n-th term. - -Example: - n = 3 - return [0, 1, 1] -""" +""" The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The sequence looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The function below receives an integer n, which represents the n-th term of the sequence, and must return a list from the first value up to the n-th term. Example: n = 3 return [0, 1, 1] """ def fibonacci(n: int) -> list[int]: - - return [] - - -""" -The function below receives a list of numbers (nums) -and returns a list of even numbers from nums in ascending order. - -Example: - 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 [] - - -""" -The function below receives a list of numbers (nums) -and returns a list of odd numbers from nums in descending order. - -Example: - nums = [1, 3, 2, 27, 50, 17, 8, 4, 98, 22] - return [27, 17, 3, 1] -""" + if n <= 0: + return [] + elif n == 1: + return [0] + else: + sequence = [0, 1] + while len(sequence) < n: + next_fib = sequence[-1] + sequence[-2] + sequence.append(next_fib) + return sequence + +""" The function below receives a list of numbers (nums) and returns a list of even numbers from nums in ascending order. Example: 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]: + even_numbers = [] + for num in nums: + if num % 2 == 0: + even_numbers.append(num) + even_numbers.sort() + return even_numbers + +""" The function below receives a list of numbers (nums) and returns a list of odd numbers from nums in descending order. Example: nums = [1, 3, 2, 27, 50, 17, 8, 4, 98, 22] return [27, 17, 3, 1] """ def odd(nums: list[int]) -> list[int]: - - return [] - - -""" -This function receives a list of numbers (nums) -and returns a string that says: - - * 'Odd' if the sum of odd numbers is greater than the sum of even numbers. - * 'Even' if the sum of even numbers is greater than the sum of odd numbers. - * 'Tie' if both sums are equal. - -Example: - nums = [1, 2, 3] - sum(odd) = 4, sum(even) = 2 → returns 'Odd' -""" + odd_numbers = [] + for num in nums: + if num % 2 != 0: + odd_numbers.append(num) + odd_numbers.sort(reverse=True) + return odd_numbers + +""" This function receives a list of numbers (nums) and returns a string that says: * 'Odd' if the sum of odd numbers is greater than the sum of even numbers. * 'Even' if the sum of even numbers is greater than the sum of odd numbers. * 'Tie' if both sums are equal. Example: nums = [1, 2, 3] sum(odd) = 4, sum(even) = 2 → returns 'Odd' """ def even_vs_odd(nums: list[int]) -> str: - return "" - - -""" -This function checks if 'n' is a prime number. - -It should: - * Return True if n is prime, False otherwise. - * Raise a ValueError if n is negative or not an integer. - -Example: - is_prime(7) → True - is_prime(10) → False - is_prime(-3) → raises ValueError -""" + sum_of_evens = 0 + sum_of_odds = 0 + for num in nums: + if num % 2 == 0: + sum_of_evens += num + else: + sum_of_odds += num + + if sum_of_odds > sum_of_evens: + return 'Odd' + elif sum_of_evens > sum_of_odds: + return 'Even' + else: + return 'Tie' + +""" This function checks if 'n' is a prime number. It should: * Return True if n is prime, False otherwise. * Raise a ValueError if n is negative or not an integer. Example: is_prime(7) → True is_prime(10) → False is_prime(-3) → raises ValueError """ def is_prime(n): - return False - - -""" -This function takes in a fullname, short year, -and campus code ('jhb' or 'cpt') to generate a WeThinkCode_ email address. - -Example: - Input: ('Auston Mtabane', '2024', 'jhb') - Output: 'aumtajhb024@student.wethinkcode.co.za' - -Hint: - - Use lowercase letters. - - Look at the pattern of the example carefully. -""" + if not isinstance(n, int): + raise ValueError("Input must be an integer.") + if n < 1: + raise ValueError("Input must be a positive integer.") + if n == 1: + return False + if n == 2: + return True + if n % 2 == 0: + return False + for i in range(3, int(n ** 0.5) + 1, 2): + if n % i == 0: + return False + return True + +""" This function takes in a fullname, short year, and campus code ('jhb' or 'cpt') to generate a WeThinkCode_ email address. Example: Input: ('Auston Mtabane', '2024', 'jhb') Output: 'aumtajhb024@student.wethinkcode.co.za' Hint: - Use lowercase letters. - Look at the pattern of the example carefully. """ def generate_email(fullname: str, year: str, campus: str) -> str: - return "" + fullname_lower = fullname.lower().replace('\t', ' ') + name_parts = fullname_lower.split() + first_name_prefix = name_parts[0][:2] + last_name_prefix = name_parts[-1][:2] + campus_prefix = campus[:3] + year_suffix = year[-3:] + local_part = f"{first_name_prefix}{last_name_prefix}{campus_prefix}{year_suffix}" + return f"{local_part}@student.wethinkcode.co.za" \ No newline at end of file diff --git a/tests/__pycache__/test_main.cpython-311.pyc b/tests/__pycache__/test_main.cpython-311.pyc new file mode 100644 index 0000000..89e6788 Binary files /dev/null and b/tests/__pycache__/test_main.cpython-311.pyc differ