0
def sum_of_integers_in_string(s):
    sum = 0
    for x in s:
        if x.isdigit() == True: #check if its a digit
            if isinstance(x, int) == True: #check if its int and sum
                sum += int(x)
            if isinstance(x, float) == True: #check if its float and sum
                sum += float(x)           
        else:
            sum = 0
        return sum

I'm trying to solve this kata, however, it do not passes any test from these:

exampleTests = (
("12.4", 16),
("h3ll0w0rld", 3),
("2 + 3 = ", 5),
("Our company made approximately 1 million in gross revenue last quarter.", 1),
("The Great Depression lasted from 1929 to 1939.", 3868),
("Dogs are our best friends.", 0),
("C4t5 are 4m4z1ng.", 18),
("The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog", 3635)
)

I think there is any issue in the part of summing an int or float. Any help?

zain
  • 25
  • 5
  • 1
    Does this answer your question? [How can I check if a string represents an int, without using try/except?](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except) – Yevhen Kuzmovych Feb 01 '23 at 12:26
  • 5
    if you want "12.4" to go 12+4, you cannot look at every digit of the string, otherwise you would get 1+2+4. You have to parse your string until you get to a non digit character, and use the previous characters as an int – XaC Feb 01 '23 at 12:27
  • And [Checking if a string can be converted to float in Python](https://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python) – Yevhen Kuzmovych Feb 01 '23 at 12:27
  • And [How to extract numbers from a string in Python?](https://stackoverflow.com/questions/4289331/how-to-extract-numbers-from-a-string-in-python) – Yevhen Kuzmovych Feb 01 '23 at 12:28
  • `for x in s:` iterates over string character by character, `x` could never be `int` or `float`, it's always a one-char string. Also I don't see why you consider floats at all if you want`"12.4"` to make `16`. – Nikolaj Š. Feb 01 '23 at 12:30
  • Also you return `sum` after the first char is being processed. So even if you fix type mishandling, you'll get `1` for `"12.4"`. – Nikolaj Š. Feb 01 '23 at 12:35

2 Answers2

1

It's because you are adding the digits not the entire number. For example, in the first test:

Your program is doing this: 1+2+4 = 7

And it should do: 12+4 = 16

Because 12 it's a unique number not 1 and 2.

And float never must be added.

You should try this:

import re

def sum_of_integers_in_string(s):
    s = [int(s) for s in re.findall(r'\d+', s)]
    return sum(s)

Using regular expressions it's easier. you split the entire string to get only the digits that are together. and then you can add them with sum function that add every element of a list.

  • 1
    I would change it to `return sum(int(s) for s in re.findall(r'\d+', s))`, thus there's no list generated only to be "reduced" to one number by `sum`. There's hardly any difference for simple cases, but for big files it could be noticeable. – Nikolaj Š. Feb 02 '23 at 09:23
  • What is findall() used for? – zain Feb 02 '23 at 11:28
  • 1
    findall() is a function from "re" library that returns all the strings of each match in the searched string. In this case. It will return all the numbers that are together because the regular expression is "\d+" (all the digits 1+) in the first arg and "s" our string in the second arg. Thank you @KlasŠ. I'm agree with you – Juan Saavedra Feb 02 '23 at 15:19
0

You must keep track of whether of not you are inside of a number, as @Xac said, here is something to put you on the right track:

def sum_of_integers_in_string(s):
    sum = 0
    inside_number = False
    this_num = ""
    for x in s:
        if x.isdigit() == True: #check if its a digit
            inside_number = True
            this_num += x        
        else:
            pass # complete this function! 
        return sum
Caridorc
  • 6,222
  • 2
  • 31
  • 46