During session 3
Exercises
NB! The practical session exercises are provided in such capacity that more experienced students would also find them interesting. It is not expected of you to get all of them done during one session. However, you are always welcome to complete the unfinished tasks at home!
1. Deck of cards
Write a program that declares two data structures, one for four suits ('♥','♦','♠','♣') and another one for thirteen ranks ('A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3', '2'). Choose the data structures that suit best. The program should compose a set of 52 playing cards - all possible pairs that can be formed by "mating" the elements of two declared data structures (the elements of the set are pairs like ('♥','A'), ('♦','8'), ('♠','2'), and so on). Program should output all playing cards from the set in the following way:
A♥ 8♦ 2♠
You can copy the suit symbols from the text of the exercise. If the program does not work with the suit symbols, replace the suits with names of the suits (clubs, diamonds, hearts, spades). The symbols are not important in this exercise, data structures are.
2. Fantasy game inventory v.3
The next version of the fantasy game inventory deals with several players. Inventories of different players are in the text file (look at the example here). Every line in the file is an inventory of one player. The program should read the inventories from the file and list up the items from all the inventories only once. Which data structure would suit best?
3. Fantasy game inventory v.4
Modify the previous program to show the item(s) that all players have.
Use the following file to check the program - the answer is "gold coin" (in the previous file, there is no such items).
4.* Fantasy game inventory v.5
Modify the previous program to find two players who are the most similar in terms of inventory items (it does not matter how many of each item the players have).
Hint:
- read the data from file and compose a list of sets (every set contains the items of one player)
- find two sets with the largest intersection
- keep the indexes of the most suitable players in a tuple
5. Blocks of stocks
A block of stocks has a number of attributes, including a purchase date, a purchase price, a number of shares, and a ticker symbol. We can record these pieces of information in a tuple for each block of the stock and perform a number of simple operations on the blocks. Imagine that we have the following portfolio.
Purchase Date | Purchase Price | Shares | Symbol | Current Price |
---|---|---|---|---|
25 Nov 2019 | 43.50 | 25 | CAT | 92.45 |
25 Nov 2019 | 42.80 | 50 | DD | 51.19 |
25 Nov 2019 | 42.10 | 75 | EK | 34.87 |
25 Nov 2019 | 37.58 | 100 | GM | 37.58 |
We can represent each block of the stock as a 5-element tuple , and put all the tuples into a list.
portfolio= [ ( "25-Nov-2019", 43.50, 25, 'CAT', 92.45 ), ( "25-Nov-2019", 42.80, 50, 'DD', 51.19 ), ( "25-Nov-2019", 42.10, 75, 'EK', 34.87 ), ( "25-Nov-2019", 37.58, 100, 'GM', 37.58 ) ]
Write a function that calculates the total purchase price of the portfolio, and returns this value (multiply the shares by their purchase price and sum the results). If we print the result of the function call, the output should look like this:
The total purchase price of this portfolio is: 10143.0
Write the second function that examines each tuple, multiplies the shares by the current price and multiplies the shares by the purchase price, subtracts the results, and determines the total profit or loss, and returns this value. If we print the result of the function call, the output should look like this:
The total gain from this portfolio is: 1101.0
or
The total loss from this portfolio is: 135.0
6. Banknotes
Write a function banknotes that takes a number as a parameter which represents an amount of money, and returns a tuple of 6 integers, each of them representing how many banknotes of €50, €20, €10, €5, €2, €1 there are in this amount of money.
Hint: The function should break down the amount starting with the largest banknote, then with the second largest, and so on.
Examples of the function call:
>>> banknotes(3) (0, 0, 0, 0, 1, 1) >>> banknotes(25) (0, 1, 0, 1, 0, 0) >>> banknotes(108) (2, 0, 0, 1, 1, 1) >>> banknotes(1999) (39, 2, 0, 1, 2, 0)