Question

In: Computer Science

please solve by utilizing python Define a function named checkOut which takes two JSON strings, each...

please solve by utilizing python

Define a function named checkOut which takes two JSON strings, each representing a dictionary.
The first has the following structure,

{ "customer" : name , "cart" : [ item1, item2, ... ] }

The second has the following structure,
{ item1 : price1, item2 : price2, ... }


The function must return a JSON string representing the following dictionary:

{ "customer" : name, "amount_due" : x }

where x is the sum of the prices of the items in the customer's cart.

Example:
checkOut('{"customer":"Sally", "cart":["milk","cookies"]}','{"milk":1.79,"cookies":3.99}') must return '{"customer":"Sally", "amount_due":5.78}'

Solutions

Expert Solution

import json


def checkOut(items_json, price_json):

    # Parses JSON string to python dictionary
    items_dictionary = json.loads(items_json)
    price_dictionary = json.loads(price_json)

    amount_due = 0

    # Iterating through the cart of items
    for item in items_dictionary.get("cart"):
        # If price of the item is mentioned
        if item in price_dictionary:
            amount_due += price_dictionary[item]

    # Output dictionary
    output_dictionary = {"customer": items_dictionary["customer"], "amount_due": amount_due}
    # Converting Python dictionary to JSON String
    output_json_string = json.dumps(output_dictionary)

    return output_json_string


# Driver code
if __name__ == "__main__":

    # json.dumps converts Python object into a JSON string
    # Converting Python dictionary JSON string
    items_json_string = json.dumps({"customer": "Sally", "cart": ["milk", "cookies"]})
    price_json_string = json.dumps({"milk": 1.79, "cookies": 3.99})

    print(checkOut(items_json_string, price_json_string))

Related Solutions

Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON...
Define a JavaScript function named showBins which has one parameter which is a JSON blob (JSON encoding). The parameter encodes an array of Numbers. Your function should display the value at index 0 of the array in a text element whose id is "small", display the value at index 1 of the array in a text element whose id is "med", and display the value at index 2 of the array in a text element whose id is "large".
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Define a Python function named matches that has two parameters. Both parameters will be lists of...
Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator. Hint: Since you must use the same index with each...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT