Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding solutions #48

Merged
merged 23 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions solutions/count_vowels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def count_vowels(s):
"""
Counts the number of vowels in the given string.

Parameters:
s (str): The string in which to count vowels.

Returns:
int: The number of vowels in the string.

Example:
>>> count_vowels("hello")
2
>>> count_vowels("Nelsona")
3
"""
vowels = "aeiouAEIOU" # Define vowels (both lowercase and uppercase)
return sum(
1 for char in s if char in vowels
) # Count vowels using a generator expression
30 changes: 30 additions & 0 deletions solutions/fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
def fizz_buzz(n):
"""
Generates a list of numbers from 1 to n with the following rules:
- For multiples of 3, "Fizz" is added instead of the number.
- For multiples of 5, "Buzz" is added instead of the number.
- For multiples of both 3 and 5, "FizzBuzz" is added instead of the number.

Parameters:
n (int): The upper limit of the range (inclusive) to generate.

Returns:
list: A list containing the numbers and/or string representations according to the rules.

Example:
>>> fizz_buzz(15)
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz']
"""
results = [] # Initialize an empty list to store the results
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
results.append(
"FizzBuzz"
) # Append "FizzBuzz" for multiples of both 3 and 5
elif i % 3 == 0:
results.append("Fizz") # Append "Fizz" for multiples of 3
elif i % 5 == 0:
results.append("Buzz") # Append "Buzz" for multiples of 5
else:
results.append(i) # Append the number itself if not a multiple of 3 or 5
return results # Return the list of results
15 changes: 15 additions & 0 deletions solutions/reverse_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def reverse_string(s):
"""
Reverses the given string.

Parameters:
s (str): The string to be reversed.

Returns:
str: The reversed string.

Example:
>>> reverse_string("hello")
'olleh'
"""
return s[::-1] # Return the reversed string using slicing
55 changes: 55 additions & 0 deletions solutions/tests/test_count_vowels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest

from solutions.count_vowels import count_vowels # Import the count_vowels function


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

This class contains unit tests for the count_vowels function to ensure
that it accurately counts the number of vowels in various input strings.
"""

def test_count_vowels_normal(self):
"""Test counting vowels in a normal string.

This test checks that the function correctly counts the vowels in a
typical string with mixed characters.
"""
self.assertEqual(count_vowels("Nagham"), 2)

def test_count_vowels_empty(self):
"""Test counting vowels in an empty string.

This test verifies that the function returns 0 when the input string
is empty.
"""
self.assertEqual(count_vowels(""), 0)

def test_count_vowels_no_vowels(self):
"""Test counting vowels in a string with no vowels.

This test checks that the function returns 0 when there are no vowels
in the input string.
"""
self.assertEqual(count_vowels("bcdfghjklmnpqrstvwxyz"), 0)

def test_count_vowels_case_insensitive(self):
"""Test counting vowels in a case-insensitive manner.

This test verifies that the function counts both uppercase and lowercase
vowels correctly.
"""
self.assertEqual(count_vowels("AbEcIdOfUg"), 5)

def test_count_vowels_with_spaces(self):
"""Test counting vowels in a string with spaces.

This test checks that the function correctly counts vowels in a string
that contains spaces and punctuation.
"""
self.assertEqual(count_vowels("Hello World!"), 3)


if __name__ == "__main__":
unittest.main()
77 changes: 77 additions & 0 deletions solutions/tests/test_fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import unittest

from solutions.fizz_buzz import fizz_buzz


class TestFizzBuzz(unittest.TestCase):
"""Test cases for the fizzbuzz function.

This class contains unit tests for the fizzbuzz function to ensure
that it produces the expected output for various input values.
"""

def test_fizzbuzz_15(self):
"""Test FizzBuzz for n = 15.

This test checks that the output for n=15 matches the expected
list which includes Fizz, Buzz, and FizzBuzz at the correct
positions.
"""
expected = [
1,
2,
"Fizz",
4,
"Buzz",
"Fizz",
7,
8,
"Fizz",
"Buzz",
11,
"Fizz",
13,
14,
"FizzBuzz",
]
self.assertEqual(fizz_buzz(15), expected)

def test_fizzbuzz_3(self):
"""Test FizzBuzz for n = 3.

This test checks that for n=3, the output should return
a list containing the numbers 1, 2, and 'Fizz'.
"""
expected = [1, 2, "Fizz"]
self.assertEqual(fizz_buzz(3), expected)

def test_fizzbuzz_5(self):
"""Test FizzBuzz for n = 5.

This test verifies that for n=5, the output list includes
numbers 1, 2, 'Fizz', 4, and 'Buzz'.
"""
expected = [1, 2, "Fizz", 4, "Buzz"]
self.assertEqual(fizz_buzz(5), expected)

def test_fizzbuzz_1(self):
"""Test FizzBuzz for n = 1.

This test checks that for n=1, the output should simply be
a list containing the number 1.
"""
expected = [1]
self.assertEqual(fizz_buzz(1), expected)

def test_fizzbuzz_0(self):
"""Test FizzBuzz for n = 0.

This test checks that for n=0, the output should be an
empty list since there are no numbers to include.
"""
expected = []
self.assertEqual(fizz_buzz(0), expected)


if __name__ == "__main__":
unittest.main()
56 changes: 56 additions & 0 deletions solutions/tests/test_reverse_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
from solutions.reverse_string import (
reverse_string,
) # Import the reverse_string function


class TestReverseString(unittest.TestCase):
"""Test cases for the reverse_string function.

This class contains unit tests for the reverse_string function to ensure
that it correctly reverses strings under various scenarios.
"""

def test_reverse_normal(self):
"""Test reversing a normal string.

This test checks that the function correctly reverses a typical string
with multiple characters.
"""
self.assertEqual(reverse_string("hello"), "olleh")

def test_reverse_empty(self):
"""Test reversing an empty string.

This test verifies that the function returns an empty string when
the input is empty.
"""
self.assertEqual(reverse_string(""), "")

def test_reverse_single_character(self):
"""Test reversing a single character string.

This test checks that the function returns the same character when
the input is a single character string.
"""
self.assertEqual(reverse_string("a"), "a")

def test_reverse_palindrome(self):
"""Test reversing a palindrome.

This test verifies that the function returns the same string when
the input is a palindrome.
"""
self.assertEqual(reverse_string("madam"), "madam")

def test_reverse_spaces(self):
"""Test reversing a string with spaces.

This test checks that the function correctly reverses a string that
contains spaces, ensuring that the spaces are also reversed.
"""
self.assertEqual(reverse_string("hello world"), "dlrow olleh")


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