forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:MIT-Emerging-Talent/ET6-foundations…
…-group-22
- Loading branch information
Showing
22 changed files
with
390 additions
and
513 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
# Solutions | ||
|
||
This folder contains implementations for the challenges. | ||
|
||
## Challenges | ||
|
||
1. **Merge Dictionaries**: | ||
- A utility to merge two dictionaries with conflict resolution. | ||
- See `merge_dictionaries.py` for the implementation. | ||
|
||
2. **Anagram Finder**: | ||
- A function to check if two strings are anagrams. | ||
- See `anagram_finder.py` for the implementation. | ||
|
||
## Usage | ||
|
||
To use any solution, simply import the | ||
required function and pass the appropriate arguments. | ||
|
||
### How to Run | ||
|
||
1. Clone the repository. | ||
2. Navigate to the folder containing the solution. | ||
3. Run the desired script: | ||
|
||
```bash | ||
python <script_name>.py | ||
``` | ||
|
||
### Example | ||
|
||
```python | ||
# Example for Anagram Finder | ||
from anagram_finder import are_anagrams | ||
result = are_anagrams("listen", "silent") | ||
print(result) # Output: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This function asks the user to enter a number and checks if the number | ||
is even or odd. It then returns the result as a string. | ||
Created on 05 01 2025 | ||
@author: Eman Alfalouji. | ||
""" | ||
|
||
|
||
def check_number_type(user_input: str) -> str: | ||
""" | ||
The function asks the user to enter a number and determines if it is type (even or odd.) | ||
Parameters: | ||
user_input (str): str | ||
A string that represents an integer. | ||
Floats or non-integer formats are not allowed. | ||
Raises: | ||
ValueError: If the input is empty. | ||
ValueError: If the input is not a valid integer. | ||
Returns: | ||
results will be a text whether "The number is even", "The number is odd" | ||
or raises an appropriate error. | ||
Examples : | ||
>>> check_number_type("20") | ||
"The number is even" | ||
>>> check_number_type("11") | ||
"The number is odd" | ||
>>> check_number_type("-11") | ||
"The number is odd" | ||
>>> check_number_type("") | ||
Traceback (most recent call last): | ||
... | ||
ValueError:"Input cannot be empty. Enter a valid number." | ||
>>> check_number_type("Eman") | ||
Traceback (most recent call last): | ||
... | ||
ValueError:"Please enter a valid number" | ||
""" | ||
user_input = user_input.strip() | ||
# Check if it is empty | ||
if not user_input: | ||
raise ValueError("Input cannot be empty. Enter a valid number.") | ||
# check if it is a number | ||
if not user_input.lstrip("-").isdigit(): | ||
raise ValueError("Please enter a valid number") | ||
number = int(user_input) | ||
# Check if the number is even or odd | ||
if number % 2 == 0: | ||
return "The number is even" | ||
else: | ||
return "The number is odd" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This module provides functionality to extract unique values from a CSV file. | ||
Author: Derek Karungani | ||
Date Created: 2024-12-11 | ||
""" | ||
|
||
import csv | ||
|
||
|
||
def get_unique_values(file_path: str, column_name: str) -> list: | ||
""" | ||
Get unique values from a specified column in a CSV file. | ||
Parameters: | ||
file_path (str): Path to the CSV file. It should be a valid path to an existing CSV file. | ||
The function assumes the file is encoded in UTF-8. Both relative and absolute | ||
paths are acceptable. The file must exist, or a FileNotFoundError will be raised. | ||
column_name (str): Name of the column to extract unique values. It should correspond to a | ||
header in the CSV file. | ||
Returns: | ||
list: Unique values in the specified column. The order of the unique values is not guaranteed to be | ||
the same as their order of appearance in the CSV file. | ||
Raises: | ||
FileNotFoundError: If the file path is invalid or the file does not exist. | ||
KeyError: If the column name doesn't exist in the CSV. | ||
ValueError: If the `file_path` is an empty string. | ||
Examples: | ||
>>> with open("test.csv", "w", newline="") as f: | ||
... writer = csv.writer(f) | ||
... writer.writerow(["Name", "Age", "City"]) | ||
... writer.writerow(["Alice", "25", "New York"]) | ||
... writer.writerow(["Bob", "30", "London"]) | ||
... writer.writerow(["Alice", "25", "Paris"]) | ||
>>> get_unique_values("test.csv", "Name") | ||
['Bob', 'Alice'] | ||
>>> get_unique_values("test.csv", "City") | ||
['Paris', 'New York', 'London'] | ||
>>> get_unique_values("test.csv", "Age") | ||
['30', '25'] | ||
>>> get_unique_values("test.csv", "Country") | ||
Traceback (most recent call last): | ||
... | ||
KeyError: "Column 'Country' does not exist in the CSV." | ||
>>> get_unique_values("nonexistent_file.csv", "Name") | ||
Traceback (most recent call last): | ||
... FileNotFoundError: File not found: nonexistent_file.csv | ||
>>> get_unique_values("", "Name") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: The file_path cannot be an empty string. | ||
""" | ||
# Check for empty file_path | ||
if not file_path: | ||
raise ValueError("The file_path cannot be an empty string.") | ||
# Try to open the file and read data | ||
try: | ||
with open(file_path, "r", encoding="utf-8") as file: | ||
reader = csv.DictReader(file) | ||
# Check if the column name exists in the CSV header | ||
if column_name not in reader.fieldnames: | ||
raise KeyError(f"Column '{column_name}' does not exist in the CSV.") | ||
|
||
unique_values = set() | ||
# Iterate through the rows and add unique values to the set | ||
for row in reader: | ||
unique_values.add(row[column_name]) | ||
|
||
return list(unique_values) | ||
# Handle file not found error | ||
except FileNotFoundError as e: | ||
raise FileNotFoundError(f"File not found: {file_path}") from e |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.