python/CodeChallenge_13.py

25 lines
1.2 KiB
Python

'''
Jane, a budding mathematician and a second-grade student, is learning how to perform addition. Her teacher wrote down a sum of several numbers on the board, and the
students were asked to calculate the total. To keep things simple, the sum only includes the numbers 1, 2, and 3. However, Jane is still getting the hang of addition,
so she can only calculate the sum if the numbers are arranged in non-decreasing order (i.e., each number is greater than or equal to the one before it). For example, she cannot compute a sum like 2+1+3+2, but she can handle 1+2+2+3.
Your task is to rearrange the numbers in the given sum so that Sophia can compute it.
Example 1
Input:
1+3+2+1
Output:
1+1+2+3
'''
class Solution(object):
def solve(self, s:str) -> str:
print(f"s: {s}")
# Write your code here.
input_numbers = s.split('+')
print(f"input_numbers: {input_numbers}")
input_numbers = [int(num) for num in input_numbers]
input_numbers.sort()
return '+'.join(map(str, input_numbers)) # Join them back with '+' to form the output string
s = Solution()
print(s.solve("1+3+2+1"))
print(s.solve("5+3+11+5+7")) # Output: "1+1+2+3"