In: Computer Science
Use Python:
Susie is learning arithmetic, but she’s not so good at it yet. When the teacher writes down the sum of several numbers together, like 1 + 3 + 2 + 1, Susie has to rearrange the numbers into ascending order before adding them. For example, she would rearrange the previous example to 1 + 1 + 2 + 3 before doing the math. Complete the function math help(problem) that takes a string representing a math problem and rearranges the digits so that Susie can compute the sum. The function will return the rearranged sum as a string. You may assume that only digits 1, 2 and 3 appear in the argument string problem. You may also assume that at least one + symbol appears in problem and that problem is always formatted properly.
Example:
| Function Call | Return Value |
| math help(’3+2+1+1’) | ’1+1+2+3’ |
| math help(’1+2+3’) | ’1+2+3’ |
| math help(’2+2’) | ’2+2’ |
| math help(’1+3+2+3+2+1+3+2+3+1+2’) | ’1+1+1+2+2+2+2+3+3+3+3’ |
----------------
def math_help(text):
# Fill in your code here
return None # Change or replace this line
if __name__ == "__main__":
############### Tests ###############
print('Testing math_help() for \"3+2+1+1\": \"' +
str(math_help('3+2+1+1')) + '\"')
print('Testing math_help() for \"1+2+3\": \"' +
str(math_help('1+2+3')) + '\"')
print('Testing math_help() for \"2+2+2\": \"' +
str(math_help('2+2+2')) + '\"')
print('Testing math_help() for \"1+3+2+3+2+1+3+2+3+1+2\": \"' +
str(math_help('1+3+2+3+2+1+3+2+3+1+2')) + '\"')
"""
Author:
Date :27/2/17
File Name:math_help.py
Description:
"""
def math_help(text):
"""
:param text:
:return:
"""
# Split the text by + delimiter
number_list = text.split("+")
# Sort the list because here only single digit number so we can use just sort
number_list.sort()
# Join the above list with + sign
result = "+".join(number_list)
return result
if __name__ == "__main__":
############### Tests ###############
print('Testing math_help() for \"3+2+1+1\": \"' +
str(math_help('3+2+1+1')) + '\"')
print('Testing math_help() for \"1+2+3\": \"' +
str(math_help('1+2+3')) + '\"')
print('Testing math_help() for \"2+2+2\": \"' +
str(math_help('2+2+2')) + '\"')
print('Testing math_help() for \"1+3+2+3+2+1+3+2+3+1+2\": \"' +
str(math_help('1+3+2+3+2+1+3+2+3+1+2')) + '\"')
output:
