From b7a5f0c91498cb857830764228ea7d6e71916936 Mon Sep 17 00:00:00 2001 From: Raz Ebrat Date: Wed, 8 Apr 2026 20:49:09 -0400 Subject: [PATCH] Complete assignment1 --- assignment1/assignment1.py | 168 +++++++++++++++++++++++++++++- assignment1/tempCodeRunnerFile.py | 1 + 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 assignment1/tempCodeRunnerFile.py diff --git a/assignment1/assignment1.py b/assignment1/assignment1.py index fafa187..cd8140c 100644 --- a/assignment1/assignment1.py +++ b/assignment1/assignment1.py @@ -1 +1,167 @@ -# Write your code here. \ No newline at end of file +# Write your code here. + +# task 1 +def hello(): + return "Hello!" + +# task 2 + + +def greet(name): + return f"Hello, {name}!" + +# task 3 + + +def calc(a, b, operation="multiply"): + try: + match operation: + case "add": + return a + b + case "subtract": + return a - b + case "multiply": + return a * b + case "divide": + return a/b + case "modulo": + return a % b + case "int_divide": + return a // b + case "power": + return a ** b + except ZeroDivisionError: + return "You can't divide by 0!" + except TypeError: + return "You can't multiply those values!" + +# task 4 + + +def data_type_conversion(value, requested_type): + try: + if requested_type == "float": + return float(value) + elif requested_type == "str": + return str(value) + elif requested_type == "int": + return int(value) + except (ValueError, TypeError): + return f"You can't convert {value} into a {requested_type}." + +# task 5 + + +# Task 5 +def grade(*args): + try: + average = sum(args) / len(args) + + if average >= 90: + return "A" + elif average >= 80: + return "B" + elif average >= 70: + return "C" + elif average >= 60: + return "D" + else: + return "F" + except (TypeError, ZeroDivisionError): + return "Invalid data was provided." + +# task 6 + + +def repeat(text, count): + result = "" + for _ in range(count): + result += text + return result + + +print(repeat("hi", 3)) + +# task 7 + + +def student_scores(option, **kwargs): + if option == "best": + best_student = "" + highest_score = -1 + + for key, value in kwargs.items(): + if value > highest_score: + highest_score = value + best_student = key + + return best_student + elif option == "mean": + total = 0 + + for value in kwargs.values(): + total += value + + return total / len(kwargs) + + +print(student_scores("mean", Alice=90, Bob=85, Carol=95)) + + +# task 8 + +def titleize(text): + little_words = ["a", "on", "an", "the", "of", "and", "is", "in"] + words = text.split() + + for index, word in enumerate(words): + lower_word = word.lower() + + if index == 0 or index == len(words) - 1: + words[index] = word.capitalize() + elif word in little_words: + words[index] = word.lower() + else: + words[index] = word.capitalize() + return " ".join(words) + + +# task 9 +def hangman(secret, guess): + result = "" + + for letter in secret: + if letter in guess: + result += letter + else: + result += "_" + return result + + +print(hangman("alphabet", "ab")) + + +# Task 10 +def pig_latin(text): + vowels = "aeiou" + words = text.split() + result = [] + + for word in words: + if word[0] in vowels: + result.append(word + "ay") + else: + index = 0 + + while index < len(word) and word[index] not in vowels: + if word[index:index + 2] == "qu": + index += 2 + break + index += 1 + + result.append(word[index:] + word[:index] + "ay") + + return " ".join(result) + + +print(pig_latin("apple")) diff --git a/assignment1/tempCodeRunnerFile.py b/assignment1/tempCodeRunnerFile.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/assignment1/tempCodeRunnerFile.py @@ -0,0 +1 @@ +