Skip to content

Commit 9d5ad2e

Browse files
committed
Simplify example files
1 parent 477013e commit 9d5ad2e

File tree

8 files changed

+0
-634
lines changed

8 files changed

+0
-634
lines changed
Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy as np
2-
import pandas as pd
3-
41
import random
52

63

@@ -35,104 +32,3 @@ def randomly_sample_and_filter_participants(
3532
age_filtered_participants = filter_participants_by_age(sampled_participants, min_age, max_age)
3633
height_filtered_participants = filter_participants_by_height(age_filtered_participants, min_height, max_height)
3734
return height_filtered_participants
38-
39-
40-
def remove_anomalies(data: list, maximum_value: float, minimum_value: float) -> list:
41-
"""Remove anomalies from a list of numbers"""
42-
43-
result = []
44-
45-
for value in data:
46-
if minimum_value <= value <= maximum_value:
47-
result.append(value)
48-
49-
return result
50-
51-
52-
def calculate_frequency(data: list) -> dict:
53-
"""Calculate the frequency of each element in a list"""
54-
55-
frequencies = {}
56-
57-
# Iterate over each value in the list
58-
for value in data:
59-
# If the value is already in the dictionary, increment the count
60-
if value in frequencies:
61-
frequencies[value] += 1
62-
# Otherwise, add the value to the dictionary with a count of 1
63-
else:
64-
frequencies[value] = 1
65-
66-
return frequencies
67-
68-
69-
def calculate_cumulative_sum(array: np.ndarray) -> np.ndarray:
70-
"""Calculate the cumulative sum of a numpy array"""
71-
72-
# don't use the built-in numpy function
73-
result = np.zeros(array.shape)
74-
result[0] = array[0]
75-
for i in range(1, len(array)):
76-
result[i] = result[i - 1] + array[i]
77-
78-
return result
79-
80-
81-
def calculate_player_total_scores(participants: dict):
82-
"""Calculate the total score of each player in a dictionary.
83-
84-
Example input:
85-
{
86-
"Alice": {
87-
"scores": np.array([1, 2, 3])
88-
},
89-
"Bob": {
90-
"scores": np.array([4, 5, 6])
91-
},
92-
"Charlie": {
93-
"scores": np.array([7, 8, 9])
94-
},
95-
}
96-
97-
Example output:
98-
{
99-
"Alice": {
100-
"scores": np.array([1, 2, 3]),
101-
"total_score": 6
102-
},
103-
"Bob": {
104-
"scores": np.array([4, 5, 6]),
105-
"total_score": 15
106-
},
107-
"Charlie": {
108-
"scores": np.array([7, 8, 9]),
109-
"total_score": 24
110-
},
111-
}
112-
"""
113-
114-
for player in participants:
115-
participants[player]["total_score"] = np.sum(participants[player]["scores"])
116-
117-
return participants
118-
119-
120-
def calculate_player_average_scores(df: pd.DataFrame) -> pd.DataFrame:
121-
"""Calculate the average score of each player in a pandas DataFrame.
122-
123-
Example input:
124-
| | player | score_1 | score_2 |
125-
|---|---------|---------|---------|
126-
| 0 | Alice | 1 | 2 |
127-
| 1 | Bob | 3 | 4 |
128-
129-
Example output:
130-
| | player | score_1 | score_2 | average_score |
131-
|---|---------|---------|---------|---------------|
132-
| 0 | Alice | 1 | 2 | 1.5 |
133-
| 1 | Bob | 3 | 4 | 3.5 |
134-
"""
135-
136-
df["average_score"] = df[["score_1", "score_2"]].mean(axis=1)
137-
138-
return df

learners/files/06-floating-point-data/statistics/test_stats.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
import numpy as np
2-
import pandas as pd
3-
41
from stats import (
52
sample_participants,
63
filter_participants_by_age,
74
filter_participants_by_height,
85
randomly_sample_and_filter_participants,
9-
remove_anomalies,
10-
calculate_frequency,
11-
calculate_cumulative_sum,
12-
calculate_player_total_scores,
13-
calculate_player_average_scores,
146
)
157

168
import random
@@ -88,50 +80,3 @@ def test_randomly_sample_and_filter_participants():
8880
)
8981
expected = [{"age": 38, "height": 165}, {"age": 30, "height": 170}, {"age": 35, "height": 160}]
9082
assert filtered_participants == expected
91-
92-
93-
def test_remove_anomalies():
94-
"""Test remove_anomalies function"""
95-
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
96-
maximum_value = 5
97-
minimum_value = 2
98-
expected_result = [2, 3, 4, 5]
99-
assert remove_anomalies(data, maximum_value, minimum_value) == expected_result
100-
101-
102-
def test_calculate_frequency():
103-
"""Test calculate_frequency function"""
104-
data = [1, 2, 3, 1, 2, 1, 1, 3, 3, 3]
105-
expected_result = {1: 4, 2: 2, 3: 4}
106-
assert calculate_frequency(data) == expected_result
107-
108-
109-
def test_calculate_cumulative_sum():
110-
"""Test calculate_cumulative_sum function"""
111-
array = np.array([1, 2, 3, 4, 5])
112-
expected_result = np.array([1, 3, 6, 10, 15])
113-
np.testing.assert_array_equal(calculate_cumulative_sum(array), expected_result)
114-
115-
116-
def test_calculate_player_total_scores():
117-
"""Test calculate_player_total_scores function"""
118-
participants = {
119-
"Alice": {"scores": np.array([1, 2, 3])},
120-
"Bob": {"scores": np.array([4, 5, 6])},
121-
"Charlie": {"scores": np.array([7, 8, 9])},
122-
}
123-
expected_result = {
124-
"Alice": {"scores": np.array([1, 2, 3]), "total_score": 6},
125-
"Bob": {"scores": np.array([4, 5, 6]), "total_score": 15},
126-
"Charlie": {"scores": np.array([7, 8, 9]), "total_score": 24},
127-
}
128-
np.testing.assert_equal(calculate_player_total_scores(participants), expected_result)
129-
130-
131-
def test_calculate_player_average_scores():
132-
"""Test calculate_player_average_scores function"""
133-
df = pd.DataFrame({"player": ["Alice", "Bob"], "score_1": [1, 3], "score_2": [2, 4]})
134-
expected_result = pd.DataFrame(
135-
{"player": ["Alice", "Bob"], "score_1": [1, 3], "score_2": [2, 4], "average_score": [1.5, 3.5]}
136-
)
137-
pd.testing.assert_frame_equal(calculate_player_average_scores(df), expected_result)
Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import numpy as np
2-
import pandas as pd
3-
41
import random
52

