-
Code def add(string, option): odd,even = 0,0 for x in string: x = int(x) if x%2 ==0:even+=x if x%2 ==1:odd+=x if option ==0: return odd+even return even if option == 2 else odd
Preloaded Code def add(string,option): return #total depending on the option
Test Cases test.assert_equals(add("1234567", 0),28) test.assert_equals(add("1234567", 1),16) test.assert_equals(add("1234567", 2),12)
Output:
-
Code - def add(string, option):
even_list = []odd_list = []all_sum = 0for num in string:num = int(num)all_sum += numif num % 2 == 0:even_list.append(num)if num % 2 != 0:odd_list.append(num)if option == 0:return all_sumif option == 1:return sum(odd_list)if option == 2:return sum(even_list)- odd,even = 0,0
- for x in string:
- x = int(x)
- if x%2 ==0:even+=x
- if x%2 ==1:odd+=x
- if option ==0:
- return odd+even
- return even if option == 2 else odd
Test Cases test.assert_equals(add("1234567" , 0),28)test.assert_equals(add("1234567" , 1),16)test.assert_equals(add("1234567" , 2),12)- test.assert_equals(add("1234567", 0),28)
- test.assert_equals(add("1234567", 1),16)
- test.assert_equals(add("1234567", 2),12)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}