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

Count the vowels in the sentence. #59

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Empty file.
14 changes: 14 additions & 0 deletions Desktop/Majd Readme/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Python File",
"type": "debugpy",
"request": "launch",
"program": "${file}"
}
]
}
1 change: 1 addition & 0 deletions Desktop/Majd Readme/ET6-foundations-group-16
Submodule ET6-foundations-group-16 added at 103e93
15 changes: 15 additions & 0 deletions solutions/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
1 change: 1 addition & 0 deletions solutions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ while corresponding test files are maintained in the `tests` folder.
|--------------------------|--------------------------------------------------------------|-------------------|
| `calc_avg.py`| Calculates the average of a list of numbers | Clement |
| `spiral_traverse.py` | Traverses a 2D matrix in spiral order | Fahed |
| `euler_totient.py` | Computes Euler's totient function (ϕ(n))| Fahed |

---

Expand Down
1 change: 0 additions & 1 deletion solutions/__init__.py

This file was deleted.

16 changes: 16 additions & 0 deletions solutions/count_vowel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Function that count the vowel in sentence
def count_vowels_recursively(string):

if not string:

return 0
else:
if string[0].lower() in 'aeiou':
return 1 + count_vowels_recursively(string[1:])
else:
return count_vowels_recursively(string[1:])

input_string = "Palestinian culture is beautiful."
a = count_vowels_recursively(input_string)
print(f"Number of vowels in '{input_string}' is: {a}")

57 changes: 57 additions & 0 deletions solutions/euler_totient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
euler_totient.py

This module provides a function to compute Euler's totient function (ϕ(n)),
which counts the number of integers between 1 and n inclusive, that are coprime to n.

Author: Fahed Daibes
Date: Mon Jan 6 2025
Group: ET6-foundations-group-16

Functions:
- euler_totient(n): Computes Euler's totient function (ϕ(n)) for a given integer n.
"""


def euler_totient(n):
"""
Euler's totient function or Phi function.

Parameters:
- n (int): A positive integer for which the Euler's totient function is to be computed.

Returns:
- int: The number of integers between 1 and n inclusive that are coprime to n.

Raises:
- ValueError: If the input is not a positive integer.

Examples:
>>> euler_totient(5)
4
>>> euler_totient(1)
1
>>> euler_totient(10)
4
>>> euler_totient(0)
Traceback (most recent call last):
...
AssertionError: Input must be a positive integer.
>>> euler_totient(-5)
Traceback (most recent call last):
...
AssertionError: Input must be a positive integer.
"""
# Defensive assertions
assert isinstance(n, int), "Input must be an integer."
assert n > 0, "Input must be a positive integer."

result = n
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
while n % i == 0:
n //= i
result -= result // i
if n > 1:
result -= result // n
return result
17 changes: 17 additions & 0 deletions solutions/intersection_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Write Python 3 code in this online

def common_elements(list1, list2):
# Convert both lists to sets to remove duplicates and find intersection
set1 = set(list1)
set2 = set(list2)
# Find the common elements
common = set1.intersection(set2)
# Convert the result back to a list
return list(common)

# Example usage
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]
result = common_elements(list1, list2)
print(result) # Output: [4, 5, 6]

72 changes: 72 additions & 0 deletions solutions/tests/test_count_vowel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unittest
# Function that count the vowel in sentence
def count_vowels_recursively(string):

if not string:

return 0
else:
if string[0].lower() in 'aeiou':
return 1 + count_vowels_recursively(string[1:])
else:
return count_vowels_recursively(string[1:])

input_string = "Palestinian culture is beautiful."
a = count_vowels_recursively(input_string)
print(f"Number of vowels in '{input_string}' is: {a}")

# Function that count the vowel in sentence
def count_vowels_recursively(string):

if not string:

return 0
else:
if string[0].lower() in 'aeiou':
return 1 + count_vowels_recursively(string[1:])
else:
return count_vowels_recursively(string[1:])

input_string = "Palestinian culture is beautiful."
a = count_vowels_recursively(input_string)
print(f"Number of vowels in '{input_string}' is: {a}")


def count_vowels_recursively(string):
if not string:
return 0
else:
if string[0].lower() in 'aeiou':
return 1 + count_vowels_recursively(string[1:])
else:
return count_vowels_recursively(string[1:])
#Here is the test

class TestCountVowelsRecursively(unittest.TestCase):

def test_empty_string(self):
self.assertEqual(count_vowels_recursively(""), 0)

def test_no_vowels(self):
self.assertEqual(count_vowels_recursively("bcdfghjklmnpqrstvwxyz"), 0)

def test_only_vowels(self):
self.assertEqual(count_vowels_recursively("aeiouAEIOU"), 10)

def test_mixed_string(self):
self.assertEqual(count_vowels_recursively("hello world"), 3)

def test_complex_sentence(self):
self.assertEqual(count_vowels_recursively("The quick brown fox jumps over the lazy dog"), 11)

def test_special_characters(self):
self.assertEqual(count_vowels_recursively("!@#$%^&*()_+ 1234567890-="), 0)

def test_single_vowel(self):
self.assertEqual(count_vowels_recursively("a"), 1)

def test_single_non_vowel(self):
self.assertEqual(count_vowels_recursively("b"), 0)

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

This module contains unit tests for the `euler_totient` function, which computes Euler's totient
function (ϕ(n)). The function calculates the number of integers between 1 and n that are coprime
with n.

Author: Fahed Daibes
Date: Mon Jan 6 2025
Group: ET6-foundations-group-16

Tests:
- Valid positive integer inputs for Euler's totient function.
- Edge cases like 1 and prime numbers.
- Invalid inputs such as negative numbers, zero, and non-integer values.
"""

