Exercise Lambda functions

Imagine an accounting routine used in a book shop. It works on a list with sublists, which look like this:

Order Number Book Title and Author Quantity Price per Item
34587 Learning Python, Mark Lutz 4 40.95
98762 Programming Python, Mark Lutz 5 56.80
77226 Head First Python, Paul Barry 3 32.95
88112 Einführung in Python3, Bernd Klein 3 24.99
  1. In the Python shell, write a Python program that computes the sum of all products of the price per items and the quantity (price per item x quantity). To enter the Python shell, open the terminal and type python. Use map and reduce function to do so.
    You can write the list with sublists like the following in the Python shell:

    orders = [ ["34587", "Learning Python, Mark Lutz", 4, 40.95],
    ["98762", "Programming Python, Mark Lutz", 5, 56.80],
    ["77226", "Head First Python, Paul Barry", 3, 32.95],
    ["88112", "Einfuhrung in Python3, Bernd Klein", 3, 24.99]]
    

    The result should be: 624.62

    Hints:
    • The input of a reduce() can be the output of a map(), but this is not mandatory.

  2. Write a Python program, which returns a list with 2-tuples. Each tuple consists of the order number and the product of the price per items and the quantity. The product should be increased by $10 if the value of the order is less than $100.00. Write a Python program using lambda and map.

    The output of your program should look like this:

    [('34587', 163.8), ('98762', 284.0), ('77226', 108.85000000000001), ('88112', 84.97)]
    

    Hints:

    • You can write an IF … ELSE condition in a lambda function.
    • The input of a map() can be the output of another map().

results matching ""

    No results matching ""