Last active
September 8, 2018 15:27
-
-
Save jonaed1230/bae8efca32ad96a126dbdd37cd82ab12 to your computer and use it in GitHub Desktop.
Making average of different types input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def average(L): | |
if not L: | |
return None | |
return sum(L)/len(L) | |
def test_average(): | |
test_cases = [ | |
{ | |
"name": "simple case 1", | |
"input": [1, 2, 3], | |
"expected": 2.0 | |
}, | |
{ | |
"name": "simple case 2", | |
"input": [1, 2, 3, 4], | |
"expected": 2.0 | |
}, | |
{ | |
"name": "list with one item", | |
"input": [100], | |
"expected": 100.0 | |
}, | |
{ | |
"name": "empty list", | |
"input": [], | |
"expected": None | |
} | |
] | |
for test_case in test_cases: | |
assert test_case["expected"] == average(test_case["input"]), test_case["name"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment