StudentSeng

WIP

May 13th, 2021 (edited)
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. class Solution:
  2.     def ambiguousCoordinates(self, s):
  3.         def possibleDecs(s):
  4.             if len(s) == 1:
  5.                 return s
  6.  
  7.             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)
  8.                 return [f"{s[:1]}.{s[1:]}"]
  9.  
  10.             if s[-1] == "0":
  11.                 return [s]
  12.  
  13.             res = [s]
  14.             for dotI in range(1, len(s)):
  15.                 res.append(f"{s[:dotI]}.{s[dotI:]}")
  16.             return res
  17.  
  18.         res = []
  19.         digits = s[1:-1]
  20.  
  21.         if sum(int(i) for i in digits) == 0 and len(digits) > 2: return []
  22.  
  23.         splitAfter = 0
  24.         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.
  25.             # 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.
  26.             if splitAfter == 1 and digits[0] == digits[1] == "0":
  27.                 while digits[splitAfter] == "0":  # We only continue splitting after the first non-zero (0000325040 -> 00003 | 25040)
  28.                     splitAfter += 1
  29.                 continue    # We use continue just in case splitAfter now ==(len(digits)-1), then we should not continue the while loop
  30.  
  31.             x = digits[:splitAfter+1]
  32.             y = digits[splitAfter+1:]
  33.  
  34.             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)
  35.                 x = digits[:-1]
  36.                 y = digits[-1:]
  37.                 splitAfter = len(digits)
  38.  
  39.             possibleXs = possibleDecs(x)
  40.             possibleYs = possibleDecs(y)
  41.  
  42.             for someX in possibleXs:
  43.                 for someY in possibleYs:
  44.                     res.append(f"({someX}, {someY})")
  45.  
  46.             splitAfter += 1
  47.         return res
  48.  
  49.  
  50. print(Solution().ambiguousCoordinates("(002)"))
  51.  
Add Comment
Please, Sign In to add comment