forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
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 binary_decimal_conversion
- Loading branch information
Showing
14 changed files
with
650 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,150 @@ | ||
# Solutions | ||
|
||
## This file contains a brief on the challenges completed by Group 18 Team | ||
|
||
### factorial | ||
|
||
- **description:** A module for Computing the factorial of | ||
a non-negative integer n. | ||
|
||
- **written by:** Saad (sashour82) | ||
|
||
- **reviewed by:** Luyandochitindi (Luyando-Chitindi) | ||
|
||
### is_palinrome | ||
|
||
- **description:** A module for checking if a given string is | ||
a palindrome (reads the same backward as forward). | ||
|
||
- **written by:** Saad (sashour82) | ||
|
||
- **reviewed by:** Falaq (FalaqMajeed) | ||
|
||
### is_even | ||
|
||
- **description:** A module that contains a function that checks if a number is even. | ||
|
||
- **written by:** Luyandochitindi (Luyando-Chitindi) | ||
|
||
- **reviewed by:** Mohammed (Moealfadil) | ||
|
||
### is_positive | ||
|
||
- **description:** A module that contains a function that checks if a number is positive. | ||
|
||
- **written by:** Luyandochitindi (Luyando-Chitindi) | ||
|
||
- **reviewed by:** Muna (Muna-S) | ||
|
||
### mean | ||
|
||
- **description:** Calculates the mean of numbers in a list | ||
|
||
- **written by:** Mohammed (Moealfadil) | ||
|
||
- **reviewed by:** Falaq (FalaqMajeed) | ||
|
||
### is_prime | ||
|
||
- **description:** Checks if a number is prime or not | ||
|
||
- **written by:** Mohammed (Moealfadil) | ||
|
||
- **reviewed by:** Luyandochitindi (Luyando-Chitindi) | ||
|
||
### cumulative_sum | ||
|
||
- **description:** Calculates the cumulative sum of a list by iterating | ||
through its elements and maintaining a running total | ||
|
||
- **written by:** Falaq (FalaqMajeed) | ||
|
||
- **reviewed by:** Mohammed (Moealfadil) | ||
|
||
### list_to_string | ||
|
||
- **description:** Converts a list into a single string | ||
|
||
- **written by:** Falaq (FalaqMajeed) | ||
|
||
- **reviewed by:** Raghad (raghad598) | ||
|
||
### max_in | ||
|
||
- **description:** Find the maximum number in a list | ||
|
||
- **written by:** TagwaHashim (TagwaHashim) | ||
|
||
- **reviewed by:** Raghad (raghad598) | ||
|
||
### sum_of | ||
|
||
- **description:** Calculate the summation of two numbers | ||
|
||
- **written by:** TagwaHashim (TagwaHashim) | ||
|
||
- **reviewed by:** Muna (Muna-S) | ||
|
||
### binary_to_decimal | ||
|
||
- **description:** Give the decimal number for a binary | ||
|
||
- **written by:** Muna (Muna-S) | ||
|
||
- **reviewed by:** TagwaHashim (TagwaHashim), Falaq (FalaqMajeed), Saad (sashour82) | ||
|
||
### leap_year | ||
|
||
- **description:** Checks whether a year is a leap year | ||
|
||
- **written by:** Muna (Muna-S) | ||
|
||
- **reviewed by:** TagwaHashim (TagwaHashim) | ||
|
||
### remove_duplicates | ||
|
||
- **description:** Take a list and return it without any repeated values | ||
|
||
- **written by:** Heba (HebaShaheen) | ||
|
||
- **reviewed by:** Saad (sashour82) | ||
|
||
### binary_decimal_conversion | ||
|
||
- **description:** Gives the binary number for a decimal and vice versa | ||
|
||
- **written by:** Heba (HebaShaheen) | ||
|
||
- **reviewed by:** Saad (sashour82) | ||
|
||
### count_words | ||
|
||
- **description:** Counts the number of words in a string | ||
|
||
- **written by:** Raghad (raghad598) | ||
|
||
- **reviewed by:** Saad (sashour82) | ||
|
||
### area_of_circle | ||
|
||
- **description:** Calculates the area of a circle given its radius | ||
|
||
- **written by:** Raghad (raghad598) | ||
|
||
- **reviewed by:** Heba (HebaShaheen), Mohammed (Moealfadil) | ||
|
||
### hex_to_binary | ||
|
||
- **description:** Converts a hexadecimal string to its binary equivalent | ||
|
||
- **written by:** Marc (MarcDarazi99) | ||
|
||
- **reviewed by:** Mohammed (Moealfadil) | ||
|
||
### binary_to_hex | ||
|
||
- **description:** Converts a binary string to its hexadecimal equivalent | ||
|
||
- **written by:** Marc (MarcDarazi99) | ||
|
||
- **reviewed by:** Mohammed (Moealfadil), Heba (HebaShaheen), Falaq (FalaqMajeed) |
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,47 @@ | ||
""" | ||
A module for calculating the area of a circle. | ||
Module contents: | ||
- area_of_circle: calculates the area of a circle given its radius. | ||
Created on 01 04 2025 | ||
@author: Raghad | ||
""" | ||
|
||
import math | ||
|
||
|
||
# Define a function to calculate the area of a circle. | ||
def area_of_circle(radius: float) -> float: | ||
"""Calculate the area of a circle given its radius. | ||
Parameters: | ||
radius: float, the radius of the circle | ||
Returns -> float: area of the circle | ||
Raises: | ||
ValueError: if the radius is negative | ||
Examples: | ||
>>> area_of_circle(5) | ||
78.53981633974483 | ||
>>> area_of_circle(0) | ||
0.0 | ||
>>> area_of_circle(3.5) | ||
38.48451000647496 | ||
""" | ||
# Raise an error if the radius is negative. | ||
if radius < 0: | ||
raise ValueError("Radius cannot be negative") | ||
|
||
# Calculate and return the area using the formula: area = π * r^2 | ||
return math.pi * radius**2 | ||
|
||
|
||
# Entry point for script execution | ||
if __name__ == "__main__": | ||
# Example usage: Calculate the area of a circle with a radius of 5. | ||
radius = 5 | ||
area = area_of_circle(radius) | ||
print("Area of the circle:", area) |
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,42 @@ | ||
""" | ||
Module: Binary to Hexadecimal Converter | ||
This module provides a utility function to convert binary strings to their | ||
corresponding hexadecimal representations. It validates the input to ensure | ||
that it contains only binary digits (0s and 1s) and performs the conversion | ||
accurately. | ||
@author: Marc Darazi | ||
""" | ||
|
||
|
||
def binary_to_hex(binary_str): | ||
""" | ||
Convert a binary string to its hexadecimal representation. | ||
Parameters: | ||
binary_str (str): A string of 0s and 1s representing a binary number. | ||
Returns: | ||
str: The hexadecimal representation of the binary number. | ||
Raises: | ||
ValueError: If the input is not a valid binary string. | ||
Example: | ||
>>> binary_to_hex("1101") | ||
'D' | ||
>>> binary_to_hex("11110000") | ||
'F0' | ||
>>> binary_to_hex("101010101010") | ||
'AAA' | ||
""" | ||
# Validate input | ||
if not all(char in "01" for char in binary_str): | ||
raise ValueError("Input must be a binary string containing only 0s and 1s.") | ||
|
||
# Convert binary string to integer | ||
decimal_value = int(binary_str, 2) | ||
|
||
# Convert integer to hexadecimal string and return | ||
return hex(decimal_value)[2:].upper() |
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,44 @@ | ||
""" | ||
This module provides a utility function to convert a hexadecimal string | ||
to its binary representation. | ||
Functions: | ||
- hex_to_binary: Converts a hexadecimal string to its binary equivalent. | ||
Author: Marc Darazi | ||
""" | ||
|
||
|
||
def hex_to_binary(hex_string): | ||
""" | ||
Convert a hexadecimal string to its binary representation. | ||
Parameters: | ||
hex_string (str): A string representing a hexadecimal number. | ||
It can include a leading '0x' prefix or not. | ||
Returns: | ||
str: A binary string representation of the given hexadecimal number, | ||
with leading zeros preserved. | ||
Raises: | ||
ValueError: If the input string is not a valid hexadecimal number. | ||
Example: | ||
>>> hex_to_binary("1A") | ||
'11010' | ||
>>> hex_to_binary("0xFF") | ||
'11111111' | ||
>>> hex_to_binary("0x0") | ||
'0' | ||
""" | ||
# Remove '0x' prefix if present | ||
if hex_string.startswith("0x"): | ||
hex_string = hex_string[2:] | ||
|
||
try: | ||
# Convert to integer, then to binary | ||
binary_string = bin(int(hex_string, 16))[2:] # Remove '0b' prefix | ||
return binary_string | ||
except ValueError: | ||
raise ValueError("Invalid hexadecimal string") |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
The module checks whether a given year is a leap year. | ||
create by Muna Sattouf on January 9, 2025 | ||
Completed on January 10, 2024 | ||
A year is a leap year if it is divisible by 4, | ||
and if it is divisible by 100, it must also be divisible by 400. | ||
""" | ||
|
||
|
||
def leap_year(year: int) -> bool: | ||
""" | ||
Checks whether the given year is a leap year. | ||
Argument: | ||
year, a positive integer | ||
Returns: | ||
boolean: True if the year is a leap year, false otherwise. | ||
Examples: | ||
>>> leap_year(2024) | ||
True | ||
>>> leap_year(1900) | ||
False | ||
>>> leap_year(2021) | ||
False | ||
""" | ||
if not isinstance(year, int): | ||
raise ValueError("Year must be an integer") | ||
if year < 0: | ||
raise ValueError("Year must be positive integer") | ||
|
||
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) |
Oops, something went wrong.