From 0b1a2ebdc35d4c0041fc09755c6eef333da300d4 Mon Sep 17 00:00:00 2001 From: Fatima Agnes Date: Mon, 13 Oct 2025 21:30:42 +0200 Subject: [PATCH] practice exercises --- .vscode/settings.json | 11 ++ __pycache__/main.cpython-311.pyc | Bin 0 -> 3914 bytes main.py | 153 +++++++++----------- tests/__pycache__/test_main.cpython-311.pyc | Bin 0 -> 11401 bytes 4 files changed, 81 insertions(+), 83 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 __pycache__/main.cpython-311.pyc create mode 100644 tests/__pycache__/test_main.cpython-311.pyc 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 0000000000000000000000000000000000000000..835ae9198c0aa99a6f4428c386fb77448d19e619 GIT binary patch literal 3914 zcmb_e-D_LP6`%W|?`zqzB}>_o_}Y%iir6BykRh)27|{)MYoa4VNT^SkGW%Q zEtD2j;S2G$;6tj|J@XDt7ify#Nz)CQu6Js>VMh)0ZP6!~tO9QrOhzJeVAd#>3%YG#jG z-uo}Kz5f7-^R|$|;0Gn2$GRF92>mkK+m46r)$u(unbsB!>Jo6|n@g$l3%3BK#zI;# zCzXYpI=(_6@(WjTsX~^q0;*gvzytn zvK<_%28SBap$flMtSrAEf6CONQ`ScfDg4`;k8YMfs!3yYX{;)Z{nLed`@5Gl-KZ~S z_3p#o$QI$o8C^&aox|}0{sjpSfqz3(|HnwAyqei z0t7rg5ZSoV=pASx-ZjREB{TxTQt07_CFRit>q0|{?MOr0(ojXHNhj;l$*OenUk$Nu zM~rNXk(wB-i_xkWZ3HMKyOxum)jY=Qcw{-Wl_oQ5bV86wrw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-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 0000000000000000000000000000000000000000..89e67880ec018189a1a4931f79b642c51fd8a7fb GIT binary patch literal 11401 zcmeHNTWs6b873vsI=1CY95*%X$W|_PBFFJ9cH(AD(lmCOHL;R3PP%$V1zKb*)w1N0 zQqpM2kcT~_MS%r-Xw4zU9a_`q;@ZpTM7S%sMF-2V8ZVaJeX#8@R|OTpZ=92d;)q zxaufZBXD^(;c`>1Cg5t`go}udON_rIi-YmA8MZ$V$_A_~7CLMq*}Jk(qUf z?C+Rar|1B5;RMZcq6@@unWh;@Nt}+%$74b$6ba>yaqT zECZ|YG4Os;kRm!3*pFcyJ}ImPTb&%IS&qmqIBJsQDwnjth}=iD^{LJx$!(P+GBB+q zki{vGanxA~IG28oR>*E7Q6Nai>C57kpV-l5GzPgIp%FQo03J9pje?>`a$@T3q!7*2 zU(GMz12*GQr07)z@9+EJ+^M5;mw`{2yDB6`gt_xEX;O|{ljbJlq2!VjODJ>8Hxl7^ z>|jC^4=&3>C=m%s2Zx78=Mtb73dP`(5Qz;g-^es$T~JvPqS0V15l-za)1AR`Y8`Yy zc^BX|^9}KQO*+0J9ryY4$jZ*uo$0sJA&vCwq+ccdHq^CkX-OjkIvG&OfR( zIvG{TsD-ZfO&CQZFX-e2HD8`UXBc}8{|+d%X*lSU!>R>U*@{WWL6WORNt{J9>#UNf zX1W7e4~*6CsY2Ccike=a;xs951C(p}=!$pMtM0pC6pdWe$wif1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