Problem 1 โ€” Even Count

Write a function count_even(lst) that returns how many numbers in lst are even.

def count_even(lst):
    return sum(1 for x in lst if x % 2 == 0)

Problem 2 โ€” Maximum

Without using max(), find the largest number.

def my_max(lst):
    best = lst[0]
    for x in lst[1:]:
        if x > best:
            best = x
    return best

Problem 3 โ€” Reverse In-Place

def reverse_inplace(lst):
    i, j = 0, len(lst) - 1
    while i < j:
        lst[i], lst[j] = lst[j], lst[i]
        i += 1; j -= 1

Problem 4 โ€” Remove Duplicates Preserving Order

def dedupe(lst):
    seen, out = set(), []
    for x in lst:
        if x not in seen:
            seen.add(x)
            out.append(x)
    return out

Problem 5 โ€” Second Largest

def second_largest(lst):
    first = second = float("-inf")
    for x in lst:
        if x > first:
            second, first = first, x
        elif x > second and x != first:
            second = x
    return second

Problem 6 โ€” Running Average

def running_avg(lst):
    out, total = [], 0
    for i, x in enumerate(lst, 1):
        total += x
        out.append(total / i)
    return out

Problem 7 โ€” Merge Two Sorted Lists

def merge(a, b):
    i = j = 0; out = []
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            out.append(a[i]); i += 1
        else:
            out.append(b[j]); j += 1
    out.extend(a[i:]); out.extend(b[j:])
    return out

Problem 8 โ€” Flatten 2D List

def flatten(grid):
    return [x for row in grid for x in row]

Problem 9 โ€” Rotate Left by k

def rotate_left(lst, k):
    k %= len(lst)
    return lst[k:] + lst[:k]

Problem 10 โ€” Chunk Into Groups of n

def chunk(lst, n):
    return [lst[i:i+n] for i in range(0, len(lst), n)]

Practise this on PyForm โ€” free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm โ†’