MCPcopy
hub / github.com/keon/algorithms / plus_one_v2

Function plus_one_v2

algorithms/array/plus_one.py:46–66  ·  view source on GitHub ↗

Add one to a big-endian digit array by scanning from the right. Args: digits: Non-empty list of digits representing a non-negative integer. Returns: List of digits representing the input number plus one. Examples: >>> plus_one_v2([1, 2, 9]) [1, 3, 0]

(digits: list[int])

Source from the content-addressed store, hash-verified

44
45
46def plus_one_v2(digits: list[int]) -> list[int]:
47 """Add one to a big-endian digit array by scanning from the right.
48
49 Args:
50 digits: Non-empty list of digits representing a non-negative integer.
51
52 Returns:
53 List of digits representing the input number plus one.
54
55 Examples:
56 >>> plus_one_v2([1, 2, 9])
57 [1, 3, 0]
58 """
59 length = len(digits)
60 for index in range(length - 1, -1, -1):
61 if digits[index] < 9:
62 digits[index] += 1
63 return digits
64 digits[index] = 0
65 digits.insert(0, 1)
66 return digits
67
68
69def plus_one_v3(num_arr: list[int]) -> list[int]:

Callers 1

test_plus_one_v2Method · 0.90

Calls 1

insertMethod · 0.45

Tested by 1

test_plus_one_v2Method · 0.72