diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f0064cb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + ".", + "-p", + "*test*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/student_code.py b/student_code.py index d7fdfd0..cf6bc87 100644 --- a/student_code.py +++ b/student_code.py @@ -1,4 +1,5 @@ -"""Learning Outcome: Functions""" +import math +import statistics def sum_of_squares(n: int): """ Calculate the sum of the squares of all integers from 1 to n. @@ -12,8 +13,13 @@ def sum_of_squares(n: int): Raises: ValueError: If n is a negative integer. """ - pass + sumnum = [] + for i in range(1, n +1): + i = pow(i,2) + sumnum.append(i) + return (sum(sumnum)) +# sum_of_squares(5) def evaluate_performance(grades: list, min_pass: int): """ Evaluate the performance based on a list of grades and a minimum passing grade. @@ -25,7 +31,13 @@ def evaluate_performance(grades: list, min_pass: int): Returns: str: "Pass" if the average grade is greater than or equal to min_pass, otherwise "Fail". """ - pass + dict = {} + for i in grades: + if i >= min_pass: + return f"{i} Pass" + else: + return f"{i} Fail" +# print(evaluate_performance([100, 70, 90, 75, 65], 75)) def calculate_cumulative_performance(scores: dict): """ @@ -37,7 +49,11 @@ def calculate_cumulative_performance(scores: dict): Returns: dict: A dictionary containing the average score and a list of subjects where the score is below average. """ - pass + lis = [] + for i in scores.values(): + lis.append(i) + return sum(lis)/len(lis) +# print(calculate_cumulative_performance({"Math": 65, "English": 85, "Science": 50})) def analyze_improvement(scores: list): """ @@ -50,7 +66,22 @@ def analyze_improvement(scores: list): dict: A dictionary containing the trend of improvement ("positive", "negative", or "neutral") and a boolean indicating whether there has been an improvement. """ - pass + dictionary = {} + positve = False + negative = False + neutral = False + for i in scores: + if i > 0: + positve = True + return positve + elif i < 0: + negative = True + return negative + elif i == 0: + neutral = True + return neutral + + def rank_students(students: list[tuple[str, int]]): """ @@ -75,7 +106,12 @@ def even_numbers(n: int): Returns: list: A list of even integers from 1 to n. """ - pass + even = [] + for i in range(1,n+1): + if i % 2 == 0: + even.append(i) + return even +# print(even_numbers(10)) def odd_numbers(n: int): """ @@ -87,8 +123,12 @@ def odd_numbers(n: int): Returns: list: A list of odd integers from 1 to n. """ - pass - + odd = [] + for i in range(1,n+1): + if i % 2 != 0: + odd.append(i) + return odd +# print(odd_numbers(10)) def sum_multiples_of_num(num: int, length: int): """ Calculate the sum of multiples of a given number up to a specified length. @@ -100,7 +140,11 @@ def sum_multiples_of_num(num: int, length: int): Returns: int: The sum of multiples of num from 1 to length. """ - pass + lis = [] + for i in range(1,length+1): + lis.append(num * i) + return sum(lis) +# print(sum_multiples_of_num(9,2)) def skip_num(n: int, length: int): """ @@ -113,8 +157,12 @@ def skip_num(n: int, length: int): Returns: list: A list of integers from 1 to length, excluding n. """ - pass - + lis = [] + for i in range(1,length+1): + if i != n: + print(lis.append(i)) + return lis +# print(skip_num(2,5)) def break_test(n: int, length: int): """ Generate a list of numbers from 1 to length, stopping when a specific number is encountered. @@ -126,8 +174,12 @@ def break_test(n: int, length: int): Returns: list: A list of integers from 1 to length, excluding n and stopping before it. """ - pass - + lis = [] + for i in range(1,length): + if i <= n: + print(lis.append(i)) + return lis +# print(break_test(3,6)) def sum_numbers_until_zero(nums: list): """ Calculate the sum of numbers in a list until a zero is encountered. @@ -150,7 +202,12 @@ def count_positive_numbers(nums: list): Returns: int: The count of positive integers in the list. """ - pass + count = 0 + for num in nums: + if num > 0: + count += 1 + print(count) +# print(count_positive_numbers([-1,2,3,-9,-10,3])) def sum_dictionary_values(dictionary: dict): """ @@ -162,7 +219,11 @@ def sum_dictionary_values(dictionary: dict): Returns: int: The sum of all values in the dictionary. """ - pass + lis = [] + for value in dictionary.values(): + lis.append(value) + print(sum(lis)) +# print(sum_dictionary_values( {'a': 1, 'b': 2, 'c': 3})) def sum_unique_elements(elements: list): """ @@ -174,8 +235,12 @@ def sum_unique_elements(elements: list): Returns: int: The sum of unique integers in the list. """ - pass - + lis = [] + for element in elements: + if element not in lis: + lis.append(element) + print(sum(lis)) +# sum_unique_elements([1,1,1,2,4,5,4]) 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. @@ -201,8 +266,11 @@ def square_numbers(nums: list): Returns: list: A list containing the squares of the input integers. """ - pass - + lis = [] + for num in nums: + lis.append(num ** 2) + print(lis) +#print(square_numbers([2,3,5,7,9])) def transform_string(input: str, transform: str): """ Transform a string based on the specified transformation type. @@ -229,7 +297,8 @@ def sum_and_average(nums: list[int]): Returns: tuple: A tuple containing the sum and average of the numbers. """ - pass + sum_list = sum(nums) + average = statistics.average() def word_frequency_count(words: list[str]): """ @@ -241,7 +310,15 @@ def word_frequency_count(words: list[str]): Returns: dict: A dictionary with words as keys and their frequencies as values. """ - pass + lis = {} + for word in words: + if word in lis: + lis[word] += 1 + else: + lis[word] = 1 + print(lis) + +# print(word_frequency_count(["hello", "hello", "hey","sho"])) def filter_even_numbers(nums: list[int]): """ @@ -253,9 +330,13 @@ def filter_even_numbers(nums: list[int]): Returns: list: A list containing only the even integers from the input list. """ - pass + even = [] + for num in nums: + if num % 2 == 0: + even.append(num) + print(even) +#print(filter_even_numbers([2,5,7,9,8,10,12,24])) -"""Learning Outcome: Simple Algorithms(Problem Solving)""" def find_median(nums: list[int]): """ @@ -270,8 +351,9 @@ def find_median(nums: list[int]): Raises: ValueError: If the list is empty. """ - pass - + median = statistics.median(nums) + print(median) +# print(find_median([1,2,3,5])) def reverse_string(input: str): """ Reverse the given string. @@ -282,7 +364,8 @@ def reverse_string(input: str): Returns: str: The reversed string. """ - pass + return input[::-1] +# print(reverse_string("hello")) def largest_number(nums: list[int]): """ @@ -294,7 +377,11 @@ def largest_number(nums: list[int]): Returns: int or None: The largest number in the list, or None if the list is empty. """ - pass + largest = max(nums) + if not nums: + return None#stuck here + print(largest) +#print(largest_number([1,20,19])) def is_prime(n: int): """ @@ -306,8 +393,16 @@ def is_prime(n: int): Returns: bool: True if the number is prime, False otherwise. """ - pass - + if n < 2: + return False + if n == 2: + return True + for i in range(3, int(math.sqrt(n)) + 1, 2): + if n % i == 0: + return False + return True + +# print(is_prime(71)) def count_character_occurrences(word_sentence: str, char_count: str): """ Count the occurrences of a character in a given sentence. @@ -319,4 +414,9 @@ def count_character_occurrences(word_sentence: str, char_count: str): Returns: int: The number of occurrences of the character in the sentence. """ - pass \ No newline at end of file + count = 0 + for word in word_sentence.lower(): + if word == char_count: + count += 1 + return print(count) +# print(count_character_occurrences("My name is Ashly", "a")) \ No newline at end of file