Skip to content

Instantly share code, notes, and snippets.

@YUChoe
Created August 23, 2019 06:22
Show Gist options
  • Save YUChoe/ce57d71abb2cd58c775fa1366b91d7a2 to your computer and use it in GitHub Desktop.
Save YUChoe/ce57d71abb2cd58c775fa1366b91d7a2 to your computer and use it in GitHub Desktop.
MISO coding-test-1
# Test 1
# [email protected]
#
# 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.
#
# Expected result:
# $ python3.6 test1.py 123123111242342342003004400442255523 225244004004004234234234412312399999
# 123123111242342342003004400442255523 + 225244004004004234234234412312399999 = 348367115246346576237238812754655522
import sys
def print_usage_and_exit():
print("Usage:")
print("{} [non-negative integer] [non-negative integer]".format(sys.argv[0]))
sys.exit(1)
def conv_str_to_non_negative_int(arg_str):
s = arg_str.strip()
if '.' in s in ',' or '-' in s:
print_usage_and_exit()
int_value = 0
digit_count = 0
for n in s[::-1]:
base10 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].index(n)
int_value += (base10 * (10 ** digit_count))
digit_count += 1
return int_value
if __name__ == '__main__':
# two numbers as arguments test
if len(sys.argv) != 3:
print_usage_and_exit()
# integer test
int_1 = conv_str_to_non_negative_int(sys.argv[1])
int_2 = conv_str_to_non_negative_int(sys.argv[2])
print('{} + {} = {}'.format(int_1, int_2, (int_1 + int_2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment