Skip to content

Commit

Permalink
Merge 'origin' into learning_goals
Browse files Browse the repository at this point in the history
  • Loading branch information
jola-ds committed Jan 11, 2025
2 parents c21f332 + 2bdba2c commit 0638b93
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
12 changes: 6 additions & 6 deletions solutions/calculate_max_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def calculate_max_profit(prices: list) -> int:
"""

assert isinstance(prices, list), "Input should be a list."
assert all(
isinstance(price, (int, float)) for price in prices
), "Prices should be integers or floats."
assert all(
price >= 0 for price in prices
), "Prices should be non-negative integers or floats."
assert all(isinstance(price, (int, float)) for price in prices), (
"Prices should be integers or floats."
)
assert all(price >= 0 for price in prices), (
"Prices should be non-negative integers or floats."
)

maximum_profit = 0

Expand Down
42 changes: 42 additions & 0 deletions solutions/count_vowels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
A module for counting vowels in a string.
Module contents:
- count_vowels: Counts the number of vowels in a given string.
It supports both uppercase and lowercase vowels.
Created on: 2025-07-06
@author: Fatima
"""


def count_vowels(text: str) -> int:
"""Returns the number of vowels (both uppercase and lowercase) in a string.
Parameters:
text: str, the input string to check
Returns:
int: Number of vowels in the text.
Raises:
AssertionError: if the argument is not a string
>>> count_vowels("hello")
2
>>> count_vowels("APPLE")
2
>>> count_vowels("why")
0
"""

assert isinstance(text, str), "input must be a string"

vowels = "aeiouAEIOU"
count = 0

for letter in text:
if letter in vowels:
count += 1

return count
6 changes: 3 additions & 3 deletions solutions/remove_duplicates_from_sorted_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def remove_duplicates_from_sorted_list(head: Optional[ListNode]) -> Optional[Lis
# result is now a list with values: [1, 2]
"""
# Defensive Assertion
assert head is None or isinstance(
head, ListNode
), "Input must be a ListNode or None."
assert head is None or isinstance(head, ListNode), (
"Input must be a ListNode or None."
)

current_node = head
while current_node and current_node.next:
Expand Down
81 changes: 81 additions & 0 deletions solutions/tests/test_count_vowels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Unit tests for validating the count_vowels function.
Test Categories:
- Standard Cases: Regular words with varying vowel counts.
- Edge Cases: Empty strings, repeated vowels, and mixed casing.
- Special Cases: Strings with numbers, special characters, and whitespace.
- Defensive Cases: Ensuring proper handling of non-string inputs.
Created on: 2025-07-06
@author: Fatima
"""

import unittest
from solutions.count_vowels import count_vowels


class TestCountVowels(unittest.TestCase):
"""Test cases for count_vowels function."""

# Standard Cases
def test_typical_word(self):
"""It should count vowels in a regular word with both vowels and consonants."""
self.assertEqual(count_vowels("hello"), 2)

def test_mixed_case_vowels(self):
"""It should count both uppercase and lowercase vowels in a string."""
self.assertEqual(count_vowels("hellOWorld"), 3)

def test_uppercase_vowels(self):
"""It should count uppercase vowels in the string."""
self.assertEqual(count_vowels("SMALL"), 1)

def test_lowercase_vowels(self):
"""It should count lowercase vowels in the string."""
self.assertEqual(count_vowels("small"), 1)

def test_repeated_vowels(self):
"""It should count all repeated vowels in a string."""
self.assertEqual(count_vowels("aaeeiioouuAAEEIIOOUU"), 20)

# Edge Cases
def test_empty_string(self):
"""It should return 0 for an empty string."""
self.assertEqual(count_vowels(""), 0)

def test_only_whitespace(self):
"""It should return 0 for strings with only whitespace.."""
self.assertEqual(count_vowels(" "), 0)

def test_no_vowels(self):
"""It should return 0 for a string with no vowels."""
self.assertEqual(count_vowels("fly"), 0)

def test_long_string(self):
"""It should correctly count vowels in a long mixed-case string."""
text = ("aAeEiIoOuU" * 500) + "bcdfghjklmnpqrstvwxyz" * 100
self.assertEqual(count_vowels(text), 5000)

# Special Cases
def test_alphanumeric_string(self):
"""It should count vowels in alphanumeric strings, ignoring digits."""
self.assertEqual(count_vowels("fatima123Malik"), 5)

def test_special_characters(self):
"""It should ignore special characters and only count vowels."""
self.assertEqual(count_vowels("!@#$%^&*()aeiOu"), 5)

def test_string_with_whitespace(self):
"""It should count vowels even if the string contains spaces."""
self.assertEqual(count_vowels("hello world"), 3)

# Defensive Cases
def test_invalid_input(self):
"""Test that raises assertion error for non-string input."""
with self.assertRaises(AssertionError):
count_vowels(None)


if __name__ == "__main__":
unittest.main()

0 comments on commit 0638b93

Please sign in to comment.