diff --git a/__pycache__/student_code.cpython-310.pyc b/__pycache__/student_code.cpython-310.pyc new file mode 100644 index 0000000..7934728 Binary files /dev/null and b/__pycache__/student_code.cpython-310.pyc differ diff --git a/__pycache__/student_code.cpython-313.pyc b/__pycache__/student_code.cpython-313.pyc new file mode 100644 index 0000000..f19027e Binary files /dev/null and b/__pycache__/student_code.cpython-313.pyc differ diff --git a/student_code.py b/student_code.py index d7fdfd0..9bfed02 100644 --- a/student_code.py +++ b/student_code.py @@ -1,106 +1,77 @@ """Learning Outcome: Functions""" def sum_of_squares(n: int): - """ - Calculate the sum of the squares of all integers from 1 to n. - - Parameters: - n (int): A non-negative integer up to which the squares will be summed. - - Returns: - int: The sum of the squares of all integers from 1 to n. - - Raises: - ValueError: If n is a negative integer. - """ - pass + + if n < 0: + raise ValueError + + results = [] + for i in range(n+1): + square_number = i ** 2 + results.append(square_number) + + return sum(results) def evaluate_performance(grades: list, min_pass: int): - """ - Evaluate the performance based on a list of grades and a minimum passing grade. - - Parameters: - grades (list): A list of integers representing student grades. - min_pass (int): The minimum average grade required to pass. - - Returns: - str: "Pass" if the average grade is greater than or equal to min_pass, otherwise "Fail". - """ - pass + + if (sum(grades) / len(grades)) >= min_pass: + return "Pass" + else: + return "Fail" def calculate_cumulative_performance(scores: dict): - """ - Calculate the cumulative performance based on student scores. - - Parameters: - scores (dict): A dictionary where keys are subject names and values are the corresponding scores. - - Returns: - dict: A dictionary containing the average score and a list of subjects where the score is below average. - """ - pass + + if not scores: + return {"average": 0, "below_average": []} + + total_score = sum(scores.values()) + average_score = round((total_score / len(scores)), 2) + + below_average_score = [subject for subject, score in scores.items() if score < average_score] + + return {"average": average_score, "below_average": below_average_score} + def analyze_improvement(scores: list): - """ - Analyze the improvement trend based on a list of scores. - Parameters: - scores (list): A list of integers representing scores over time. + results = {} + for i in scores: + if i < i + 1: + results["trend"] = "positive" + results["improved"] = True + elif i > i + 1: + results["trend"] = "negative" + results["improved"] = False + else: + results["trend"] = "neutral" + results["improved"] = False + + return results - Returns: - dict: A dictionary containing the trend of improvement ("positive", "negative", or "neutral") - and a boolean indicating whether there has been an improvement. - """ - pass def rank_students(students: list[tuple[str, int]]): - """ - Rank students based on their scores. - - Parameters: - students (list): A list of tuples where each tuple contains a student's name and their score. + + return sorted(students, key=lambda student: student[1], reverse=True) - Returns: - list: A sorted list of tuples in descending order based on scores. - """ - pass """Learning Outcome: Basic Loops""" def even_numbers(n: int): - """ - Generate a list of even numbers from 1 to n. + + return [num for num in range(1, n + 1) if num % 2 == 0] - Parameters: - n (int): The upper limit for generating even numbers. - - Returns: - list: A list of even integers from 1 to n. - """ - pass def odd_numbers(n: int): - """ - Generate a list of odd numbers from 1 to n. + + return [num for num in range(1, n + 1) if num % 2 != 0] - Parameters: - n (int): The upper limit for generating odd numbers. - - Returns: - list: A list of odd integers from 1 to n. - """ - pass def sum_multiples_of_num(num: int, length: int): - """ - Calculate the sum of multiples of a given number up to a specified length. + + sum = 0 + for i in range(1, length + 1): + sum += i * num - Parameters: - num (int): The number whose multiples are to be summed. - length (int): The upper limit for the range of multiples. + return sum - Returns: - int: The sum of multiples of num from 1 to length. - """ - pass def skip_num(n: int, length: int): """ @@ -113,210 +84,134 @@ def skip_num(n: int, length: int): Returns: list: A list of integers from 1 to length, excluding n. """ - pass + return [num for num in range(1, length + 1) if num != n] -def break_test(n: int, length: int): - """ - Generate a list of numbers from 1 to length, stopping when a specific number is encountered. - Parameters: - n (int): The number at which to stop adding to the list. - length (int): The upper limit for generating numbers. +def break_test(n: int, length: int): + + results = [] + for i in range(1, length + 1): + if i == n: + break + + results.append(i) + return results - Returns: - list: A list of integers from 1 to length, excluding n and stopping before it. - """ - pass def sum_numbers_until_zero(nums: list): - """ - Calculate the sum of numbers in a list until a zero is encountered. - - Parameters: - nums (list): A list of integers. - - Returns: - int: The sum of integers in the list up to (but not including) the first zero. - """ - pass + + result = [] + for n in nums: + if 0 == n: + break + result.append(n) + return sum(result) def count_positive_numbers(nums: list): - """ - Count the number of positive integers in a list. + + count = 0 + for n in nums: + if n > 0: + count += 1 + return count - Parameters: - nums (list): A list of integers. - - Returns: - int: The count of positive integers in the list. - """ - pass def sum_dictionary_values(dictionary: dict): - """ - Calculate the sum of all values in a dictionary. - - Parameters: - dictionary (dict): A dictionary with numeric values. - - Returns: - int: The sum of all values in the dictionary. - """ - pass + + return sum(dictionary.values()) def sum_unique_elements(elements: list): - """ - Calculate the sum of unique elements in a list. + + result = [] + for el in elements: + if el not in result: + result.append(el) + return sum(result) - Parameters: - elements (list): A list of integers. - - Returns: - int: The sum of unique integers in the list. - """ - pass def skip_divisible_by_num(n: int, length: int): - """ - Generate a list of numbers from 1 to length, skipping those that are divisible by a specific number. - - Parameters: - n (int): The number to skip multiples of. - length (int): The upper limit for generating numbers. - - Returns: - list: A list of integers from 1 to length, excluding those divisible by n. - """ - pass + + result = [] + for i in range(1, length + 1): + if i % n == 0: + continue + result.append(i) + return result """Learning Outcome: Processing Data""" def square_numbers(nums: list): - """ - Calculate the square of each number in a list. - - Parameters: - nums (list): A list of integers. - - Returns: - list: A list containing the squares of the input integers. - """ - pass - -def transform_string(input: str, transform: str): - """ - Transform a string based on the specified transformation type. - - Parameters: - input (str): The string to be transformed. - transform (str): The type of transformation ('capitalize', 'upper', 'lower'). - - Returns: - str: The transformed string. - - Raises: - ValueError: If the transformation type is unknown. - """ - pass + + return [n**2 for n in nums] + +def transform_string(input: str, transform): + + if transform == "capitalize": + return input.capitalize() + elif transform == "uppercase": + return input.upper() + elif transform == "lowercase": + return input.lower() + else: + raise ValueError def sum_and_average(nums: list[int]): - """ - Calculate the sum and average of a list of numbers. - - Parameters: - nums (list[int]): A list of integers. - - Returns: - tuple: A tuple containing the sum and average of the numbers. - """ - pass + + sum_of_nums = sum(nums) + average = sum_of_nums / len(nums) + return sum_of_nums, average def word_frequency_count(words: list[str]): - """ - Count the frequency of each word in a list. - - Parameters: - words (list[str]): A list of words. - - Returns: - dict: A dictionary with words as keys and their frequencies as values. - """ - pass + + result = {} + for w in words: + result[w] = result.get(w, 0) + 1 + return result def filter_even_numbers(nums: list[int]): - """ - Filter out even numbers from a list. - - Parameters: - nums (list[int]): A list of integers. - - Returns: - list: A list containing only the even integers from the input list. - """ - pass + + return [n for n in nums if n % 2 == 0] """Learning Outcome: Simple Algorithms(Problem Solving)""" def find_median(nums: list[int]): - """ - Find the median of a list of numbers. - - Parameters: - nums (list[int]): A list of integers. - - Returns: - float: The median value of the list. - - Raises: - ValueError: If the list is empty. - """ - pass + + if not nums: + raise ValueError + + nums.sort() + length = len(nums) + mid_nums = length // 2 + + if length % 2 == 0: + return (nums[mid_nums - 1] + nums[mid_nums]) / 2 + else: + return float(nums[mid_nums]) def reverse_string(input: str): - """ - Reverse the given string. - - Parameters: - input (str): The string to be reversed. - - Returns: - str: The reversed string. - """ - pass + + return input[::-1] def largest_number(nums: list[int]): - """ - Find the largest number in a list. - - Parameters: - nums (list[int]): A list of integers. - - Returns: - int or None: The largest number in the list, or None if the list is empty. - """ - pass + + if not nums: + return None + return max(nums) def is_prime(n: int): - """ - Check if a number is prime. - - Parameters: - n (int): The number to check. - - Returns: - bool: True if the number is prime, False otherwise. - """ - pass + + 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 def count_character_occurrences(word_sentence: str, char_count: str): - """ - Count the occurrences of a character in a given sentence. - - Parameters: - word_sentence (str): The sentence in which to count occurrences. - char_count (str): The character to count. - - Returns: - int: The number of occurrences of the character in the sentence. - """ - pass \ No newline at end of file + + return word_sentence.count(char_count) \ No newline at end of file diff --git a/tests/test.py b/test.py similarity index 99% rename from tests/test.py rename to test.py index 4966190..777da1b 100644 --- a/tests/test.py +++ b/test.py @@ -95,7 +95,7 @@ def test_square_numbers(self): class InputTransformation(unittest.TestCase): def test_transform_string(self): text = 'hello, world' - transform = ['Uppercase', 'Capitalize', 'Lowercase'] + transform = ['uppercase', 'capitalize', 'lowercase'] self.assertEqual(transform_string(text, transform[0]), 'HELLO, WORLD', "Uppercase transformation test failed.") self.assertEqual(transform_string(text, transform[1]), 'Hello, world', "Capitalize transformation test failed.") self.assertEqual(transform_string(text, transform[2]), text, "Lowercase transformation test failed.")