Last active
September 1, 2019 14:45
-
-
Save drum-grammer/2895a22113fe20df35983789431dbf3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import sys | |
from itertools import zip_longest | |
""" | |
>> int 형의 최대값 알아보기 | |
t1 = sys.maxsize | |
t2 = sys.maxsize + 1 | |
print(f"t1: {t1}, type: {type(t1)}") | |
print(f"t2: {t2}, type: {type(t2)}") | |
>> 출력 | |
t1: 9223372036854775807, type: <class 'int'> | |
t2: 9223372036854775808, type: <class 'int'> | |
>> 분석 | |
위 테스트로 알 수 있듯이, 파이썬3에서는 int형의 범위를 넘아가도 | |
type int로 취급함. | |
문제의 의도를 고려하여, 18자리씩 잘라서 계산함. | |
""" | |
################################################################# | |
# part 1 : functions & variables definition # | |
################################################################# | |
max_length = 17 | |
def cutter(data, length): | |
if len(data) > length: | |
zero_p = len(data)%max_length | |
data = data.zfill(len(data) +(length - zero_p)) | |
listed_data = [data[i:i+length] for i in range(0, len(data), length)] | |
listed_data = list(map(str, listed_data)) | |
else: | |
listed_data = [data] | |
return listed_data | |
def str2int_converter(str_data): | |
listed_data = list(str_data) | |
listed_data.reverse() | |
str2int_map = { | |
"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, | |
"6": 6, "7": 7, "8": 8, "9": 9, "0": 0 | |
} | |
int_data = 0 | |
n = 0 | |
for data in listed_data: | |
int_data += str2int_map[data] * pow(10, n) | |
n += 1 | |
return int_data | |
################################################################# | |
# part 2 : data input # | |
################################################################# | |
print("Please input first number:") | |
x = input() | |
print("Please input second number:") | |
y = input() | |
################################################################# | |
# part 3 : calculation # | |
################################################################# | |
# x와 y의 길이 맞추기 | |
if len(x) > len(y): | |
y = y.zfill(len(x)) | |
else: | |
x = x.zfill(len(y)) | |
# x와 y를 int 최대 길이로 쪼개어 리스트로 만들기 | |
x_list = cutter(x, max_length) | |
y_list = cutter(y, max_length) | |
# 결과 초기화 | |
res = '' | |
# 쪼갠 리스트를 자리수 별로 더하여 붙이기 | |
for x, y in zip_longest(x_list, y_list, fillvalue = "0"): | |
int_x = str2int_converter(x) | |
int_y = str2int_converter(y) | |
two_sum = int_x + int_y | |
str_two_sum = str(two_sum) | |
str_two_sum = str_two_sum.zfill(max_length) | |
res = res + str_two_sum | |
res = res.lstrip("0") | |
################################################################# | |
# part 4 : print out result # | |
################################################################# | |
print(f"result: {res} type:{type(res)}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment