bootdotdev / curriculum

An open roadmap for Boot.dev's backend development curriculum
https://boot.dev
1.76k stars 77 forks source link

Bug in test cases for Ch 1.7 It's Math of Learn Functional Programming #17

Closed markalanboyd closed 10 months ago

markalanboyd commented 11 months ago

Ch 1.7 It's Math of Learn Functional Programming might have an error in the test cases.

Official solution

def get_median_font_size(font_sizes):
    if len(font_sizes) == 0:
        return None
    sorted_sizes = sorted(font_sizes)
    n = len(sorted_sizes)
    if n % 2 == 0:
        return (sorted_sizes[n // 2 - 1] + sorted_sizes[n // 2]) / 2
    else:
        return sorted_sizes[n // 2]

My solution

def get_median_font_size(font_sizes):
    if not font_sizes:
        return None
    idx1 = len(font_sizes) // 2
    if len(font_sizes) % 2 == 0:
        idx2 = idx1 - 1
        return (font_sizes[idx1] + font_sizes[idx2]) / 2
    else:
        return font_sizes[idx1]

Test cases - see how they're all presorted? They maybe shouldn't be? My code wouldn't work if they weren't.

run_cases = [
    ([10, 12, 14], 12),
    ([9, 11, 16, 20], 13.5),
]

submit_cases = run_cases + [
    ([8, 8, 8], 8),
    ([14, 18, 22, 30], 20.0),
    ([6, 6, 6, 24, 24, 24], 15.0),
    ([], None),
]

Suggested fix:

Add some unsorted run_cases or submit_cases