UNB/ CS/ David Bremner/ teaching/ cs2613/ labs/ Lab 16

# Getting Started

• Make a directory `~/cs2613/labs/L16`

# Data types

Time
25 minutes
Activity
Convert REPL session to script with tests
• Exercise 2.1

• Exercise 2.2. Instead of using the REPL for this one, write a script so that the following two tests pass (you may need to insert code between the two test definitions, but try not to modify the tests).

```def test_cost():
assert cost == pytest.approx(3220.000,abs=0.00000001)

def test_d():
assert d == {'name': 'AA', 'shares': 75, 'price':32.2, 'date': (6, 11, 2007),
'account': 12345}
```

# Containers: `read_portfolio`

Time
25 minutes
Activity
Write function based on template
• Exercise 2.4. Embed the following tests in `report.py`, and make sure they pass.
```def test_read():
assert portfolio == \
[('AA', 100, 32.2), ('IBM', 50, 91.1),
('CAT', 150, 83.44), ('MSFT', 200, 51.23),
('GE', 95, 40.37), ('MSFT', 50, 65.1), ('IBM', 100, 70.44)]

def test_total():
total = 0.0
for name, shares, price in portfolio:
total += shares*price
assert total == pytest.approx(44671.15,abs=0.001)
```
• Exercise 2.5. Call this version `report2.py` Embed the following tests in `report2.py`, and make sure they pass.
```def test_read():
assert portfolio == \
[{'name': 'AA', 'shares': 100, 'price': 32.2}, {'name': 'IBM', 'shares': 50, 'price': 91.1},
{'name': 'CAT', 'shares': 150, 'price': 83.44}, {'name': 'MSFT', 'shares': 200, 'price': 51.23},
{'name': 'GE', 'shares': 95, 'price': 40.37}, {'name': 'MSFT', 'shares': 50, 'price': 65.1},
{'name': 'IBM', 'shares': 100, 'price': 70.44}]

def test_total():

total = 0.0
for s in portfolio:
total += s['shares']*s['price']
assert total == pytest.approx(44671.15,abs=0.001)
```

# Sequences

Time
25 minutes
Activity
Convert REPL session to tests
```def test_items():
assert prices.items() == \
[('GOOG', 490.1), ('AA', 23.45), ('IBM', 91.1), ('MSFT', 34.23)]
```
• Question for your journal: what is `list()` being used for in this section?
• Embed the following tests and make sure they pass
```def test_zip():
assert pricelist == \
[(490.1, 'GOOG'), (23.45, 'AA'), (91.1, 'IBM'), (34.23, 'MSFT')]

def test_min_max():
assert min(pricelist) == (23.45, 'AA')
assert max(pricelist) == (490.1, 'GOOG')
```
• Question for your journal: What is this kind of ordering of tuples usually called?
• Add a test for last part of 2.17 about different input length lists

# List Comprehensions

Time
25 minutes
Activity
Write tests based on REPL session
```import pytest