Uploaded by lourgesgerges

Problem session

advertisement
Problem 1
Write a Python function to insert an element in a given list after
every nth position.
Sample output
Original list:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Insert a in the said list after 3 nd element:
[1, 2, 3, 'a', 4, 5, 6, 'a', 7, 8, 9, 'a', 0]
Problem 2
Write a Python function to find the specified number of largest
products from two given lists, multiplying an element from each
list
Original lists:
[1, 2, 3, 4, 5, 6]
[3, 6, 8, 9, 10, 6]
3 Number of largest products from the said two lists:
[60, 54, 50]
Problem 3
Write a Python function that recursively reverses an input string
Hint: in the recursive case, if we move through the string from left
to right, one character at the time, we can simply extract the
add it to the end of whatever the result would be.
So for the first letter in “Hello world”, we would just remove it from
the whole function with just “ello world”, then add the “H” to the
returned at the end.
Problem 4
• Given a dictionary d whose key values are lists. Write a Python program that
transforms the dictionary d by sorting the lists. Example for the dictionary:
Problem 5
Sort a list of tuples by 2nd item
Problem 6
Try to do the followings:
•Add a key to inventory called 'pocket'.
•Set the value of 'pocket' to be a list consisting of the strings 'seashell',
'strange berry', and 'lint’.
•Sort the items in the list stored under the 'backpack' key.
•Then remove 'dagger' from the list of items stored under the 'backpack' key.
•Add 50 to the number stored under the 'gold' key.
Problem 7
•Define a function compute_bill that takes
one argument food as input. In the function, create
a variable total with an initial value of zero.
•For each item in the food list, add the price of
that item to total. Finally, return the total.
•While you loop through each item of food, only
add the price of the item to total if the item's stock
count is greater than zero.
•If the item is in stock and after you add the price
to the total, subtract one from the item's stock
count.
•Define a function compute_bill that takes one argument food as input. In the function, create a variable total with an initial value of
zero.For each item in the food list, add the price of that item to total. Finally, return the total. Ignore whether or not the item you're
billing for is in stock.Note that your function should work for any food list.
•Make the following changes to your compute_bill function:
•While you loop through each item of food, only add the price of the item to total if the item's stock count is greater than zero.
•If the item is in stock and after you add the price to the total, subtract one from the item's stock count.
Download