Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/69272dcf1c24b44fd79137c5.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69272dcf1c24b44fd79137c5 Challenge 142: Sum the String 29 challenge-142

--description--

Given a string containing digits and other characters, return the sum of all numbers in the string.

  • Treat consecutive digits as a single number. For example, "13" counts as 13, not 1 + 3.
  • Ignore any non-digit characters.

--hints--

string_sum("3apples2bananas") should return 5.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(string_sum("3apples2bananas"), 5)`)
}})

string_sum("10cats5dogs2birds") should return 17.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(string_sum("10cats5dogs2birds"), 17)`)
}})

string_sum("125344") should return 125344.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(string_sum("125344"), 125344)`)
}})

string_sum("a1b20c300") should return 321.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(string_sum("a1b20c300"), 321)`)
}})

string_sum("a12b34c56d78e90f123g456h789i0j1k2l3m4n5") should return 1653.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(string_sum("a12b34c56d78e90f123g456h789i0j1k2l3m4n5"), 1653)`)
}})

--seed--

--seed-contents--

def string_sum(s):

    return s

--solutions--

import re
def string_sum(s):
    numbers = re.findall(r'\d+', s)
    return sum(int(num) for num in numbers)