Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 3777afc

Browse files
authored
[Datasets] Package update (#114)
2 parents ab8dce3 + b8633ed commit 3777afc

File tree

13 files changed

+508
-372
lines changed

13 files changed

+508
-372
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
-Dsonar.projectKey=edu-python-course_problem-sets
4848
-Dsonar.organization=edu-python-course
4949
-Dsonar.python.coverage.reportPaths=coverage.xml -X
50-
-Dsonar.coverage.exclusions=**/scripts/**,**/__init__,**/__main__,**/tests/** -X
50+
-Dsonar.coverage.exclusions=**/scripts/**,**/__init__*,**/__main__*,**/tests/** -X
5151
env:
5252
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
5353
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

src/datasets/__init__.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,32 @@
1212
"""
1313

1414
__all__ = [
15-
"swap_dict",
16-
"get_low_student",
17-
"get_top_student",
18-
"get_both_top_low_students",
1915
"get_bricks_count",
20-
"get_position_frequency",
21-
"get_least_bricks_position",
2216
"get_least_bricks_count",
17+
"get_least_bricks_position",
18+
"get_position_frequency",
2319
"filter_by_values",
24-
"flatten",
20+
"flatten_list",
21+
"swap_dict",
22+
"swap_dict_loop",
23+
"get_avg_score",
24+
"get_both_top_low_students",
25+
"get_low_student",
26+
"get_top_student",
2527
]
2628

27-
from datasets.func import *
29+
from datasets.bricks import (
30+
get_bricks_count,
31+
get_least_bricks_count,
32+
get_least_bricks_position,
33+
get_position_frequency
34+
)
35+
from datasets.filters import filter_by_values
36+
from datasets.flatten import flatten_list
37+
from datasets.func import swap_dict, swap_dict_loop
38+
from datasets.students import (
39+
get_avg_score,
40+
get_both_top_low_students,
41+
get_low_student,
42+
get_top_student
43+
)

src/datasets/bricks.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""
2+
Brick wall challenge functions
3+
4+
"""
5+
6+
from typing import Dict, List
7+
8+
9+
def get_bricks_count(structure: List[List[int]]) -> int:
10+
"""
11+
Return number of bricks in the wall structure
12+
13+
:param structure: represents wall structure as sequences of integers
14+
:type structure: list
15+
16+
:return: total number of bricks in the entire wall structure
17+
:rtype: int
18+
19+
Usage:
20+
21+
>>> assert get_bricks_count([[1], [1], [1]]) == 3
22+
>>> assert get_bricks_count([[1, 1, 1], [1, 1, 1]]) == 6
23+
>>> assert get_bricks_count([[2, 1, 2], [1, 1, 1, 1, 1]]) == 8
24+
25+
"""
26+
27+
return sum(len(row) for row in structure)
28+
29+
30+
def get_position_frequency(structure: List[List[int]]) -> Dict[int, int]:
31+
"""
32+
Return a matrix of position-bricks pairs for a structure
33+
34+
:param structure: represents wall structure as sequences of integers
35+
:type structure: list
36+
37+
:return: the position - frequency matrix
38+
:rtype: dict
39+
40+
Usage:
41+
42+
>>> assert get_position_frequency([[1], [1], [1]]) == {}
43+
>>> assert get_position_frequency([[1, 2], [2, 1], [3]]) = {1: 1, 2: 1}
44+
>>> assert get_position_frequency([[1, 1, 1], [1, 1, 1]]) = {1: 2, 2: 2}
45+
46+
"""
47+
48+
structure_matrix = {}
49+
50+
for row in structure:
51+
row_length = 0
52+
for brick_length in row[:-1]:
53+
row_length += brick_length
54+
if row_length not in structure_matrix:
55+
structure_matrix[row_length] = 0
56+
structure_matrix[row_length] += 1
57+
58+
return structure_matrix
59+
60+
61+
def get_least_bricks_position(structure: List[List[int]]) -> int:
62+
"""
63+
Return a pointer to the weakest line in the structure
64+
65+
:param structure: represents wall structure as sequences of integers
66+
:type structure: list
67+
68+
:return: the distance from the left edge to the weakest line location
69+
:rtype: int
70+
71+
This function uses helper function ``get_structure_matrix`` to build
72+
the matrix of distances from the left edge of the "wall".
73+
74+
Usage:
75+
76+
>>> assert get_least_bricks_position([[1], [1], [1]]) == 0
77+
>>> assert get_least_bricks_position([[1, 1, 1], [1, 1, 1]]) == 1
78+
79+
"""
80+
81+
matrix = get_position_frequency(structure)
82+
if not matrix:
83+
return 0
84+
85+
return max(matrix, key=matrix.get) # type: ignore
86+
87+
88+
def get_least_bricks_count(structure: List[List[int]]) -> int:
89+
"""
90+
Return the least number of bricks in a vertical line
91+
92+
:param structure: represents wall structure as sequences of integers
93+
:type structure: list
94+
95+
:return: minimum number of bricks in a line
96+
:rtype: int
97+
98+
Usage:
99+
100+
>>> assert get_least_bricks_count([[1], [1], [1]]) == 3
101+
>>> assert get_least_bricks_count([[1, 2], [2, 1], [3], [1, 1, 1]]) == 2
102+
>>> assert get_least_bricks_count([[1, 1, 1], [1, 1, 1]]) == 0
103+
104+
"""
105+
106+
max_value: int = 0
107+
matrix = get_position_frequency(structure)
108+
for count in matrix.values():
109+
max_value = max(max_value, count)
110+
111+
return len(structure) - max_value

src/datasets/filters.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Dataset filtering functions
3+
4+
"""
5+
6+
from typing import Any, Dict, Hashable, List, Optional, Set
7+
8+
9+
def filter_by_values(origin: List[Dict[str, Hashable]],
10+
keys: Optional[List[str]] = None
11+
) -> List[Dict[str, Any]]:
12+
"""
13+
Return a filtered datasets by unique values in a given keys sets
14+
15+
:param origin: an original dataset with entries to filter
16+
:type origin: list
17+
:param keys: a collection of keys to use for filtering
18+
:type keys: list, optional
19+
20+
:return: a filtered dataset
21+
:rtype: list
22+
23+
The origin dataset is a list of dictionaries. All the dictionaries have
24+
the same set of keys of a string type. All dictionaries values are of
25+
a hashable type.
26+
27+
In case no data provided (``origin`` is an empty list) the function will
28+
return an empty list.
29+
30+
The keys parameter is used to set the dictionary keys to filter unique
31+
values. Keys list is considered to be validated before passing to this
32+
function, all values (if any) are valid. In case this parameter is
33+
omitted - all available keys should be used.
34+
35+
Usage:
36+
37+
>>> ds = [{"x": 1, "y": 2, "z": 3}, {"x": 0, "y": 2, "z": 3}]
38+
>>> assert filter_by_values(ds, ["x"]) == ds # the same as origin
39+
>>> assert filter_by_values(ds, ["x", "z"]) == ds # the same as origin
40+
>>> assert filter_by_values(ds, ["y"]) == [{"x": 1, "y": 2, "z": 3}]
41+
>>> assert filter_by_values(ds, ["y", "z"]) == [{"x": 1, "y": 2, "z": 3}]
42+
43+
"""
44+
45+
# check base cases
46+
if not origin:
47+
return []
48+
49+
# declare variables to store runtime data
50+
filtered_dataset: List[Dict[str, Hashable]] = []
51+
filtered_values: Set[int] = set()
52+
53+
keys = keys or origin[0].keys() # type: ignore
54+
for entry in origin:
55+
entry_values = hash(tuple(map(entry.get, keys))) # type: ignore
56+
if entry_values in filtered_values:
57+
continue
58+
59+
filtered_values.add(entry_values)
60+
filtered_dataset.append(entry)
61+
62+
return filtered_dataset

src/datasets/flatten.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Data flatten functions
3+
4+
"""
5+
6+
from typing import List, Union
7+
8+
NestedIntList = Union[int, List["NestedIntList"]]
9+
10+
11+
def flatten_list(origin: List[NestedIntList]) -> List[int]:
12+
"""
13+
Flattens a list of integers and nested lists of integers
14+
15+
:param origin: an original list
16+
17+
:return: a single-level list of integers
18+
19+
Usage:
20+
21+
>>> assert flatten_list([1, 2, 3]) == [1, 2, 3]
22+
>>> assert flatten_list(([[1], [2], [3]])) == [1, 2, 3]
23+
>>> assert flatten_list([1, [2, 3]]) == [1, 2, 3]
24+
>>> assert flatten_list([1, [[2], [3]]]) == [1, 2, 3]
25+
26+
"""
27+
28+
result: List[int] = []
29+
30+
for item in origin:
31+
if isinstance(item, list):
32+
result.extend(flatten_list(item))
33+
else:
34+
result.append(item)
35+
36+
return result

0 commit comments

Comments
 (0)