Home > Blockchain >  changing from string numbers in a list to int and sum them up. Number strings are separated by chara
changing from string numbers in a list to int and sum them up. Number strings are separated by chara

Time:09-24

Implement a function called add_even_numbers(). The function takes in a list of strings called str_list (type: list) as its parameter, where each string in str_list contains a sequence of positive integers separated by '|', e.g., '12|89|34'. The function should add up all the even numbers found in the given list. You can assume that each string in str_list is always properly formatted as described above and always contains at least one integer. It is possible, though, for str_list to be empty.

E.g.,

  • add_even_numbers(['1|2|3', '10|50']) returns 62 (because 2 10 50 = 62, while 1 and 3 are not added).
  • add_even_numbers(['99|1|27', '11|5']) returns 0 (because there is no even number).
  • add_even_numbers(['1', '12']) returns 12.
  • add_even_numbers([]) returns 0.

The code i had is:



def add_even_numbers(str_list):
    count=[]
    num=""
    int_num=0
    accepted="02468" 
    for x in str_list:
        num=""
        for i in x:
            if i!="|":
                num=num i
            
                    
            elif i=="|":
                if int(num)%2==0:
                    int_num =int(num)
                    
                    num=""
            elif i==x[-1]:
                if int(num)%2==0:
                    int_num =int(num)

                    num=""
            
    return int_num

the way i did it was that i try to change all the string between into int and sum them up but i am not getting the answer. Please help me correct this code thank you in advance

These are the codes for testing

print()
print('-' * 20)
print()

print("Test Case 1: add_even_numbers(['1|2|3', '10|50']))")

print()

result = add_even_numbers(['1|2|3', '10|50'])
print('Expected: 62')
print('Actual:   '   str(result))

print("Expected type of returned value: <class 'int'>")
print('Actual type of returned value:   '   str(type(result)))

print()
print('-' * 20)
print()

print("Test Case 2: add_even_numbers(['99|1|27', '11|5'])")

print()

result = add_even_numbers(['99|1|27', '11|5'])
print('Expected: 0')
print('Actual:   '   str(result))

print()
print('-' * 20)
print()

print("Test Case 3: add_even_numbers(['1', '12'])")

print()

result = add_even_numbers(['1', '12'])
print('Expected: 12')
print('Actual:   '   str(result))

print()
print('-' * 20)
print()

print("Test Case 4: add_even_numbers([])")

print()

result = add_even_numbers([])
print('Expected: 0')
print('Actual:   '   str(result))

CodePudding user response:

You may not be allowed to use regex in your assignment, but if you can, there is a very straightforward approach:

def add_even_numbers(str_list):
    total = 0
    for str in str_list:
        total  = sum([int(x) for x in re.findall(r'\d ', str) if int(x) % 2 == 0])

    return total

print(add_even_numbers(['1|2|3', '10|50']))  # 62

CodePudding user response:

You can do:

def add_even_numbers(str_list):
    return sum(n for n in map(
        int, (x for s in str_list for x in s.split("|"))) if not n%2)

>>> add_even_numbers(['1|2|3', '10|50'])
62
>>> add_even_numbers(['99|1|27', '11|5'])
0
>>> add_even_numbers(['1', '12'])
12
>>> add_even_numbers([])
0
  • Related