Institute of Computer Science
  1. Courses
  2. 2018/19 fall
  3. Computer Programming (Narva College) (SVNC.00.054)
ET
Log in

Computer Programming (Narva College) 2018/19 fall

  • Home
  • Grading
  • Links

During session 12

slides12.pdf

Exercises

NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Take your time and complete the 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 above. (If the program does not work with the suit symbols, you can replace the suits with their names – 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 the inventory of one player. The program should read the inventories from the file and find two players who are the most similar in terms of items of inventory (it does not matter how many of each item the players have).

Hint:

  1. Read file and compose a list of sets, where every set contains the items of one player.
  2. Find two sets with the largest intersection.

Keep the indices of most suitable players as tuple.

3. Fantasy game inventory v.4

Upgrade the previous program to list the items from all inventories only once. Which data structure would suit best?

4. Fantasy game inventory v.5

Upgrade 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 are no such items).

5. Blocks of stocks

A block of stocks has a number of attributes, including purchase date, purchase price, number of shares, and ticker symbol. We can record these pieces of information in a tuple for each block of stock and perform a number of simple operations on the blocks.

Imagine that we have the following portfolio.

Purchase DatePurchase PriceSharesSymbolCurrent Price
26 Nov 201843.5025CAT92.45
26 Nov 201842.8050DD51.19
26 Nov 201842.1075EK34.87
26 Nov 201837.58100GM37.58

We can represent each block of stocks as a 5-tuple which contains purchase date, purchase price, number of shares, ticker symbol and current price, and put all these tuples into a list.

portfolio = [
    ("26-Nov-2018", 43.50, 25, 'CAT', 92.45),
    ("26-Nov-2018", 42.80, 50, 'DD', 51.19),
    ("26-Nov-2018", 42.10, 75, 'EK', 34.87),
    ("26-Nov-2018", 37.58, 100, 'GM', 37.58)
]

Write a function that examines each tuple, multiplies the number of shares by the purchase price, adds them together to determine the total purchase price of the portfolio, and returns that value. If printed, it should look like this:

 The total purchase price of this portfolio is: 10143.0

Write the second function that examines each tuple, multiplies the number of 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 that value. If printed, it should look like this:

 The total gain from this portfolio is: 1101.0

or

 The total loss from this portfolio is: 135.0
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment