From b7a5f0c91498cb857830764228ea7d6e71916936 Mon Sep 17 00:00:00 2001 From: Raz Ebrat Date: Wed, 8 Apr 2026 20:49:09 -0400 Subject: [PATCH 1/2] 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 @@ + From f471046ca733cba86ecbe1073e8e8247c472bed7 Mon Sep 17 00:00:00 2001 From: Raz Ebrat Date: Mon, 20 Apr 2026 19:40:01 -0400 Subject: [PATCH 2/2] Complete lesson 2 assignment --- assignment2/assignment2.py | 226 +++++++++++++++++++++++++++++++++++ assignment2/custom_module.py | 7 ++ assignment2/diary.py | 36 ++++++ assignment2/minutes.csv | 32 ++--- 4 files changed, 285 insertions(+), 16 deletions(-) create mode 100644 assignment2/assignment2.py create mode 100644 assignment2/custom_module.py create mode 100644 assignment2/diary.py diff --git a/assignment2/assignment2.py b/assignment2/assignment2.py new file mode 100644 index 0000000..ae84fee --- /dev/null +++ b/assignment2/assignment2.py @@ -0,0 +1,226 @@ +# Task 2: Read a CSV File +import csv +import traceback +import os +import custom_module +from datetime import datetime + + +def read_employees(): + employees_dict = {} + rows = [] + + try: + with open("../csv/employees.csv", "r", newline="") as file: + reader = csv.reader(file) + + for index, row in enumerate(reader): + if index == 0: + employees_dict["fields"] = row + else: + rows.append(row) + + employees_dict["rows"] = rows + return employees_dict + + except (Exception, KeyboardInterrupt) as e: + trace_back = traceback.extract_tb(e.__traceback__) + stack_trace = [] + + for trace in trace_back: + stack_trace.append( + f"File : {trace[0]} , Line : {trace[1]}, Func.Name : {trace[2]}, Message : {trace[3]}" + ) + + print("An exception occurred.") + print(f"Exception type: {type(e).__name__}") + + message = str(e) + if message: + print(f"Exception message: {message}") + + print(f"Stack trace: {stack_trace}") + raise + + +employees = read_employees() +print(employees) + + +# Task 3: Find the Column Index +def column_index(column_name): + return employees["fields"].index(column_name) + + +employee_id_column = column_index("employee_id") +print(employee_id_column) + + +# Task 4: Find the Employee First Name +def first_name(row_number): + first_name_column = column_index("first_name") + return employees["rows"][row_number][first_name_column] + + +# Task 5: Find the Employee +def employee_find(employee_id): + def employee_match(row): + return int(row[employee_id_column]) == employee_id + + matches = list(filter(employee_match, employees["rows"])) + return matches + + +# Task 6: Find the Employee with a Lambda +def employee_find_2(employee_id): + matches = list( + filter(lambda row: int(row[employee_id_column]) + == employee_id, employees["rows"]) + ) + return matches + + +# Task 7: Sort the Rows by last_name Using a Lambda +def sort_by_last_name(): + last_name_column = column_index("last_name") + employees["rows"].sort(key=lambda row: row[last_name_column]) + return employees["rows"] + + +sort_by_last_name() +print(employees) + + +# Task 8: Create a dict for an Employee +def employee_dict(row): + employee = {} + + for index, field in enumerate(employees["fields"]): + if field != "employee_id": + employee[field] = row[index] + + return employee + + +print(employee_dict(employees["rows"][0])) + + +# Task 9: A dict of dicts, for All Employees +def all_employees_dict(): + all_employees = {} + + for row in employees["rows"]: + employee_id = row[employee_id_column] + all_employees[employee_id] = employee_dict(row) + + return all_employees + + +print(all_employees_dict()) + + +# Task 10: Use the os Module +def get_this_value(): + return os.getenv("THISVALUE") + + +# Task 11: Creating Your Own Module +def set_that_secret(new_secret): + custom_module.set_secret(new_secret) + + +set_that_secret("open sesame") +print(custom_module.secret) + + +# Task 12: Read minutes1.csv and minutes2.csv +def read_csv_to_dict(path): + data = {} + rows = [] + + try: + with open(path, "r", newline="") as file: + reader = csv.reader(file) + + for index, row in enumerate(reader): + if index == 0: + data["fields"] = row + else: + rows.append(tuple(row)) + + data["rows"] = rows + return data + + except (Exception, KeyboardInterrupt) as e: + trace_back = traceback.extract_tb(e.__traceback__) + stack_trace = [] + + for trace in trace_back: + stack_trace.append( + f"File : {trace[0]} , Line : {trace[1]}, Func.Name : {trace[2]}, Message : {trace[3]}" + ) + + print("An exception occurred.") + print(f"Exception type: {type(e).__name__}") + + message = str(e) + if message: + print(f"Exception message: {message}") + + print(f"Stack trace: {stack_trace}") + raise + + +def read_minutes(): + minutes1 = read_csv_to_dict("../csv/minutes1.csv") + minutes2 = read_csv_to_dict("../csv/minutes2.csv") + return minutes1, minutes2 + + +minutes1, minutes2 = read_minutes() +print(minutes1) +print(minutes2) + + +# Task 13: Create minutes_set +def create_minutes_set(): + set1 = set(minutes1["rows"]) + set2 = set(minutes2["rows"]) + return set1.union(set2) + + +minutes_set = create_minutes_set() +print(minutes_set) + + +# Task 14: Convert to datetime +def create_minutes_list(): + minutes = list(minutes_set) + minutes = list( + map(lambda x: (x[0], datetime.strptime(x[1], "%B %d, %Y")), minutes) + ) + return minutes + + +minutes_list = create_minutes_list() +print(minutes_list) + + +# Task 15: Write Out Sorted List +def write_sorted_list(): + minutes_list.sort(key=lambda x: x[1]) + + converted_list = list( + map(lambda x: (x[0], datetime.strftime( + x[1], "%B %d, %Y")), minutes_list) + ) + + with open("./minutes.csv", "w", newline="") as file: + writer = csv.writer(file) + writer.writerow(minutes1["fields"]) + writer.writerows(converted_list) + + return converted_list + + +print(write_sorted_list()) diff --git a/assignment2/custom_module.py b/assignment2/custom_module.py new file mode 100644 index 0000000..2ced9c1 --- /dev/null +++ b/assignment2/custom_module.py @@ -0,0 +1,7 @@ + +secret = "shazam!" + + +def set_secret(new_secret): + global secret + secret = new_secret diff --git a/assignment2/diary.py b/assignment2/diary.py new file mode 100644 index 0000000..b1c97e5 --- /dev/null +++ b/assignment2/diary.py @@ -0,0 +1,36 @@ +# Task 1: Diary +import traceback + +try: + with open("diary.txt", "a") as file: + first_prompt = True + + while True: + if first_prompt: + line = input("What happened today? ") + first_prompt = False + else: + line = input("What else? ") + + file.write(line + "\n") + + if line == "done for now": + break + +except (Exception, KeyboardInterrupt) as e: + trace_back = traceback.extract_tb(e.__traceback__) + stack_trace = [] + + for trace in trace_back: + stack_trace.append( + f"File : {trace[0]} , Line : {trace[1]}, Func.Name : {trace[2]}, Message : {trace[3]}" + ) + + print("An exception occurred.") + print(f"Exception type: {type(e).__name__}") + + message = str(e) + if message: + print(f"Exception message: {message}") + + print(f"Stack trace: {stack_trace}") diff --git a/assignment2/minutes.csv b/assignment2/minutes.csv index 0acabb7..84a339b 100644 --- a/assignment2/minutes.csv +++ b/assignment2/minutes.csv @@ -1,46 +1,46 @@ Name,Date Jason Tucker,"September 20, 1980" -Austin Hester,"March 8, 1981" -Daniel Jackson,"October 2, 1981" +Austin Hester,"March 08, 1981" +Daniel Jackson,"October 02, 1981" Mrs. Samantha Johnson,"December 17, 1981" -Joseph Harris,"March 3, 1982" +Joseph Harris,"March 03, 1982" Mrs. Samantha Johnson,"March 12, 1982" Gina Maldonado,"December 16, 1982" Mrs. Samantha Johnson,"December 23, 1982" Yesenia Smith,"August 10, 1983" -Lori Martin,"February 5, 1984" +Lori Martin,"February 05, 1984" Jonathan Parrish,"June 12, 1984" Mrs. Samantha Johnson,"July 20, 1984" -Amanda Brown,"August 8, 1984" +Amanda Brown,"August 08, 1984" Sarah Murray,"October 30, 1984" Mrs. Samantha Johnson,"November 28, 1984" -Austin Hester,"June 4, 1985" +Austin Hester,"June 04, 1985" Jonathan Parrish,"March 18, 1986" -Yesenia Smith,"May 6, 1986" +Yesenia Smith,"May 06, 1986" Daniel Jackson,"December 13, 1986" Gina Maldonado,"February 13, 1987" Kimberly Stewart,"December 12, 1987" Mrs. Samantha Johnson,"July 10, 1988" Sarah Murray,"August 16, 1988" Aaron Kaufman,"October 24, 1988" -Tony Henderson,"November 7, 1988" +Tony Henderson,"November 07, 1988" Sarah Murray,"November 19, 1988" Austin Hester,"January 18, 1989" -Joseph Harris,"March 1, 1989" -Gina Maldonado,"April 7, 1989" +Joseph Harris,"March 01, 1989" +Gina Maldonado,"April 07, 1989" Aaron Kaufman,"November 14, 1989" -Daniel Jackson,"April 8, 1990" -Aaron Kaufman,"May 1, 1990" +Daniel Jackson,"April 08, 1990" +Aaron Kaufman,"May 01, 1990" Aaron Kaufman,"July 21, 1990" -Tony Henderson,"October 4, 1990" +Tony Henderson,"October 04, 1990" Yesenia Smith,"November 23, 1990" -Joseph Harris,"April 3, 1991" +Joseph Harris,"April 03, 1991" Jason Tucker,"April 30, 1991" Matthew Russell,"May 31, 1991" -Lori Martin,"July 8, 1991" +Lori Martin,"July 08, 1991" Mrs. Samantha Johnson,"July 23, 1991" Tony Henderson,"November 15, 1991" -Gina Maldonado,"February 9, 1992" +Gina Maldonado,"February 09, 1992" Sarah Murray,"June 27, 1992" Gina Maldonado,"October 31, 1992" Austin Hester,"December 10, 1992"