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

Use Python 3, please type. Write a Python 3 program which is an arithmetic quiz for...
Use Python 3, please type. Write a Python 3 program which is an arithmetic quiz for children. The program asks the user to calculate the multiplication of two numbers generated at random in your code. The numbers should be in the range 1 through 10. User input is shown in bold black font in the example below. You will need to use random numbers. You should type the recommended comments at the top of your code and include three test...
Describe the characteristics of situations when generators are a good solution? python
Describe the characteristics of situations when generators are a good solution? python
This is a machine learning question. Please use python and google colab format. PLEASE USE BASIC...
This is a machine learning question. Please use python and google colab format. PLEASE USE BASIC MACHINE LEARNING CODES. Using the Kaggle diamonds dataset, construct a KNN estimator to predict diamond prices. Choose an appropriate K value and predict the price of a diamond with the following parameters: "carat' : 0.32, 'cut' : Ideal, 'color' : E, 'clarity' : IF, 'depth' : 60.7, 'table' : 58.0, 'x' : 4.46, 'y' : 4.48,  'z': 2.71". Please change the cut, color and clarity...
When is it a good idea to use flash chromatography?
When is it a good idea to use flash chromatography?
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 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?
Chapter 6: Use a list to store the players Update the program in python so that...
Chapter 6: Use a list to store the players Update the program in python so that it allows you to store the players for the starting lineup. This should include the player’s name, position, at bats, and hits. In addition, the program should calculate the player’s batting average from at bats and hits. Console ================================================================ Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add player 3 – Remove player 4 – Move player 5 – Edit player...
Chapter 6: Use a list to store the players In Python, Update the program so that...
Chapter 6: Use a list to store the players In Python, Update the program so that it allows you to store the players for the starting lineup. This should include the player’s name, position, at bats, and hits. In addition, the program should calculate the player’s batting average from at bats and hits. Console ================================================================ Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add player 3 – Remove player 4 – Move player 5 – Edit player...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT