python/CodeChallenge_02(Medium).py

34 lines
865 B
Python

'''
The ternary numeric system is commonly used in Codeland, and to represent ternary numbers, the Tick alphabet is employed. In this system, the digit 0
is represented by ., 1 by -., and 2 by --. Your task is to decode a Tick-encoded string and determine the corresponding ternary number.
Example 1
Input:
.-.--
Output:
012
'''
class Solution(object):
def solve(self, code:str)->str:
tick_map = {'.': '0', '-.': '1', '--': '2'}
i = 0
result = ''
while i < len(code):
if code[i] == '.':
result += '0'
i += 1
elif code[i:i+2] == '-.':
result += '1'
i += 2
elif code[i:i+2] == '--':
result += '2'
i += 2
return result
# Usage
s = Solution()
print(s.solve("-.--.-.--"))