Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


ICS-33: In-Lab Programming Exam #1

 

General Rules

This In-Lab programming exam is worth a total of 100 points. It requires you to define several functions in a  module. I will supply a script for calling these functions and printing their results, so that you can check them visually for correctness; we will use a batch self-check file with similar tests for grading purposes only: you   will not use bsc files. Although these functions are related, you can write and test these functions in any order. None of the functions rely on each other to work correctly.

You will have approximately 105 minutes to work on the exam, after logging in and setting up your computer (I estimate that taking about 5 minutes). Make sure that you read the instructions and example output for each problem carefully, understanding it before starting to write/debug its code: don't rush through this part ofthe   exam. You may write-on/annotate these pages; the last page of this exam will also be blank. Finally, you will  write, test, and debug the functions in the exam.py module provided in a project file.

We will test your functions only for correctness; each test you pass will improve your grade, and functions   that produce no correct results will earn no credit. This means that your functions must define exactly the     parameters specified in the descriptions below and must work on all the example arguments; your functions should also work on any other similar arguments. To aid you writing and debugging these functions

1. Write clear, concise, and simple Python code.

2. Choose good names for local variables.

You do not need to include any comments in your code; but, feel free to add them to aid yourself. We will not grade you on appropriate names, comments, or Python idioms: only on correctness. You may also call extra    print functions in your code or use the Eclipse Debugger to help you understand/debug your functions.

You may import and use any functions in the standard Python library and the goody and prompt modules     (which will be included in the project file). Documentation for Python's standard library and these modules   will be available during the exam. I have written all the standard import statements that I needed to write my solution; feel free to include other imports, or change the form ofthe included imports to be simpler to use.

If you are having problems with the operating system (logging on, accessing the correct folder/files,                 accessing the Python documentation, submitting your work) or Eclipse (starting it, setting it up for Python,      running Python scripts, running the Eclipse debugger, displaying line numbers) please talk to the staff as soon as possible. We are trying to test only your programming ability, so we will help you with these other               activities. But, we cannot help you understand the specifications, nor test your code, no debug your                  programming errors. I have asked the TAs and Tutors to NOT ANSWER ANY QUESTIONS about the     exam itself: read it carefully and look at the test cases for clarification.

You should have a good understanding of the solutions to the problems in Programming Assignment #1

(especially the first 3); you should also have a good understanding of the material on Quiz #1. You should   know how to read files; create, manipulate, and iterate over lists, tuples, sets, and dictionaries (dict and      defaultdict) in 3 ways: by keysvalues, and items; call the functions or methods allanyminmaxsumjoinsplitsortsortedenumeratezip. Note that you can call minmaxsort, and sorted with a key        function (often specified by a simple lambda) that determines how to order the values in the iterable           argument on which these functions compute. You are free to use or avoid various Python language features (e.g., lambdas and comprehensions): do what is easiest for you, because we grade only whether your            functions work correctly.

Before submitting your program, ensure that it runs (has no syntax errors) and ensure there are no strange/unneeded import statements at the top.

Summary:

This exam is worth 100 points. It requires you to write five functions according to the specifications below.     Each problem is graded on correctness only (how many tests it passes); each problem will be worth 20 points.

The module you will download

1. Defines each method with reasonable annotated parameter names (which you can change) and a body that is pass (thus, it returns None and will pass no tests).

2. Includes a script that tests these functions individually on different legal inputs, printing the results, and printing whether or not they are correct; if they are not correct, further information is printed.

The next five sections explain the details of these functions. You should probably try writing/testing each    function as you read these details. Spend about 15-20 minutes on each function; at that point, ifyou are not making good progress toward a solution, move on to work on later functions (which you might find easier) and return ifyou have time. Each problem is graded based on the percentage of tests it passes.

1-5: Data Structure for a Stock Portfolio

Assume that your stock trading company stores a database that is a dictionary (dict) whose keys are client      names (str); associated with each client name is a list of 3-tuples representing the transaction history ofthat  client: in the 3-tuple, the first index is a stock name (str), the second index is the number of shares traded for that transaction (int), and the third index is the price per share in dollars (int for simplicity). The list shows   the order in which the transactions occurred: the first before the second before the third, etc. A simple/small  database can look like

