Last active
September 19, 2020 07:58
-
-
Save muminoff/7ca0e942158c75a54ad34d60e12635a6 to your computer and use it in GitHub Desktop.
Test #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.8 | |
# | |
# Test #1 | |
# smuminov@gmail.com | |
# | |
# Write a command-line program that prints out the sum of two non-negative integers as input arguments. | |
# You must not use any built-in BigInteger library or convert the inputs to integer directly. | |
import sys | |
def calc_sum(num1: str, num2: str) -> str: | |
result = "" | |
if len(num1) > len(num2): | |
num1, num2 = num2, num1 | |
diff = len(num2) - len(num1) | |
num1 = "0" * diff + num1 | |
carry = 0 | |
for i, j in zip(num1[::-1], num2[::-1]): | |
cur = ord(i) + ord(j) - 2 * ord("0") + carry | |
carry = cur // 10 | |
result = str(cur % 10) + result | |
return "1" + result if carry else result | |
def main(): | |
if len(sys.argv) == 3: | |
n1 = sys.argv[1] | |
n2 = sys.argv[2] | |
if n1 and n2: | |
print(calc_sum(n1, n2)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment