Question

In: Computer Science

Use Python: Susie is learning arithmetic, but she’s not so good at it yet. When the...

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')) + '\"')

Solutions

Expert Solution

"""

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:


Related Solutions

write code in python and test Conversation with an AI ChatBot Learning Outcomes: Use the print()...
write code in python and test Conversation with an AI ChatBot Learning Outcomes: Use the print() function to output a variety of data Use the input() function to ask for multiple data types Use basic arithmetic operations in a program Use string methods and operations Use if/else if/else statements to determine the flow of the program Comment your code Debug your code Test your code Scenario A new marketing company is launching an Artificial Intelligence (AI) ChatBot for their website...
When is it a good idea to use flash chromatography?
When is it a good idea to use flash chromatography?
When doing Machine Learning and Deep Learning (AI) research which language is better to use Java...
When doing Machine Learning and Deep Learning (AI) research which language is better to use Java or Python? When would you use Java and when would you use Python?
Scenario Katherine Potter knew a good thing when she saw it. At least, it seemed so...
Scenario Katherine Potter knew a good thing when she saw it. At least, it seemed so at first. She was traveling in Italy when she spotted pottery shops that made beautiful products ranging from ashtrays to lamps. Some of the pottery was stunning in design. Katherine began importing the products to the United States, and sales took off. Customers immediately realized the quality of the items and were willing to pay top price. Katherine decided to keep prices moderate to...
***PYTHON PLEASE*** I'd also like to use decimal to two places when calculating the money $$...
***PYTHON PLEASE*** I'd also like to use decimal to two places when calculating the money $$ values for budget and fee areas. Thank you! -------------------------------------------------------- Create a Park class. Include the following seven data members: Name of Park Location Type of Park (National, State or Local) Fee Number of Employees Number of visitors reported for the past 12 months Annual Budget Write your __init__ class In addition write four separate instance methods that: Return a string containing the name of...
IN PYTHON When you use an automated teller machine (ATM) with your bank card, you need...
IN PYTHON When you use an automated teller machine (ATM) with your bank card, you need to use a personal identification number (PIN) to access your account. If a user fails more than three times when entering the PIN, the machine will block the card. Assume that the user’s PIN is “1234” and write a program that asks the user for the PIN no more than three times, and does the following: •If the user enters the right number, print...
Please complete in Python and neatly explain and format code. Use snake case style when defining...
Please complete in Python and neatly explain and format code. Use snake case style when defining variables. Write a program named wordhistogram.py which takes one file as an argument. The file is an plain text file(make your own) which shall be analyzed by the program. Upon completing the analysis, the program shall output a report detailing the shortest word(s), the longest word(s), the most frequently used word(s), and a histogram of all the words used in the input file. If...
Please complete in Python and neatly explain and format code. Use snake case style when defining...
Please complete in Python and neatly explain and format code. Use snake case style when defining variables. Write a program named wordhistogram.py which takes one file as an argument. The file is an plain text file(make your own) which shall be analyzed by the program. Upon completing the analysis, the program shall output a report detailing the shortest word(s), the longest word(s), the most frequently used word(s), and a histogram of all the words used in the input file. If...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT