La foret rouge
Published on

[PS] Tuples

Authors
  • avatar
    Name
    신주용

Programmers: Tuples

Conditions

  • The length of s is 5...1,000,000.
  • No number starts with zero.
  • s is a String containing a set representing a tuple without overlapping elements.
  • The elements of the tuple expresse by s are 1...100,000.
  • Only given as input if the returning array length is 1...500.

Strategy

  • s is a string. Split the string and changes it to the numerical array.
  • Each set is in a different order, but the order is important for the result.
  • To take hint from I/O Example 3, "{{20,111},{111}}" should be [111, 20]. Find {111} first, add it to the result tuple, and then check {20,111}. At this time, 111 is already in the result tuple, so you only need to add 20.
  • Conversion process is required because results must be output in integer array.
def solution(s):
    answer = []

    s1 = s.lstrip('{').rstrip('}').split('},{')
    s2 = sorted(s1, key=lambda x:len(x))

    for each in s2:
        s3 = each.split(',')
        for ch in s3:
            if ch not in answer:
                answer.append(ch)

    return [int(i) for i in answer]

Remember

  • There be another way to solve without nested for statement... I need to think more.