philosyang.com

816. Ambiguous Coordinates | 1708

What another weird question…

My initial thoughts:

 1class Solution:
 2    def ambiguousCoordinates(self, s: str) -> List[str]:
 3        # intuition:
 4        # enum problem...
 5        # there must be one and only one comma, decimal 0/1/2
 6        # common example: 123
 7        # comma asap, decimal asap: (1, 2.3) (1, 23) (1.2, 3) (12, 3)
 8        # decimal can't have ending zeros
 9        # number can't start with zero unless immediately a decimal
10
11        def normalize(s: str) -> str:
12            # return "" if illegal combination
13            if (
14                s.startswith(",")
15                or s.endswith(",")
16                or s.count(",") != 1
17                or s.count(".") > 2
18            ):
19                return ""
20            comma_location = s.find(",")
21            s0, s1 = s[:comma_location], s[comma_location + 1 :]
22            print(s0, s1)
23
24            split_list = [s0, s1]
25            combined = []
26            for split in split_list:
27                if "." in split:
28                    if split.endswith("0"):
29                        return ""
30                if split.startswith("0"):
31                    if not split.startswith("0."):
32                        if split != "0":
33                            return ""
34
35                combined.append(split)
36
37            return "(" + combined[0].strip() + ", " + combined[1].strip() + ")"
38
39        print(normalize("1,23"))

I should have planned this sooner but, it is not very easy to code an enum that goes through all possible ‘,’ and ‘.’ combinations. Therefore I took a hint: Separate the problems: comma vs decimals - First, fix the comma position, then handle decimals independently on each side.

 1class Solution:
 2    def ambiguousCoordinates(self, s: str) -> List[str]:
 3        # intuition after hint:
 4        # comma vs decimals - First, fix the comma position, then handle decimals independently on each side.
 5        digits = s[1:-1]
 6        result = []
 7
 8        # given s: str, generate all valid numeric strings
 9        def generateValidCombo(s: str) -> List[str]:
10            result = []
11            n = len(s)
12
13            if n == 1:
14                return [s]
15            if s.startswith("0") and s.endswith("0"):
16                return []
17            if s.startswith("0"):
18                return ["0." + s[1:]]
19            if s.endswith("0"):
20                return [s]
21
22            result.append(s)
23            for i in range(1, n):
24                result.append(s[:i] + "." + s[i:])
25
26            return result
27
28        for i in range(1, len(digits)):
29            l = digits[:i]
30            r = digits[i:]
31
32            # now the problem becomes: generate all valid numeric strings
33            lc = generateValidCombo(l)
34            rc = generateValidCombo(r)
35
36            for a in lc:
37                for b in rc:
38                    result.append("(" + a + ", " + b + ")")
39
40        return result

#String #Enumeration #Python