Coding Challenges: NumPy for Data Analysis
8 HackerRank-style challenges, auto-graded right in your browser
using real NumPy — no local install needed. Write your
function in the code box, then click Run Tests to
check it against a set of test cases (including edge cases). Stuck?
Every challenge has a Show Solution reveal at the
bottom. Only uses what Chapters 1-23 have covered so far. No import
beyond
numpy/json/os/re/math/datetime/random/csv/threading/concurrent.futures.
list(...)/int(...)/float(...))
so the grader can compare them reliably.
1 Sum a List as a NumPy Array Easy
Write a function array_from_list_sum(values) that converts values (a plain Python list of numbers) to a NumPy array and returns its sum as a plain Python int.
values is a list of ints (possibly empty).
array_from_list_sum([1, 2, 3, 4, 5]) → 15array_from_list_sum([]) → 0
Show Solution
import numpy as np
def array_from_list_sum(values):
return int(np.array(values).sum())
2 Build a Zeros List Easy
Write a function make_zeros_list(n) that returns np.zeros(n) converted to a plain Python list of floats.
n is a non-negative int.
make_zeros_list(3) → [0.0, 0.0, 0.0]make_zeros_list(0) → []
Show Solution
import numpy as np
def make_zeros_list(n):
return [float(x) for x in np.zeros(n)]
3 Filter Above a Threshold Medium
Write a function filter_above(values, threshold) that converts values to a NumPy array, uses boolean masking to keep only elements strictly greater than threshold, and returns them as a plain Python list of ints.
values is a list of ints.
filter_above([3, 8, 1, 9, 4], 5) → [8, 9]
Show Solution
import numpy as np
def filter_above(values, threshold):
arr = np.array(values)
return [int(x) for x in arr[arr > threshold]]
4 Scale a List of Values Medium
Write a function scale_list(values, factor) that converts values to a NumPy array, multiplies every element by factor (vectorized, no loop), and returns the result as a plain Python list of floats.
values is a list of numbers; factor is a number.
scale_list([1, 2, 3], 10) → [10.0, 20.0, 30.0]
Show Solution
import numpy as np
def scale_list(values, factor):
arr = np.array(values, dtype=float)
return [float(x) for x in arr * factor]
5 Sum a Column of a Grid Medium
Write a function column_sum(grid, col_index) that converts grid (a list of lists) to a 2D NumPy array and returns the sum of column col_index as a plain int.
grid is a rectangular list of lists of ints; col_index is a valid column index.
column_sum([[1, 2], [3, 4], [5, 6]], 0) → 9
Show Solution
import numpy as np
def column_sum(grid, col_index):
arr = np.array(grid)
return int(arr[:, col_index].sum())
6 Average of a Row Medium
Write a function row_average(grid, row_index) that converts grid to a 2D NumPy array and returns the average of row row_index as a plain float.
grid is a rectangular list of lists of ints; row_index is a valid row index.
row_average([[1, 2, 3], [4, 5, 6]], 0) → 2.0
Show Solution
import numpy as np
def row_average(grid, row_index):
arr = np.array(grid)
return float(arr[row_index].mean())
7 Flatten a Grid Easy
Write a function flatten_grid(grid) that converts grid to a 2D NumPy array, flattens it, and returns the result as a plain Python list of ints.
grid is a rectangular list of lists of ints.
flatten_grid([[1, 2], [3, 4]]) → [1, 2, 3, 4]
Show Solution
import numpy as np
def flatten_grid(grid):
return [int(x) for x in np.array(grid).flatten()]
8 Combine Two Lists into One Array Easy
Write a function combine_lists(list1, list2) that converts both lists to NumPy arrays, joins them end-to-end with np.concatenate(), and returns the result as a plain Python list of ints.
list1 and list2 are lists of ints (either may be empty).
combine_lists([1, 2], [3, 4]) → [1, 2, 3, 4]
Show Solution
import numpy as np
def combine_lists(list1, list2):
a = np.array(list1, dtype=int)
b = np.array(list2, dtype=int)
return [int(x) for x in np.concatenate([a, b])]