forked from livinNector/python-programming-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. Data Processing
More file actions
161 lines (135 loc) · 3.13 KB
/
2. Data Processing
File metadata and controls
161 lines (135 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
title: Data Processing with Basic Patterns
---
# Problem Statement
Given a list of integers `data` and a string `operation`, implement the function `process_data(data: List[int], operation: str) -> Any`. The function should perform the following operations based on the input:
1. **Mapping**: If the operation is `"double"`, return a list where each element is doubled.
2. **Filtering**: If the operation is `"even"`, return a list containing only the even numbers.
3. **Aggregation**: If the operation is `"sum"`, return the sum of all elements.
4. **Sorting**: If the operation is `"sort"`, return the list sorted in ascending order.
5. **Grouping**: If the operation is `"group"`, return a dictionary where the keys are the elements, and the values are the counts of their occurrences.
6. **Uniques**: If the operation is `"unique"`, return a sorted list of unique elements.
**Examples**
```py3
process_data([1, 2, 3, 4, 5], "double") # [2, 4, 6, 8, 10]
process_data([1, 2, 3, 4, 5], "even") # [2, 4]
process_data([1, 2, 3, 4, 5], "sum") # 15
process_data([5, 3, 1, 4, 2], "sort") # [1, 2, 3, 4, 5]
process_data([1, 1, 2, 3, 2], "group") # {1: 2, 2: 2, 3: 1}
process_data([1, 2, 3, 2, 1], "unique")# [1, 2, 3]
```
# Solution
```py3
def process_data(data: list[int], operation: str) -> any:
"""
Perform basic data processing operations on a list of integers.
Arguments:
data: list[int] - The list of integers.
operation: str - The operation to perform ("double", "even", "sum", "sort", "group", "unique").
Returns:
any - The result of the operation.
"""
if operation == "double":
return [x * 2 for x in data]
elif operation == "even":
return [x for x in data if x % 2 == 0]
elif operation == "sum":
return sum(data)
elif operation == "sort":
return sorted(data)
elif operation == "group":
from collections import Counter
return dict(Counter(data))
elif operation == "unique":
return sorted(set(data))
else:
raise ValueError("Invalid operation")
```
# Public Test Cases
## Input 1
```
is_equal(
process_data([1, 2, 3, 4, 5], "double"),
[2, 4, 6, 8, 10]
)
```
## Output 1
```
True
```
## Input 2
```
is_equal(
process_data([1, 2, 3, 4, 5], "even"),
[2, 4]
)
```
## Output 2
```
True
```
## Input 3
```
is_equal(
process_data([1, 2, 3, 4, 5], "sum"),
15
)
```
## Output 3
```
True
```
## Input 4
```
is_equal(
process_data([5, 3, 1, 4, 2], "sort"),
[1, 2, 3, 4, 5]
)
```
## Output 4
```
True
```
# Private Test Cases
## Input 1
```
data = [3, 3, 2, 1, 1, 1]
operation = "group"
is_equal(
process_data(data, operation),
{3: 2, 2: 1, 1: 3}
)
```
## Output 1
```
True
```
## Input 2
```
data = [8, 5, 7, 3, 3, 5, 8]
operation = "unique"
is_equal(
process_data(data, operation),
[3, 5, 7, 8]
)
```
## Output 2
```
True
```
## Input 3
```
data = [6, 7, 8, 9]
operation = "even"
is_equal(
process_data(data, operation),
[6, 8]
)
```
## Output 3
```
True
```
# Notes
- The function should handle invalid operations by raising a `ValueError`.
- Input validation is not required for this problem.