IMPORTANT: When the number of shares traded is positive, it represents buying shares: adding them to     the portfolio. When the number of shares traded is negative, it represents selling shares: removing them from the portfolio. In these transaction lists, no client will sell more shares of a stock than they own (bought earlier and haven't sold yet).

Here, the second item in the dictionary says that Barb bought 20 shares ofIntel for 40 dollars/share; then    she sold 10 shares ofIntel for 45 dollars/share; then she bought 40 shares ofIBM for 30 dollars/share;         finally she sold 10 shares ofIntel for 35 dollars/share. Adding up the purchases and sales, her portfolio now

contains 40 shares ofIBM (she bought it once and never sold any) and 0 shares ofIntel (she bought 20 shares once and sold 10 shares twice).

Of course, dictionaries (and sets) can print their items in any order, because they are not ordered. Also, for  easier reading, I often show how data structures print by using special indentation and multiple lines, while standard printing in Python will display a single line.

1: Details of stocks:

The stocks function takes a Stock Portfolio database (dict) argument and returns a set of all the stock names traded.

So calling this function with the example dictionary in the 1-5: Data Structure for a Stock Portfolio returns

{'Dell', 'Apple', 'Intel', 'IBM'}

Of course, sets can print in any order, because they are not ordered.

2: Details of frequent_traders:

The frequent_traders function takes a Stock Portfolio database (dict) argument and returns a list of client    names (str) in decreasing order based on the number of transactions they made; iftwo clients have the same number of tranactions, they must appear in increasing alphabetical order.

So calling this function with the example dictionary in the 1-5: Data Structure for a Stock Portfolio Database returns a list whose contents are

['Alan', 'Barb', 'Carl', 'Dawn']

Note that Alan makes 4 trades; Barb makes 4 trades; Carl makes 4 trades; and Dawn makes 3 trades. So     AlanBarb, and Carl each make 4 trades (and appear in increasing alphabetical order); Dawn makes trades so appears last. It is only accidental that these names appear in alphbetical order; the actual tests I use for     grading might be different.

3: Details of read_db

The read_db function takes an open-file argument; it returns a dictionary (you may return a dict or          defaultdict, which print differently but compare for equality correctly based on their contents): the           dictionary's keys are strs (client names) whose associated values are lists of 3-tuples for each transaction (stock, shares traded, and price/share).

The input file will consist of some number of lines. Each line contains information about one client (and that client will not appear on any other lines): the name of the client, followed by a sequence of all the trades       (with the 3 pieces of information for each trade):

• The client name is separated from that client's transcations by a colon (:).

• The transactions are separated by a semi-colon (;)

• The stock, shares, and price in each transaction are separated by a comma (,).

For one example, the portfolio1.txt file contains

Carl:Intel,30,40;Dell,20,50;Intel,-10,60;Apple,20,55

Barb:Intel,20,40;Intel,-10,45;IBM,40,30;Intel,-10,35

Alan:Intel,20,10;Dell,10,50;Apple,80,80;Dell,-10,55

Dawn:Apple,40,80;Apple,40,85;Apple,-40,90

Here is what the second line means: Barb is the client name, who had 4 transactions.

1. In the first transaction she bought 20 shares of Intel for 40 dollars per share.

2. In the second transaction she sold 10 shares of Intel for 45 dollars per share.

3. In the third transaction she bought 40 shares ofIBM for 30 dollars per share.

4. In the fourth transaction she sold 10 shares of Intel for 35 dollars per share.

Each line should initialize/update information in the dictionary for the client and all his/her tranactions. The four-line file above returns a dictionary whose contents are:

Of course, dictionaries can print in any order, because they are not ordered, but the transactions should print in the order shown. Note that the numeric quantities (shares and price) must be translated into int values.

If you use/return a defaultdict it will show other information when printed, but its contents (keys and values) will show equivalently (and will test for equality correctly against dicts).

4: Details of traded:

The traded function takes a Stock Portfolio database (dict) argument and returns a dictionary (dict or            defaultdict, which print differently but compare for equality correctly based on their contents) whose keys    are all the stocks that appear in transactions; the value assocated with a stock is a 2-list whose first index is    the number of shares ofthat stock bought and whose second index is the number of shares ofthat stock sold.

So calling this function with the example dictionary in the 1-5: Data Structure for a Stock Portfolio returns a dictionary whose contents are

{'Intel': [70, 30], 'Dell': [30, 10], 'Apple': [180, 40], 'IBM': [40, 0]}

For DellCarl bought 20 shares and Alan bought 10 shares (for 30 total); Alan sold 10 shares. 5: Details of most_active_clients:

The most_active_clients function takes a Stock Portfolio database (dict) argument and returns a list of        2-tuples as a result. Each 2-tuple is a client and the number of shares that client traded (bought or sold) and the list is sorted in decreasing order based on the sum of the number of shares they traded; iftwo clients trade the same number of shares, they must appear in increasing alphabetical order.

So calling this function with the example dictionary in the 1-5: Data Structure for a Stock Portfolio Database returns a list whose contents are

[('Alan', 120), ('Dawn', 120), ('Barb', 80), ('Carl', 80)]

Note that Alan and Dawn trade 120 shares (so appear in alphabetical order); Barb and Carl trade 80 shares (so appear in alphabetical order).

6: Details of summary: Extra Credit: 1 point

The summary function takes a Stock Portfolio database (dict) and a dictionary of current stock prices (whose keys are stocks (str) and whose associated values are current price/share in dollars (int) that stock is worth)     as arguments. It returns a dictionary (you may return a dict or defaultdict, which print differently but              compare for equality correctly based on their contents): the dictionary's keys are strs (client names) whose      associated values are 2-tuples whose

1. first index is a dictionary whose keys are stocks, and whose associated values are the number of shares of that stock that client has (after adding up all the buying/selling transactions: it will always be a non- negative number; remove any stock from this dictionary for which the client owns 0 shares).

2. second index is the amount ofmoney (int dollars) that the stock portfolio is worth: adding up the  number of shares of each stock the client owns, multiplying each by the current price ofthe stock.

Given the following dictionary of current stock prices

{'IBM': 65, 'Intel': 60, 'Dell': 55, 'Apple': 70}

calling this function with the example dictionary in the 1-5: Data Structure for a Stock Portfolio Database returns a dictionary whose contents are

{

    'Alan': ({'Intel': 20, 'Apple': 80}, 6800),

    'Barb': ({'IBM': 40}, 2600),

    'Carl': ({'Intel': 20, 'Dell': 20, 'Apple': 20}, 3700),

    'Dawn': ({'Apple': 40}, 2800)

}

Note that Alan ends up with 20 shares ofIntel (selling at 60 dollars/share = 1,200 dollars) and 80 shares of    Apple (he bought this amount and sold none; selling at 70 dollars/share = 5,600 dollars); he sold all 10 shares ofDell that he bought, so Dell does not appear in the dictionary. So all together his stocks are worth 6,800       dollars.

Of course, dictionaries can print in any order, because they are not ordered, and they print on one line.