Skip to content

Commit

Permalink
Merge pull request #136 from MIT-Emerging-Talent/edit_first_second_so…
Browse files Browse the repository at this point in the history
…lution

Edit first second solution
  • Loading branch information
yuri-spizhovyi-mit authored Jan 12, 2025
2 parents 9229398 + bd14b58 commit 9896379
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
12 changes: 6 additions & 6 deletions solutions/calculate_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Calculates the Median of a List of Numbers
This file contains a function to calculate the median of a list of numbers.
Created on 01/03/2025
Author: Khadija Al Ramlawi
Author: Khadija Ramlawi
"""


Expand All @@ -14,9 +14,9 @@ def calculate_median(numbers: list) -> float:
Returns:
float: The median value of the list.
Raises:
AssertionError: If the input is not a list.
AssertionError: If the input contains non-numeric values.
AssertionError: If the input list is empty.
AssertionError: If the input is not a list
AssertionError: If the input contains non-numeric values
AssertionError: If the input list is empty
Examples:
>>> calculate_median([1, 3, 2])
2.0
Expand All @@ -35,8 +35,8 @@ def calculate_median(numbers: list) -> float:
n = len(sorted_numbers)

if n % 2 == 1:
return sorted_numbers[n // 2]
return float(sorted_numbers[n // 2])
else:
mid1 = sorted_numbers[n // 2 - 1]
mid2 = sorted_numbers[n // 2]
return (mid1 + mid2) / 2
return float((mid1 + mid2) / 2)
8 changes: 4 additions & 4 deletions solutions/capitalize_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
Created on 12/28/2024
Author : Khadija Al Ramlawi
Author : Khadija Ramlawi
"""


def capitalize_character(text: str, char_to_capitalize: str) -> str:
"""
Capitalizes a specified character in a given string
Capitalizes a specified character in a given string.
Parameters:
text (str): The string in which the character will be capitalized.
char_to_capitalize (str): The character to capitalize. Must be a single character.
text (str): The string in which the character will be capitalized
char_to_capitalize (str): The character to capitalize. Must be a single character
Returns:
str: The string with the specified character capitalized.
Expand Down
16 changes: 8 additions & 8 deletions solutions/tests/test_calculate_median.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ class TestCalculateMedian(unittest.TestCase):
"""

def test_odd_length_list(self):
# Test case for an odd-length list.
"""Test case for an odd-length list."""
self.assertEqual(calculate_median([1, 3, 2]), 2.0)

def test_even_length_list(self):
# Test case for an even-length list.
"""Test case for an even-length list."""
self.assertEqual(calculate_median([4, 1, 7, 8]), 5.5)

def test_single_element(self):
# Test case for a single-element list.
"""Test case for a single-element list."""
self.assertEqual(calculate_median([5]), 5.0)

def test_empty_list(self):
# Test case for an empty list.
"""Test case for an empty list."""
with self.assertRaises(AssertionError):
calculate_median([])

def test_non_numeric_elements(self):
# Test case for a list with non-numeric elements.
"""Test case for a list with non-numeric elements."""
with self.assertRaises(AssertionError):
calculate_median([1, "a", 3])

def test_non_list_input(self):
# Test case for a non-list input.
"""Test case for a non-list input."""
with self.assertRaises(AssertionError):
calculate_median("not a list")

def test_sorted_input(self):
# Test case for an already sorted list.
"""Test case for an already sorted list."""
self.assertEqual(calculate_median([1, 2, 3, 4]), 2.5)

def test_unsorted_input(self):
# Test case for an unsorted list.
"""Test case for an unsorted list."""
self.assertEqual(calculate_median([4, 1, 3, 2]), 2.5)


Expand Down
20 changes: 10 additions & 10 deletions solutions/tests/test_capitalize_character.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,44 @@ class TestCapitalizeCharacter(unittest.TestCase):
"""

def test_single_occurrence(self):
# Test capitalizing a single occurrence of a character.
"""Test capitalizing a single occurrence of a character."""
self.assertEqual(capitalize_character("hello", "h"), "Hello")

def test_multiple_occurrences(self):
# Test capitalizing multiple occurrences of a character.
"""Test capitalizing multiple occurrences of a character."""
self.assertEqual(capitalize_character("hello", "l"), "heLLo")

def test_no_occurrence(self):
# Test with a character that does not exist in the word.
"""Test with a character that does not exist in the word."""
self.assertEqual(capitalize_character("hello", "z"), "hello")

def test_empty_word(self):
# Test with an empty string as the input word.
"""Test with an empty string as the input word."""
self.assertEqual(capitalize_character("", "a"), "")

def test_single_character_word(self):
# Test with a single-character word that matches the input character.
"""Test with a single-character word that matches the input character."""
self.assertEqual(capitalize_character("a", "a"), "A")

def test_special_characters(self):
# Test capitalizing a special character in the string.
"""Test capitalizing a special character in the string."""
self.assertEqual(capitalize_character("he!lo!", "!"), "he!lo!")

def test_numerical_characters(self):
# Test capitalizing numerical characters in the string.
"""Test capitalizing numerical characters in the string."""
self.assertEqual(capitalize_character("h3ll0", "l"), "h3LL0")

def test_invalid_input_word(self):
# Test handling invalid input where the word is not a string.
"""Test handling invalid input where the word is not a string."""
with self.assertRaises(AssertionError):
capitalize_character(123, "a")

def test_invalid_input_char(self):
# Test handling invalid input where the character is not a string.
"""Test handling invalid input where the character is not a string."""
with self.assertRaises(AssertionError):
capitalize_character("hello", 123)

def test_multiple_characters_as_input(self):
# Test handling input where the character parameter has multiple characters.
"""Test handling input where the character parameter has multiple characters."""
with self.assertRaises(AssertionError):
capitalize_character("hello", "ll")

0 comments on commit 9896379

Please sign in to comment.