🐍 Python for Everyone

← Back to practice bank

Chapter 23 Coding Challenges 50-65 min, auto-graded

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.

How grading works Your code runs entirely in your browser using Pyodide (Python compiled to WebAssembly), with NumPy loaded automatically alongside it — nothing is uploaded anywhere. Keep the function name in the stub exactly as given, since the grader calls it by name. Return plain Python lists/numbers from your function (e.g. wrap results in 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.

Constraints: values is a list of ints (possibly empty).
Examples:
array_from_list_sum([1, 2, 3, 4, 5]) → 15
array_from_list_sum([]) → 0
challenge_1.py
Show Solution
solution_1.py
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.

Constraints: n is a non-negative int.
Examples:
make_zeros_list(3) → [0.0, 0.0, 0.0]
make_zeros_list(0) → []
challenge_2.py
Show Solution
solution_2.py
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.

Constraints: values is a list of ints.
Examples:
filter_above([3, 8, 1, 9, 4], 5) → [8, 9]
challenge_3.py
Show Solution
solution_3.py
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.

Constraints: values is a list of numbers; factor is a number.
Examples:
scale_list([1, 2, 3], 10) → [10.0, 20.0, 30.0]
challenge_4.py
Show Solution
solution_4.py
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.

Constraints: grid is a rectangular list of lists of ints; col_index is a valid column index.
Examples:
column_sum([[1, 2], [3, 4], [5, 6]], 0) → 9
challenge_5.py
Show Solution
solution_5.py
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.

Constraints: grid is a rectangular list of lists of ints; row_index is a valid row index.
Examples:
row_average([[1, 2, 3], [4, 5, 6]], 0) → 2.0
challenge_6.py
Show Solution
solution_6.py
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.

Constraints: grid is a rectangular list of lists of ints.
Examples:
flatten_grid([[1, 2], [3, 4]]) → [1, 2, 3, 4]
challenge_7.py
Show Solution
solution_7.py
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.

Constraints: list1 and list2 are lists of ints (either may be empty).
Examples:
combine_lists([1, 2], [3, 4]) → [1, 2, 3, 4]
challenge_8.py
Show Solution
solution_8.py
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])]
← Practice Bank Interview Questions →