-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathproblem_solving.py
More file actions
135 lines (107 loc) · 3.92 KB
/
problem_solving.py
File metadata and controls
135 lines (107 loc) · 3.92 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
# =========================================================
# THE PYTHON GAUNTLET
# =========================================================
# ---------------------------------------------------------
# LEVEL 1: WARM UP (Strings)
# ---------------------------------------------------------
def clean_and_reverse(text):
"""
1. Receive a string (e.g., " Hello World ").
2. Remove any leading/trailing whitespace.
3. Convert the entire string to lowercase.
4. Return the reversed version of that cleaned string.
Example: " Python " -> "nohtyp"
"""
# TODO: Write your code here
pass
# ---------------------------------------------------------
# LEVEL 2: DATA RESTRUCTURING (Dictionaries)
# ---------------------------------------------------------
def group_by_department(employees):
"""
1. Receive a list of dictionaries. Each dict represents an employee:
[{'name': 'Alice', 'dept': 'Sales'}, {'name': 'Bob', 'dept': 'Sales'}, {'name': 'Charlie', 'dept': 'IT'}]
2. Return a NEW dictionary where the keys are departments and the values are LISTS of names.
Example Output:
{
'Sales': ['Alice', 'Bob'],
'IT': ['Charlie']
}
"""
# TODO: Write your code here
pass
# ---------------------------------------------------------
# LEVEL 3: LOGIC & MATH (Loops)
# ---------------------------------------------------------
def sum_of_primes(n):
"""
1. Receive an integer n.
2. Return the SUM of all prime numbers up to (and including) n.
3. Recall: A prime number is greater than 1 and only divisible by 1 and itself.
Example: n=5. Primes are 2, 3, 5. Sum = 10.
"""
# TODO: Write your code here
pass
# ---------------------------------------------------------
# LEVEL 4: ALGORITHMIC THINKING (Compression)
# ---------------------------------------------------------
def compress_string(text):
"""
1. Implement Run-Length Encoding.
2. Receive a string of characters (e.g., "AAABBCCCA").
3. Return a compressed string showing the character and its count.
Example Input: "AAABBCCCA"
Example Output: "3A2B3C1A"
Note: If the input is empty, return empty string.
"""
# TODO: Write your code here
pass
# ---------------------------------------------------------
# LEVEL 5: LOGIC TRAPS (State Tracking)
# ---------------------------------------------------------
def get_second_largest(numbers):
"""
1. Receive a list of distinct integers.
2. Return the SECOND largest number in the list.
3. Do NOT use the built-in .sort() method or sorted() function.
Example: [10, 20, 4, 45, 99] -> 45
"""
# TODO: Write your code here
pass
# ---------------------------------------------------------
# LEVEL 6: 2D ARRAYS (Matrices)
# ---------------------------------------------------------
def sum_main_diagonal(matrix):
"""
1. Receive a list of lists (a square matrix).
2. Calculate the sum of the numbers on the main diagonal (top-left to bottom-right).
Example Input:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Logic: 1 + 5 + 9
Example Output: 15
"""
# TODO: Write your code here
pass
# ---------------------------------------------------------
# LEVEL 7: COMPLEX VALIDATION (The Boss)
# ---------------------------------------------------------
def validate_ipv4(ip_string):
"""
1. Receive a string (e.g., "192.168.0.1").
2. Return True if it is a valid IPv4 address, False otherwise.
3. Rules for Valid:
- Must contain exactly 4 parts separated by dots.
- Each part must be a number.
- Each number must be between 0 and 255 (inclusive).
- Examples:
"192.168.1.1" -> True
"256.0.0.0" -> False (256 is too high)
"192.168.1" -> False (Not enough parts)
"abc.0.0.1" -> False (Not a number)
"""
# TODO: Write your code here
pass