← Back to challenges

Return the Sum of Two Numbers (With a Twist)

PythonHardmathalgorithmsnumbers

Instructions

Wait, a very hard challenge for returning the sum of two numbers? Well in this one, the numbers can have 1000 digits!

So, what's the twist? You have to do the summation as if you're doing it manually on a piece of paper, thus, the conversion of the numeric string to numeric literal is basically disallowed.

Create a function that:

  • Takes two numbers as strings.
  • Calculates the sum of these two numbers.
  • Returns the result as a string.

Examples

sum2("5125515215521515", "125261616261626") ➞ "5250776831783141"

sum2("6666666666666666666666666666", "99999999999999999999999") ➞ "6666766666666666666666666665"

sum2("123456789123456789123456789", "987654321987654321987654329876543") ➞ "987654445444443445444443453333332"

Notes

  • All numbers are positive integers with no leading zeros.
  • Remember how to sum two numbers ON PAPER; it could be useful.
  • Your function must run in less than 10 seconds because Innokodakademija has a time limit.
  • Conversion of numeric strings to numeric literals is prohibited -- it will defeat the purpose and the level of difficulty associated to this challenge.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Burglary Series (02): Most Valuable Item