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' into find_prime_numbers_up_to_n
- Loading branch information
Showing
25 changed files
with
1,247 additions
and
7 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
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 |
---|---|---|
|
@@ -48,3 +48,4 @@ | |
|
||
- Programming Proficiency | ||
- Proficiency in Github & Python | ||
|
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,11 @@ | ||
import math | ||
|
||
def floor_sqrt(numbers): | ||
result = [] | ||
for num in numbers: | ||
if num < 0: | ||
result.append(None) | ||
else: | ||
sqrt_value = math.sqrt(num) | ||
result.append(math.floor(sqrt_value)) | ||
return result |
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 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,41 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This module defines the function `count_consonants` to count the number of consonants | ||
in a given string. The function handles different character cases and ignores non-alphabetical characters. | ||
Author: Norbert Ndayisenga | ||
Date: 07 01 2024 | ||
""" | ||
|
||
|
||
def count_consonants(s: str) -> int: | ||
""" | ||
Counts the number of consonants in a given string, ignoring vowels, numbers, and symbols. | ||
Parameters: | ||
s (str): The input string to evaluate. | ||
Returns: | ||
int: The number of consonants in the input string. | ||
Raises: | ||
TypeError: If the input is not a string. | ||
Examples: | ||
>>> count_consonants("Hello, World!") | ||
7 | ||
>>> count_consonants("123@#$") | ||
0 | ||
>>> count_consonants("bcdfghjklmnpqrstvwxyz") | ||
21 | ||
""" | ||
# Ensure the input is a string | ||
if not isinstance(s, str): | ||
raise TypeError("Input must be a string") | ||
|
||
# Define consonants using a set for faster membership testing | ||
consonants = set("bcdfghjklmnpqrstvwxyz") | ||
|
||
# Count consonants in the string | ||
return sum(1 for char in s.lower() if char in consonants) |
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,38 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
This module defines the function `count_evens` to count the number of even integers | ||
in a given list. The function ignores non-integer elements and handles invalid input gracefully. | ||
Author: Norbert Ndayisenga | ||
Date: 07 01 2024 | ||
""" | ||
|
||
|
||
def count_evens(numbers: list) -> int: | ||
""" | ||
Counts the number of even integers in a given list, ignoring non-integer elements. | ||
Parameters: | ||
numbers (list): The input list to evaluate. | ||
Returns: | ||
int: The number of even integers in the input list. | ||
Raises: | ||
TypeError: If the input is not a list. | ||
Examples: | ||
>>> count_evens([1, 2, 3, 4]) | ||
2 | ||
>>> count_evens([1, 'a', None, 2.5]) | ||
0 | ||
>>> count_evens([2, 4, 6]) | ||
3 | ||
""" | ||
# Ensure the input is a list | ||
if not isinstance(numbers, list): | ||
raise TypeError("Input must be a list") | ||
|
||
# Count even integers in the list | ||
return sum(1 for num in numbers if isinstance(num, int) and num % 2 == 0) |
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,75 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
r""" | ||
This module defines the functions `is_prime` and `count_primes` for checking primality | ||
and counting prime numbers in a list. | ||
@uthor: Zeinab Shadabshoar | ||
Date: 09 01 2025 | ||
""" | ||
|
||
|
||
def is_prime(n: int) -> bool: | ||
r""" | ||
Checks if a given number is prime. | ||
Parameters: | ||
n (int): The number to check. | ||
Returns: | ||
bool: True if the number is prime, False otherwise. | ||
Raises: | ||
TypeError: If the input is not an integer. | ||
ValueError: If the input is a negative integer. | ||
Examples: | ||
>>> is_prime(2) | ||
True | ||
>>> is_prime(4) | ||
False | ||
""" | ||
if not isinstance(n, int): | ||
raise TypeError("Input must be an integer") | ||
if n < 0: | ||
raise ValueError("Input must be a non-negative integer") | ||
if n <= 1: | ||
return False | ||
for i in range(2, int(n**0.5) + 1): | ||
if n % i == 0: | ||
return False | ||
return True | ||
|
||
|
||
def count_primes(numbers: list) -> int: | ||
r""" | ||
Counts the number of prime numbers in a list. | ||
Parameters: | ||
numbers (list): The list of numbers to check. | ||
Returns: | ||
int: The count of prime numbers in the list. | ||
Raises: | ||
TypeError: If the input is not a list or contains non-integer elements. | ||
Examples: | ||
>>> count_primes([2, 3, 4, 5]) | ||
3 | ||
""" | ||
if not isinstance(numbers, list): | ||
raise TypeError("Input must be a list") | ||
prime_count = 0 | ||
for num in numbers: | ||
if not isinstance(num, int): | ||
raise TypeError("All elements in the list must be integers") | ||
if is_prime(num): | ||
prime_count += 1 | ||
return prime_count | ||
|
||
|
||
# Example usage | ||
numbers = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] | ||
print("Number of prime numbers:", count_primes(numbers)) |
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,43 @@ | ||
#!/usr/bin/env python3 | ||
# -- coding: utf-8 -- | ||
""" | ||
A module for counting the number of words in a string. | ||
Module contents: | ||
- word_count_function: takes a string as an argument and returns the number of words. | ||
Created on 06/01/2025 | ||
@author: Mohamad Ziadah | ||
""" | ||
|
||
|
||
def word_count_function(s: str) -> int: | ||
""" | ||
Counts the number of words in a string. | ||
Parameters: | ||
s (str): The input string to count words from. | ||
Returns: | ||
int: The number of words in the string. | ||
Raises: | ||
AssertionError: If the input is not a string. | ||
>>> word_count_function("The quick brown fox jumps over the lazy dog") | ||
9 | ||
>>> word_count_function("") | ||
0 | ||
>>> word_count_function(" leading and trailing spaces ") | ||
4 | ||
>>> word_count_function("word1\tword2\nword3 word4") | ||
4 | ||
""" | ||
assert isinstance(s, str), "Input must be a string" | ||
|
||
# Filter out non-alphanumeric characters and split the string by whitespace | ||
words = [word for word in s.split() if word.isalnum()] | ||
|
||
# Return the count of words | ||
return len(words) |
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,99 @@ | ||
""" | ||
This module provides functionality to convert a fraction to | ||
a percentage and display it in a user-friendly format. | ||
Functions: | ||
main(): Entry point of the program that handles user input and output. | ||
convert(fraction): Converts a fraction string (e.g., '1/2') into a percentage. | ||
gauge(percentage): Converts a percentage into a user-friendly string representation. | ||
Author: Alnajjar | ||
Date: 10 01 2025 | ||
""" | ||
|
||
|
||
def main(): | ||
""" | ||
Continuously prompts the user for a fraction input, converts it | ||
to a percentage, and displays the result. | ||
""" | ||
while True: | ||
try: | ||
fraction = input("Fraction: ") | ||
print(gauge(convert(fraction))) | ||
break | ||
except AssertionError as e: | ||
print(e) | ||
|
||
|
||
def convert(fraction): | ||
""" | ||
Converts a fraction string into a percentage. | ||
Args: | ||
fraction (str): A string representing a fraction in the format 'X/Y'. | ||
Returns: | ||
int: The percentage equivalent of the fraction, rounded to the nearest integer. | ||
Raises: | ||
AssertionError: If the numerator is greater than the denominator, | ||
or if the input is invalid. | ||
Examples: | ||
>>> convert("1/2") | ||
50 | ||
>>> convert("3/4") | ||
75 | ||
>>> convert("2/5") | ||
40 | ||
""" | ||
x, y = fraction.split("/") | ||
|
||
# Assert that both parts of the fraction are digits | ||
assert x.isdigit() and y.isdigit(), ( | ||
"Both numerator and denominator must be integers." | ||
) | ||
|
||
x, y = int(x), int(y) | ||
|
||
assert y != 0, "Denominator cannot be zero." | ||
|
||
assert x >= 0 and y > 0, "Numerator and denominator must be non-negative." | ||
|
||
assert x <= y, "Numerator cannot be greater than denominator." | ||
|
||
return round((x / y) * 100) | ||
|
||
|
||
def gauge(percentage): | ||
""" | ||
Converts a percentage into a user-friendly string representation. | ||
Args: | ||
percentage (int): An integer representing a percentage (0-100). | ||
Returns: | ||
str: 'F' if the percentage is greater than 99, 'E' if less than 1, | ||
otherwise the percentage followed by '%'. | ||
Raises: | ||
AssertionError: If the percentage is not within the range 0-100. | ||
Examples: | ||
>>> gauge(50) | ||
'50%' | ||
>>> gauge(100) | ||
'F' | ||
>>> gauge(0) | ||
'E' | ||
""" | ||
# Assert that percentage is within valid range | ||
assert 0 <= percentage <= 100, "Percentage must be between 0 and 100." | ||
|
||
if percentage > 99: | ||
return "F" | ||
elif percentage < 1: | ||
return "E" | ||
else: | ||
return f"{percentage}%" |
Oops, something went wrong.