Skip to content

Commit be59d08

Browse files
author
Brian Elinsky
committed
Add numpy-pandas practice problems (L1 basics)
1 parent 7644e82 commit be59d08

File tree

7 files changed

+228
-25
lines changed

7 files changed

+228
-25
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Array Creation Exercises
3+
4+
Problem N1.01 - N1.03
5+
"""
6+
import numpy as np
7+
8+
9+
def identity_matrix_3x3() -> np.ndarray:
10+
"""
11+
N1.01: Create a 3x3 identity matrix.
12+
13+
Returns:
14+
np.ndarray: 3x3 identity matrix
15+
"""
16+
# TODO - you fill in here
17+
pass
18+
19+
20+
def zeros_with_fifth_one() -> np.ndarray:
21+
"""
22+
N1.02: Create an array of 10 zeros, then set the 5th value (index 4) to 1.
23+
24+
Returns:
25+
np.ndarray: Array of shape (10,) with zeros except index 4 is 1
26+
"""
27+
# TODO - you fill in here
28+
pass
29+
30+
31+
def random_array_0_to_1() -> np.ndarray:
32+
"""
33+
N1.03: Create an array of 10 random numbers between 0 and 1.
34+
35+
Returns:
36+
np.ndarray: Array of shape (10,) with random values in [0, 1)
37+
"""
38+
# TODO - you fill in here
39+
pass
40+
41+
42+
# ============ Tests ============
43+
if __name__ == '__main__':
44+
# Test N1.01
45+
result = identity_matrix_3x3()
46+
expected = np.eye(3)
47+
assert result is not None, "N1.01: Function returned None"
48+
assert result.shape == (3, 3), f"N1.01: Expected shape (3,3), got {result.shape}"
49+
assert np.array_equal(result, expected), "N1.01: Not an identity matrix"
50+
print("N1.01 identity_matrix_3x3: PASSED")
51+
52+
# Test N1.02
53+
result = zeros_with_fifth_one()
54+
expected = np.zeros(10)
55+
expected[4] = 1
56+
assert result is not None, "N1.02: Function returned None"
57+
assert result.shape == (10,), f"N1.02: Expected shape (10,), got {result.shape}"
58+
assert np.array_equal(result, expected), f"N1.02: Expected {expected}, got {result}"
59+
print("N1.02 zeros_with_fifth_one: PASSED")
60+
61+
# Test N1.03
62+
result = random_array_0_to_1()
63+
assert result is not None, "N1.03: Function returned None"
64+
assert result.shape == (10,), f"N1.03: Expected shape (10,), got {result.shape}"
65+
assert np.all((result >= 0) & (result < 1)), "N1.03: Values should be in [0, 1)"
66+
print("N1.03 random_array_0_to_1: PASSED")
67+
68+
print("\nAll array creation tests passed!")
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Broadcasting Exercises
3+
4+
Problem N1.06
5+
"""
6+
import numpy as np
7+
8+
9+
def add_1d_to_each_row(arr_2d: np.ndarray, arr_1d: np.ndarray) -> np.ndarray:
10+
"""
11+
N1.06: Add a 1D array to each row of a 2D array.
12+
13+
Example:
14+
arr_2d = [[1, 1, 1],
15+
[2, 2, 2],
16+
[3, 3, 3]]
17+
arr_1d = [1, 2, 3]
18+
19+
Output = [[2, 3, 4],
20+
[3, 4, 5],
21+
[4, 5, 6]]
22+
23+
Args:
24+
arr_2d: 2D array of shape (n, m)
25+
arr_1d: 1D array of shape (m,)
26+
27+
Returns:
28+
np.ndarray: Result of adding arr_1d to each row of arr_2d
29+
"""
30+
# TODO - you fill in here
31+
pass
32+
33+
34+
# ============ Tests ============
35+
if __name__ == '__main__':
36+
# Test N1.06
37+
arr_2d = np.array([[1, 1, 1],
38+
[2, 2, 2],
39+
[3, 3, 3]])
40+
arr_1d = np.array([1, 2, 3])
41+
result = add_1d_to_each_row(arr_2d, arr_1d)
42+
expected = np.array([[2, 3, 4],
43+
[3, 4, 5],
44+
[4, 5, 6]])
45+
assert result is not None, "N1.06: Function returned None"
46+
assert np.array_equal(result, expected), f"N1.06: Expected\n{expected}\ngot\n{result}"
47+
print("N1.06 add_1d_to_each_row: PASSED")
48+
49+
# Test with different values
50+
arr_2d = np.ones((3, 3))
51+
arr_1d = np.array([10, 20, 30])
52+
result = add_1d_to_each_row(arr_2d, arr_1d)
53+
expected = np.array([[11, 21, 31],
54+
[11, 21, 31],
55+
[11, 21, 31]])
56+
assert np.array_equal(result, expected), f"N1.06 (test 2): Expected\n{expected}\ngot\n{result}"
57+
print("N1.06 add_1d_to_each_row (test 2): PASSED")
58+
59+
print("\nAll broadcasting tests passed!")
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Indexing and Slicing Exercises
3+
4+
Problem N1.04 - N1.05
5+
"""
6+
import numpy as np
7+
8+
9+
def last_three_reversed(a: np.ndarray) -> np.ndarray:
10+
"""
11+
N1.04: Given an array, extract the last 3 elements and reverse them.
12+
13+
Example:
14+
Input: np.arange(10, 100, 10) # [10, 20, 30, 40, 50, 60, 70, 80, 90]
15+
Output: [90, 80, 70]
16+
17+
Args:
18+
a: Input array
19+
20+
Returns:
21+
np.ndarray: Last 3 elements in reverse order
22+
"""
23+
# TODO - you fill in here
24+
pass
25+
26+
27+
def replace_even_with_negative_one(a: np.ndarray) -> np.ndarray:
28+
"""
29+
N1.05: Replace all even numbers with -1 in an array.
30+
31+
Note: Modify and return a copy, don't modify the original.
32+
33+
Example:
34+
Input: [1, 2, 3, 4, 5, 6]
35+
Output: [1, -1, 3, -1, 5, -1]
36+
37+
Args:
38+
a: Input array of integers
39+
40+
Returns:
41+
np.ndarray: Array with even numbers replaced by -1
42+
"""
43+
# TODO - you fill in here
44+
pass
45+
46+
47+
# ============ Tests ============
48+
if __name__ == '__main__':
49+
# Test N1.04
50+
a = np.arange(10, 100, 10)
51+
result = last_three_reversed(a)
52+
expected = np.array([90, 80, 70])
53+
assert result is not None, "N1.04: Function returned None"
54+
assert np.array_equal(result, expected), f"N1.04: Expected {expected}, got {result}"
55+
print("N1.04 last_three_reversed: PASSED")
56+
57+
# Test N1.05
58+
a = np.array([1, 2, 3, 4, 5, 6])
59+
result = replace_even_with_negative_one(a)
60+
expected = np.array([1, -1, 3, -1, 5, -1])
61+
assert result is not None, "N1.05: Function returned None"
62+
assert np.array_equal(result, expected), f"N1.05: Expected {expected}, got {result}"
63+
# Make sure original wasn't modified
64+
assert np.array_equal(a, np.array([1, 2, 3, 4, 5, 6])), "N1.05: Original array was modified"
65+
print("N1.05 replace_even_with_negative_one: PASSED")
66+
67+
print("\nAll indexing/slicing tests passed!")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Solutions for Array Creation Exercises"""
2+
import numpy as np
3+
4+
5+
def identity_matrix_3x3() -> np.ndarray:
6+
return np.eye(3)
7+
8+
9+
def zeros_with_fifth_one() -> np.ndarray:
10+
arr = np.zeros(10)
11+
arr[4] = 1
12+
return arr
13+
14+
15+
def random_array_0_to_1() -> np.ndarray:
16+
return np.random.random(10)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Solutions for Broadcasting Exercises"""
2+
import numpy as np
3+
4+
5+
def add_1d_to_each_row(arr_2d: np.ndarray, arr_1d: np.ndarray) -> np.ndarray:
6+
return arr_2d + arr_1d
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Solutions for Indexing and Slicing Exercises"""
2+
import numpy as np
3+
4+
5+
def last_three_reversed(a: np.ndarray) -> np.ndarray:
6+
return a[-3:][::-1]
7+
8+
9+
def replace_even_with_negative_one(a: np.ndarray) -> np.ndarray:
10+
result = a.copy()
11+
result[result % 2 == 0] = -1
12+
return result

operations.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)