Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def ambiguousCoordinates(self, s):
- def possibleDecs(s):
- if len(s) == 1:
- return s
- if s[0] == "0": # If we start with a 0, the only possible number is 0.XXXXX (cases with all 0s shouldn't be passed to this, hence no checks here for that)
- return [f"{s[:1]}.{s[1:]}"]
- if s[-1] == "0":
- return [s]
- res = [s]
- for dotI in range(1, len(s)):
- res.append(f"{s[:dotI]}.{s[dotI:]}")
- return res
- res = []
- digits = s[1:-1]
- if sum(int(i) for i in digits) == 0 and len(digits) > 2: return []
- splitAfter = 0
- while splitAfter < len(digits) - 1: # We split the coordinates to a possible (x, y) and then find all the possible ways of inserting a dot in x and in y.
- # For cases with multiple leading 0s. We only split it as (0, 00XXXXX) once, then the next split would be (000X, XXXX). Since spliting it such that x = 00 is illegal.
- if splitAfter == 1 and digits[0] == digits[1] == "0":
- while digits[splitAfter] == "0": # We only continue splitting after the first non-zero (0000325040 -> 00003 | 25040)
- splitAfter += 1
- continue # We use continue just in case splitAfter now ==(len(digits)-1), then we should not continue the while loop
- x = digits[:splitAfter+1]
- y = digits[splitAfter+1:]
- if sum(int(i) for i in y) == 0: # Implies y is all 0s, the only number left is if y is a sole 0. So we skip to split at the end (XXXXX0000 | 0)
- x = digits[:-1]
- y = digits[-1:]
- splitAfter = len(digits)
- possibleXs = possibleDecs(x)
- possibleYs = possibleDecs(y)
- for someX in possibleXs:
- for someY in possibleYs:
- res.append(f"({someX}, {someY})")
- splitAfter += 1
- return res
- print(Solution().ambiguousCoordinates("(002)"))
Add Comment
Please, Sign In to add comment