Last active
April 27, 2022 17:26
-
-
Save oze4/639672b58b8c702687f9a1f919f8852e to your computer and use it in GitHub Desktop.
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 run(): | |
i = input("enter 20 numbers separated by spaces: ") | |
iList = i.split(" ") | |
validate_input(iList, 20) | |
nums = list_str_to_list_int(iList) | |
return calculate_sum(nums) | |
def validate_input(strList, expectedTotal): | |
l = len(strList) | |
if l != expectedTotal: | |
raise Exception("expected {0} got {1}".format(expectedTotal, l)) | |
def list_str_to_list_int(nl): | |
return list(map(int, nl)) | |
def calculate_sum(numList): | |
sum = { "evenTotal": 0, "oddTotal": 0 } | |
for n in numList: | |
if is_even(n): | |
sum["evenTotal"] += n | |
else: | |
sum["oddTotal"] += n | |
return sum | |
def is_even(n): | |
return n % 2 == 0 | |
results = run() | |
print("sum of even nums: %s" % results["evenTotal"]) | |
print("sum of odd nums: %s" % results["oddTotal"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment