CMPUT 175 – Lab 1: Python Warm Up solved

$35.00

Original Work ?
Category: Tags: , You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

Goal: Review the properties and methods associated with Python lists, tuples, and sets.
Review dictionaries, string formatting.
Useful list methods/operators: append(), count(), index(), insert(), pop(), remove(), sort(), in, +
Useful tuple methods/operators: index(), in, +
Useful set methods/operators: add(), discard(), remove(), &, |
Useful dictionary methods: items(), keys(), values()
Useful string methods and operators: format(), lower(), upper(), [:]
There are many more methods, operators, and functions that you can look up in the lecture
slides or in the Python 3 online documentation: https://docs.python.org/3/index.html
Another reference that may be helpful for understanding how to use format() is:
https://www.programiz.com/python-programming/methods/string/format

Exercise 1
Problem: Given a list of integers, follow the steps below to remove all occurrences of the
smallest odd number from it. In the process, gain a deeper understanding of whether a list is
mutable or immutable, and gain practice manipulating lists in Python.
1. Create a new Python file (lab1.py). In that file, create the following list:
numbers = [11, 25, 32, 4, 67, 18, 50, 11, 4, 11]

2. Create an empty list, oddNumbers. Print the identity and contents of this list to the screen.
(Hint: Look up how id(object) works in the Python 3 online documentation.)
3. Write a loop that finds all of the odd integers in numbers, and appends each odd integer to
oddNumbers. (Hint: can you use the modulo operator to determine if an integer is odd?)
Once finished, print the identity and contents of oddNumbers to the screen.
Has the identity of oddNumbers changed? Is a Python list mutable or immutable?

4. Sort oddNumbers in descending (decreasing) order. Once sorted, use the pop method to
remove the smallest and largest odd integers from oddNumbers. (Hint: where are the
smallest and largest numbers in the sorted list?) Print the values of the smallest and largest
numbers to the screen; also print the identity and contents of oddNumbers to the screen.

5. Print the length of numbers to the screen. Remove all occurrences of the smallest odd
number from numbers. (Hint: use the value you found in step 4 above.) Print the new
contents and length of numbers to the screen.

Exercise 1: Sample Output (your object IDs will be different)
The contents of object 55986456 are []
The contents of object 55986456 have been updated to [11, 25, 67, 11, 11]
The smallest odd number, 11, has been removed from the list of odd numbers.
The largest odd number, 67, has been removed from the list of odd numbers.
The contents of object 55986456 have been updated to [25, 11, 11]
There are 10 numbers in the original list.
After removing the smallest odd number, there are 7 numbers in the list:
[25, 32, 4, 67, 18, 50, 4]

Exercise 2
Problem: Follow the steps below to create a program that determines the amount of precipitation
that fell in Edmonton in a user-specified month in 2020. In the process, practice working with
tuples, and gain a deeper understanding of whether a tuple is mutable or immutable.
1. Create the following tuple:
months = (‘JAN’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’, ‘JUN’, ‘JUL’,
‘AUG’, ‘SEP’, ‘OCT’)
Print the identity and contents of this tuple to the screen.
2. Add the tuple (‘NOV’, ‘DEC’) to months. Print the identity and content of months to
the screen.
Has the identity of months changed? Is a Python tuple mutable or immutable?

3. Create the following list, which contains the monthly amounts of precipitation (in mm) that
fell in Edmonton in 2020, ordered by month (i.e. 15.5mm fell in Jan, 24.8mm fell in Dec):
precipitation2020 = [15.5, 12.1, 18.5, 15.6, 10.7, 62.2,
41.4, 58.3, 15.7, 15.3, 24.8]

4. The precipitation amount for July was accidentally left out of the above list. Insert its value
(67.8mm) at index 6. (Hint: use the list’s insert method to do this.) The precipitation
amounts in precipitation2020 should now align with the months in months (i.e. the
data is what we call parallel).

5. Find the index (i.e. position) that ‘MAY’ is at in months. Using the same index number,
look up and print the amount of precipitation that fell in May 2020 to the screen. We can do
this because we’ve manually arranged our data in our tuple and list to be parallel. What
would happen if we sorted the precipitation so that it was in ascending order (don’t
actually do this) – would the parallel relationship between the tuple and list still exist?
Are these parallel containers the best way to form relationship pairs, or is there another
built-in Python data structure that would be better to use?