63

@@ -35,104 +32,3 @@ def randomly_sample_and_filter_participants(
3532
age_filtered_participants = filter_participants_by_age(sampled_participants, min_age, max_age)
3633
height_filtered_participants = filter_participants_by_height(age_filtered_participants, min_height, max_height)
3734
return height_filtered_participants
38-
39-
40-
def remove_anomalies(data: list, maximum_value: float, minimum_value: float) -> list:
41-
"""Remove anomalies from a list of numbers"""
42-
43-
result = []
44-
45-
for value in data:
46-
if minimum_value <= value <= maximum_value:
47-
result.append(value)
48-
49-
return result
50-
51-
52-
def calculate_frequency(data: list) -> dict:
53-
"""Calculate the frequency of each element in a list"""
54-
55-
frequencies = {}
56-
57-
# Iterate over each value in the list
58-
for value in data:
59-
# If the value is already in the dictionary, increment the count
60-
if value in frequencies:
61-
frequencies[value] += 1
62-
# Otherwise, add the value to the dictionary with a count of 1
63-
else:
64-
frequencies[value] = 1
65-
66-
return frequencies
67-
68-
69-
def calculate_cumulative_sum(array: np.ndarray) -> np.ndarray:
70-
"""Calculate the cumulative sum of a numpy array"""
71-
72-
# don't use the built-in numpy function
73-
result = np.zeros(array.shape)
74-
result[0] = array[0]
75-
for i in range(1, len(array)):
76-
result[i] = result[i - 1] + array[i]
77-
78-
return result
79-
80-
81-
def calculate_player_total_scores(participants: dict):
82-
"""Calculate the total score of each player in a dictionary.
83-
84-
Example input:
85-
{
86-
"Alice": {
87-
"scores": np.array([1, 2, 3])
88-
},
89-
"Bob": {
90-
"scores": np.array([4, 5, 6])
91-
},
92-
"Charlie": {
93-
"scores": np.array([7, 8, 9])
94-
},
95-
}
96-
97-
Example output:
98-
{
99-
"Alice": {
100-
"scores": np.array([1, 2, 3]),
101-
"total_score": 6
102-
},
103-
"Bob": {
104-
"scores": np.array([4, 5, 6]),
105-
"total_score": 15
106-
},
107-
"Charlie": {
108-
"scores": np.array([7, 8, 9]),
109-
"total_score": 24
110-
},
111-
}
112-
"""
113-
114-
for player in participants:
115-
participants[player]["total_score"] = np.sum(participants[player]["scores"])
116-
117-
return participants
118-
119-
120-
def calculate_player_average_scores(df: pd.DataFrame) -> pd.DataFrame:
121-
"""Calculate the average score of each player in a pandas DataFrame.
122-
123-
Example input:
124-
| | player | score_1 | score_2 |
125-
|---|---------|---------|---------|
126-
| 0 | Alice | 1 | 2 |
127-
| 1 | Bob | 3 | 4 |
128-
129-
Example output:
130-
| | player | score_1 | score_2 | average_score |
131-
|---|---------|---------|---------|---------------|
132-
| 0 | Alice | 1 | 2 | 1.5 |
133-
| 1 | Bob | 3 | 4 | 3.5 |
134-
"""
135-
136-
df["average_score"] = df[["score_1", "score_2"]].mean(axis=1)
137-
138-
return df
Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import numpy as np
2-
import pandas as pd
31
import pytest
42

53
from stats import (
64
sample_participants,
75
filter_participants_by_age,
86
filter_participants_by_height,
97
randomly_sample_and_filter_participants,
10-
remove_anomalies,
11-
calculate_frequency,
12-
calculate_cumulative_sum,
13-
calculate_player_total_scores,
14-
calculate_player_average_scores,
158
)
169

1710
import random
@@ -69,50 +62,3 @@ def test_randomly_sample_and_filter_participants(participants):
6962
)
7063
expected = [{"age": 38, "height": 165}, {"age": 30, "height": 170}, {"age": 35, "height": 160}]
7164
assert filtered_participants == expected
72-
73-
74-
def test_remove_anomalies():
75-
"""Test remove_anomalies function"""
76-
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
77-
maximum_value = 5
78-
minimum_value = 2
79-
expected_result = [2, 3, 4, 5]
80-
assert remove_anomalies(data, maximum_value, minimum_value) == expected_result
81-
82-
83-
def test_calculate_frequency():
84-
"""Test calculate_frequency function"""
85-
data = [1, 2, 3, 1, 2, 1, 1, 3, 3, 3]
86-
expected_result = {1: 4, 2: 2, 3: 4}
87-
assert calculate_frequency(data) == expected_result
88-
89-
90-
def test_calculate_cumulative_sum():
91-
"""Test calculate_cumulative_sum function"""
92-
array = np.array([1, 2, 3, 4, 5])
93-
expected_result = np.array([1, 3, 6, 10, 15])
94-
np.testing.assert_array_equal(calculate_cumulative_sum(array), expected_result)
95-
96-
97-
def test_calculate_player_total_scores():
98-
"""Test calculate_player_total_scores function"""
99-
participants = {
100-
"Alice": {"scores": np.array([1, 2, 3])},
101-
"Bob": {"scores": np.array([4, 5, 6])},
102-
"Charlie": {"scores": np.array([7, 8, 9])},
103-
}
104-
expected_result = {
105-
"Alice": {"scores": np.array([1, 2, 3]), "total_score": 6},
106-
"Bob": {"scores": np.array([4, 5, 6]), "total_score": 15},
107-
"Charlie": {"scores": np.array([7, 8, 9]), "total_score": 24},
108-
}
109-
np.testing.assert_equal(calculate_player_total_scores(participants), expected_result)
110-
111-
112-
def test_calculate_player_average_scores():
113-
"""Test calculate_player_average_scores function"""
114-
df = pd.DataFrame({"player": ["Alice", "Bob"], "score_1": [1, 3], "score_2": [2, 4]})
115-
expected_result = pd.DataFrame(
116-
{"player": ["Alice", "Bob"], "score_1": [1, 3], "score_2": [2, 4], "average_score": [1.5, 3.5]}
117-
)
118-
pd.testing.assert_frame_equal(calculate_player_average_scores(df), expected_result)

0 commit comments

Comments
 (0)