Add two large numbers stored as digit lists
Your task
A settlement service keeps very large amounts as lists of decimal digits, most significant digit first, because an amount can run to 100,000 digits.
Write add_two_numbers(a, b) that returns the sum of the two amounts as a list of digits in the same form.
Input and output:
a,b: two non-negative integers, each a list of 1 to 100,000 digits from 0 to 9, most significant digit first.- Returns the sum as a list of digits from 0 to 9, most significant digit first.
Rules:
- The two lists can have different lengths.
- An input never starts with 0, except the number zero itself, which is
[0]. - The result never starts with 0, except a sum of zero, which is
[0].
Examples
add_two_numbers(a: list[int], b: list[int]) -> list[int]Example 1
Input
add_two_numbers([7, 2, 4, 3], [5, 6, 4])Output
[7, 8, 0, 7]Example 2
Input
add_two_numbers([4, 6], [5, 8])Output
[1, 0, 4]