Skip to content

Commit 3061036

Browse files
committed
Create count_occurrences task
1 parent 38aa2bb commit 3061036

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Python boilerplate for GitHub tasks
1+
# Count Occurrences
22

33
- Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before start
44
- Implement the task described [here](app/main.py)

app/main.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
# TODO: add initial code
2-
def hello_world():
3-
return "Hello, world!"
1+
def count_occurrences(phrase: str, letter: str) -> int:
2+
"""
3+
Implement count_occurrences function:
4+
5+
It takes a phrase and a letter and calculates the number of times
6+
the letter appears in the phrase. The function is case insensitive.
7+
8+
count_occurrences("letter", "t") == 2
9+
count_occurrences("abc", "a") == 1
10+
count_occurrences("abc", "d") == 0
11+
count_occurrences("ABC", "a") == 1
12+
13+
:param phrase: phrase to count in it
14+
:param letter: letter to find occurrences of it
15+
:return: count occurrences of letter in phrase
16+
"""
17+
# write your code here

tests/test_main.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
# TODO: add tests
2-
from app.main import hello_world
1+
import pytest
32

3+
from app.main import count_occurrences
44

5-
def test_hello_world():
6-
assert hello_world() == "Hello, world!"
5+
6+
@pytest.mark.parametrize(
7+
"phrase,letter,count",
8+
[
9+
("samsung", "a", 1),
10+
("samsung is gnusmas", "s", 5),
11+
("Samsung is gnusmas", "s", 5),
12+
("Abracadabra", "A", 5),
13+
("", "a", 0),
14+
("Samsung", "b", 0),
15+
]
16+
)
17+
def test_count_occurrences(phrase, letter, count):
18+
assert count_occurrences(phrase, letter) == count, (
19+
f"Function 'count_occurrences' should return {count}, "
20+
f"when 'phrase'='{phrase}' and 'letter'='{letter}'"
21+
)

0 commit comments

Comments
 (0)