Skip to content

Commit 4a5c257

Browse files
committed
Add count_group
1 parent 558a218 commit 4a5c257

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ The [object serializer](/object-serializer) takes an object as a argument and se
3131

3232
[socket](/socket) has a [`client`][client] and a [`server`][server]. The [`client`][client] sends to the [`server`][server] a "`Hello, World`", which in return sends back to the [`client`][client] the same message.
3333

34+
## Count group
35+
36+
The [count group](./count_group/count_group.py) counts how many times the letters repeats consecutively.
37+
3438
## Learning source
3539

3640
* [A byte of python(PDF)](https://edisciplinas.usp.br/pluginfile.php/3252353/mod_resource/content/1/b_Swaroop_Byte_of_python.pdf)

count_group/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Count group
2+
3+
Takes a string and returns a `list` of tuples where the first element is the letter and second is how many of the letters repeats consecutively
4+
5+
### Worked concepts
6+
* For loops
7+
* enumerate
8+
* for-else
9+
* Type hints
10+
* assert statement

count_group/count_group.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
def count_group(string: str) -> list:
3+
counter = 0
4+
group = []
5+
temp = string[0]
6+
for index, s in enumerate(string):
7+
if temp == s:
8+
counter += 1
9+
else:
10+
group.append((temp, counter))
11+
counter = 1
12+
temp = s
13+
else:
14+
group.append((temp, counter))
15+
16+
return group
17+
18+
def test_short_string():
19+
assert count_group('') == []
20+
assert count_group('a') == [('a', 1)]
21+
assert count_group('aaaabbbcca') == [('a', 4), ('b', 3), ('c', 2), ('a', 1)]

0 commit comments

Comments
 (0)