From e345624d0e3af4a8a926a2c6032b633c9d400547 Mon Sep 17 00:00:00 2001 From: Omar SS Date: Wed, 12 Feb 2025 17:07:53 -0300 Subject: [PATCH 1/3] I added two functions to cs50 python module. One of the functions reads a csv file and handles some errors. The other function implements binary search, where you pass an array and the target you are looking for. --- src/cs50/cs50.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/cs50/cs50.py b/src/cs50/cs50.py index f331a88..d0f16d5 100644 --- a/src/cs50/cs50.py +++ b/src/cs50/cs50.py @@ -5,6 +5,7 @@ import os import re import sys +import csv from os.path import abspath, join from termcolor import colored @@ -157,3 +158,53 @@ def get_string(prompt): return input(prompt) except EOFError: return None + +# I implemented a beginner friendly function to read .csv files, this functions sort as a training wheel for File I/O +# I am currently taking this course and I think its a good idea for future years + +def read_csv(file: str): + try: + with open(file, 'r') as f: + reader = csv.reader(f) + rows = [] + for row in reader: + rows.append(row) + return rows + except FileNotFoundError: + print("Error: The file {file} was not found") + return [] + except ValueError as val_err: + print(f"Error: {val_err}") + +# The binary_search function will be passed two arguments and it will use binary search to find the target +# This function can help cs50 students implement binary search in their projects until they figure out how to code it themselves +# If the passed array isnt sorted ascendantly, the function will not function and return -2 + + +def binary_search(array, target): + sorted_array = False + for i in range(1, len(array)): + if array[i] < array[i-1]: + print(f"Error: Array:{array} not sorted") + return -2 + else: + sorted_array = True + + if sorted_array: + low = 0 + high = len(array) - 1 + + while low <= high: + mid = (low + high) // 2 + g = array[mid] + + if g == target: + return mid + elif g > target: + high = mid - 1 + elif g < target: + low = mid + 1 + + return -1 + +# Changes made by OmarSSpy \ No newline at end of file From 4a9fe4ee3a479d5dae4f056234eb4f18a97aa4a2 Mon Sep 17 00:00:00 2001 From: Omar SS Date: Thu, 13 Feb 2025 02:12:30 -0300 Subject: [PATCH 2/3] Corrected syntax in read_csv function --- src/cs50/cs50.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cs50/cs50.py b/src/cs50/cs50.py index d0f16d5..355d90d 100644 --- a/src/cs50/cs50.py +++ b/src/cs50/cs50.py @@ -171,7 +171,7 @@ def read_csv(file: str): rows.append(row) return rows except FileNotFoundError: - print("Error: The file {file} was not found") + print(f"Error: The file {file} was not found") return [] except ValueError as val_err: print(f"Error: {val_err}") From 235d1de100b2bf313c59d8188dc86240fd70afc6 Mon Sep 17 00:00:00 2001 From: Omar SS Date: Fri, 14 Feb 2025 20:01:06 -0300 Subject: [PATCH 3/3] Added new write_csv function --- src/cs50/cs50.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/cs50/cs50.py b/src/cs50/cs50.py index 355d90d..7f9bc6d 100644 --- a/src/cs50/cs50.py +++ b/src/cs50/cs50.py @@ -207,4 +207,16 @@ def binary_search(array, target): return -1 + +def write_csv(file: str, data: list) -> bool: + try: + with open(file, 'w', newline='', encoding='utf-8') as f: + csv.writer(f).writerows(data) + return True + except IOError as io_err: + print(f"Error: {io_err}") + return False + except Exception as ex: + print(f"An error occured while writing to {file}, error: {ex}") + return False # Changes made by OmarSSpy \ No newline at end of file