import unittest
from solutions.euler_totient import euler_totient


class TestEulerTotient(unittest.TestCase):
"""
This test class contains unit tests for the `euler_totient` function.

It tests the function's ability to handle various valid inputs, including edge cases like 1,
and invalid inputs such as non-integer values, negative numbers, and zero.
"""

def test_valid_input_9(self):
"""Test case for input 9."""
self.assertEqual(euler_totient(9), 6)

def test_valid_input_12(self):
"""Test case for input 12."""
self.assertEqual(euler_totient(12), 4)

def test_valid_input_5(self):
"""Test case for input 5."""
self.assertEqual(euler_totient(5), 4)

def test_valid_input_1(self):
"""Test case for input 1."""
self.assertEqual(euler_totient(1), 1)

def test_invalid_input_negative(self):
"""Test case for negative input."""
with self.assertRaises(AssertionError):
euler_totient(-5)

def test_invalid_input_zero(self):
"""Test case for zero input."""
with self.assertRaises(AssertionError):
euler_totient(0)

def test_invalid_input_non_integer_string(self):
"""Test case for non-integer string input."""
with self.assertRaises(AssertionError):
euler_totient("string")

def test_invalid_input_non_integer_float(self):
"""Test case for non-integer float input."""
with self.assertRaises(AssertionError):
euler_totient(3.5)

def test_large_input(self):
"""Test case for a large positive integer."""
self.assertEqual(euler_totient(1000), 400)

def test_prime_number_7(self):
"""Test case for prime number 7."""
self.assertEqual(euler_totient(7), 6)

def test_prime_number_13(self):
"""Test case for prime number 13."""
self.assertEqual(euler_totient(13), 12)


if __name__ == "__main__":
unittest.main()
52 changes: 52 additions & 0 deletions solutions/tests/test_intersection_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Write Python 3 code in this online

def common_elements(list1, list2):
# Convert both lists to sets to remove duplicates and find intersection
set1 = set(list1)
set2 = set(list2)
# Find the common elements
common = set1.intersection(set2)
# Convert the result back to a list
return list(common)

# Example usage
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]
result = common_elements(list1, list2)
print(result) # Output: [4, 5, 6]

def test_common_elements():
# Test case 1: Common elements exist
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
assert sorted(common_elements(list1, list2)) == [4, 5], "Test case 1 failed"

# Test case 2: No common elements
list1 = [1, 2, 3]
list2 = [4, 5, 6]
assert common_elements(list1, list2) == [], "Test case 2 failed"

# Test case 3: Identical lists
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5]
assert sorted(common_elements(list1, list2)) == [1, 2, 3, 4, 5], "Test case 3 failed"

# Test case 4: One list is empty
list1 = []
list2 = [1, 2, 3]
assert common_elements(list1, list2) == [], "Test case 4 failed"

# Test case 5: Both lists are empty
list1 = []
list2 = []
assert common_elements(list1, list2) == [], "Test case 5 failed"

# Test case 6: Lists with duplicate elements
list1 = [1, 2, 2, 3, 4]
list2 = [3, 3, 4, 5, 6]
assert sorted(common_elements(list1, list2)) == [3, 4], "Test case 6 failed"

print("All test cases passed!")

# Run the tests
test_common_elements()
Loading