leetcode_gym/_code/math/int_to_string.py
2026-08-25 19:14:07 -06:00

40 lines
649 B
Python

'''
EPI; 6.1
'''
def main():
# the input
x = -123
s = []
# deal with negative case
is_neg = False
if x < 0:
is_neg = True
x = abs(x)
# deal with the happy case
while True:
s.append( chr( ord('0') + x%10 ) ) # ord() returns unicode of digit, chr() returns char of unicode
x//=10
if x==0:
break
print (s)
print ('-' + ''.join(reversed(s)) if is_neg else reversed(s)) # reversed() returns an iterator, ''.join() concontenates iterators
# print ('-' if is_neg else '') + ''.join(reversed(s))
if __name__ == "__main__":
main()