Advanced Python Development Course
Chapter
>
Level
Advanced Functions
Lambda Functions
Objective
Use lambda functions to document some consumables so they can be collected for shipment later.
Some of the fruit and berries in the field are almost ready to be picked so they can be stored, shipped and sold. It would be a good idea to chart down the items that are almost ready so you have an estimate of how many products from this field are being packed and shipped.
In order to optimize your code, you can use lambda functions, this is another way to create custom functions, but it’s quicker when the functions you’re creating are simpler.
# Normal Custom Function
def calculate(x , y , z) :
return( (x+y+z) *2 )
# Lambda Function
calculate = lambda x,y,z : (x + y + z) * 2
In the above example you have two functions, a custom function and a lambda function. While they are written different, they are functionally the same, the lambda function is just easier to use and can be set up quickly on the fly in the middle of the code. That said, the normal method defining functions is still very much useful for complex code sequences.
There are two (2) types of items that can be categorized in the field: "fruits" and "berries". Of these two (2) types, there are three (3) fruits: apples , pears and oranges ; and there are three (3) berries: red , blue and purple .
First set up the lambda function illustrated above named calculate , this will allow us to get the number of items predicted for the season by adding three items, and multiplying them by two (2). Create another lambda function named estimate in order to estimate the items that will be collected, as a custom speak function. Lastly set up a lambda function named total which adds two values together and subtracts a third value named loss.
# Calculate items calculate = lambda x,y,z : (x + y + z) * 2 # Estimate items estimate = lambda number , name : player.speak( "I estimate %d %s this season" % (number,name) ) # Total all items total = lambda num1, num2, loss : ( (num1 + num2) - loss)
Create variables for each of the three (3) fruits, and add the number of how may of each are in the field. Walk to the dark X mark and use the lambda function calculate() with all three fruit variables, and store the value in a custom variable named fruit_number , like this: fruit_number = calculate(orange,pear,apple) . Follow this up by using the estimate() lambda function using fruit_number as the number, and "fruits" as the name, like this: estimate(fruit_number, "fruits" ) .
Once you’re done with the fruit, walk to the light X mark and do the same with the berries. Set up variables for the three (3) berries with the amount of bushes you count on the field, then set up the lambda function the same way you did with the fruit, like this: berry_number = calculate(red,blue,purple) . Use the estimate lambda function with berry_number and "berries" as arguments.
There is a constant named doc that holds last months estimates for lost product. Move on to the storehouse, walk towards the gold X mark and use the speak() function with the constant to read a document on the table so you can determine last month’s numbers.
Once last month’s number’s are known to you, walk to the dark X mark over the purple carpet, face the table so you may determine totals for this month. Create an variable named season to calculate the season’s totals, use the fruit_number and berry_number as the numbers, and use the number you determined from doc as the loss argument, like this: season = total(berry_number,fruit_number, number found in doc constant)
Follow this up by using the estimate() lambda function using the season variable as the number argument and "products" as the name argument in order to complete the level.