Skip to content

Commit

Permalink
ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Muna-S committed Jan 12, 2025
2 parents 8c562e1 + f8df7df commit 2c8d4e0
Show file tree
Hide file tree
Showing 14 changed files with 650 additions and 23 deletions.
149 changes: 149 additions & 0 deletions solutions/README.md
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)
47 changes: 47 additions & 0 deletions solutions/area_of_circle.py
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)
42 changes: 42 additions & 0 deletions solutions/binary_to_hex.py
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()
44 changes: 44 additions & 0 deletions solutions/hex_to_binary.py
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")
20 changes: 10 additions & 10 deletions solutions/IsPrime.py → solutions/is_prime.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
A module for finding if an integer is prime.
Module for finding if an integer is prime.
Module contents:
- IsPrime: finds if an integer is prime.
- is_prime: finds if an integer is prime.
Created on XX XX XX
@author: Mohammed Elfadil
"""


def IsPrime(a: int) -> str:
def is_prime(a: int) -> str:
"""Checks if an integer is prime.
Parameter:
a: int
Return -> str: whether a is prime or not
Raises:
AssertionError: if the argument is not an integer
>>> IsPrime(0)
>>> is_prime(0)
not prime
>>> IsPrime(1)
>>> is_prime(1)
not prime
>>> IsPrime(2)
>>> is_prime(2)
prime
>>> IsPrime(4)
>>> is_prime(4)
not prime
>>> IsPrime(7)
>>> is_prime(7)
prime
>>> IsPrime(2.5)
>>> is_prime(2.5)
invalid input
>>> IsPrime(-1)
>>> is_prime(-1)
not prime
"""
if not isinstance(a, int):
Expand Down
36 changes: 36 additions & 0 deletions solutions/leap_year.py
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)
Loading

0 comments on commit 2c8d4e0

Please sign in to comment.