-
Notifications
You must be signed in to change notification settings - Fork 55
/
Python.py
32 lines (27 loc) · 982 Bytes
/
Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#*********************************************#
#* *#
#* CodinGame.com Solutions by pathosDev *#
#* *#
#* Puzzle: Balanced ternary computer: encode *#
#* Difficulty: Easy *#
#* Date solved: 09.11.2018 *#
#* *#
#*********************************************#
#Generates the ternary representation of an integer.
def convertToTernary(number):
if number < 0:
BT = convertToTernary(-number)
return ''.join(['1' if c == 'T' else ('T' if c == '1' else '0') for c in BT])
if number == 0:
return ''
if number % 3 == 2:
return convertToTernary((number + 1) // 3) + 'T';
else:
return convertToTernary(number // 3) + str(number % 3)
#Read input.
N = int(input())
if N == 0:
print('0')
else:
#Get ternary representation.
print(convertToTernary(N))