6. Ask the user to enter the name of a month. If the user’s entry matches one of the strings in
months exactly, print the amount of precipitation that fell in that month to the screen.
Otherwise, tell the user that you cannot find information for the month entered.

Exercise 2: Sample Output
The contents of object 70948160 are (‘JAN’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’,
‘JUN’, ‘JUL’, ‘AUG’, ‘SEP’, ‘OCT’)
The contents of object 62484192 are (‘JAN’, ‘FEB’, ‘MAR’, ‘APR’, ‘MAY’,
‘JUN’, ‘JUL’, ‘AUG’, ‘SEP’, ‘OCT’, ‘NOV’, ‘DEC’)
10.7mm fell in May 2020.
Please enter a month: JUL
67.8mm fell in JUL 2020.

Exercise 3
Problem: Pets R Us has asked you to create a program that helps customers with their purchase
decision. Follow the steps below to compare the store’s inventory against the pets which a
customer wishes to buy. In the process, practice using sets, and test whether sets are immutable
or mutable.
1. Create the following set of animals sold in Pets R Us:
animals = {‘dog’, ‘cat’, ‘fish’, ‘snake’}
Print the identity and contents of this set to the screen.
Do the contents print in the expected order? Do you think that they will always print in
this order, even on other computers?
2. Pets R Us has decided to stop selling snakes, and sell birds instead. Update animals to
reflect this. Once updated, print the identity and contents of animals to the screen.
Has the identity of animals changed? Is a Python set mutable or immutable?

3. Alice is interested in buying either a dog, a cat, a rabbit, or a hamster. Create a set to
represent the pets that Alice would like.
4. Find the intersection of pets between the ones that the store sells and the ones that Alice
would like to buy. Print that set to the screen.

Exercise 3: Sample Output
The contents of object 51131256 are {‘fish’, ‘cat’, ‘snake’, ‘dog’}
The contents of object 51131256 are {‘fish’, ‘cat’, ‘bird’, ‘dog’}
Alice could buy {‘cat’, ‘dog’} from Pets R Us.

Exercise 4
Problem: Follow the steps below to create a program for Bluebell Greenhouses that prints a
purchase receipt. In the process, practice using dictionaries and formatting strings.
1. Bluebell Greenhouses sells the following Spring flower bulbs:

Flower Bulb Name Price Per Bulb
daffodil $ 0.35
tulip $ 0.33
crocus $ 0.25
hyacinth $ 0.75
bluebell $ 0.50
Create a dictionary that stores this information:
bulbsForSale = {‘daffodil’: 0.35, ‘tulip’: 0.33, ‘crocus’: 0.25,
‘hyacinth’: 0.75, ‘bluebell’: 0.50}

2. Mary has a standing order with Bluebell Greenhouses for 50 daffodil bulbs and 100 tulip
bulbs every year. Create a new dictionary that stores information about Mary’s order. Which
data should be the keys (must be unique), and which should be the values?

3. Demand for tulips this year has dramatically outpaced demand. As a result, the price of tulip
bulbs has increased by 25%. Update the price of tulip bulbs in the appropriate dictionary.
(Have your program calculate this and round the price per bulb to 2 decimal places. Do not
calculate yourself and hardcode the new value in.)

4. This year, Mary would also like to try planting hyacinths. Add 30 hyacinth bulbs to the
dictionary that is storing her order.
5. Display Mary’s purchase order for this year on the screen. Each line should be formatted as
follows:
bulb code * number of bulbs = $ subtotal
field width: 5 field width: 4 field width: 6
left-aligned right-aligned right-aligned
2 decimal places

The code for each bulb name is the first three letters of its name, all in capital letters. The
lines should be printed so that the bulb codes are in alphabetical order.

6. Calculate the total number of bulbs that Mary purchased this year, as well as the total cost of
her order. Include this information at the bottom of her purchase order. Format the total cost
float value so that it is right-aligned in a field width of 6, with 2 decimal places.

single space
Exercise 4: Sample Output
You have purchased the following bulbs:
DAF * 50 = $ 17.50
HYA * 30 = $ 22.50
TUL * 100 = $ 41.00
Thank you for purchasing 180 bulbs from Bluebell Greenhouses.
Your total comes to $ 81.00.