Created
September 21, 2020 11:41
-
-
Save DarthJahus/da5443e560248f53f12142d645287d72 to your computer and use it in GitHub Desktop.
Solution to 16+18=214 at Codewars (Python)
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
# Jahus | |
# 2020-09-19 | |
# Passes tests at Codewars / 16+18=214 | |
# https://www.codewars.com/kata/5effa412233ac3002a9e471d/train/python | |
def decompose(num): | |
if num != 0: | |
_vec = [] | |
while num > 0: | |
_vec.append(int(num % 10)) | |
num = (num - (num % 10))/10 | |
return _vec | |
else: | |
return [0] | |
def add_vec(vec1, vec2): | |
if len(vec1) > len(vec2): | |
_vec_g = vec1 | |
_vec_p = vec2 | |
else: | |
_vec_g = vec2 | |
_vec_p = vec1 | |
for i in range(len(_vec_p)): | |
_vec_g[i] = vec2[i] + vec1[i] | |
return _vec_g | |
def add(num1, num2): | |
_a = add_vec(decompose(num1), decompose(num2))[::-1] | |
_b = [] | |
for e in _a: | |
_b.append(str(e)) | |
return int(''.join(_b)) | |
if __name__ == "__main__": | |
assert add(2,11) == 13 | |
assert add(0,1) == 1 | |
assert add(0,0) == 0 | |
assert add(16,18) == 214 | |
assert add(26,39) == 515 | |
assert add(122,81) == 1103 | |
print("Passed all tests") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment