You are on page 1of 3

CS 115 - Introduction to Programming in Python

Lab Guide 09

Lab Objectives: Searching and Sorting

1. Write a program that has the following functionality:


a. Download the Order.py class from Moodle and update the following:
i. Add a method sort_items()that sorts the list of items for an order by name
using the selection sort algorithm.
ii. Add a method, get_item_by_name() that takes a string name and searches
and returns the item in the list of items with the given name. Use the binary search
algorithm.
iii. Add a __lt __() method that compares two Order objects, and returns True
if the first Order (self) has a month that comes before that of the Order
passed as a parameter. If the months are the same it should compare the days.
Note: datetime.datetime.strptime(var, ‘%Y%m%d’).date()
converts the given string var string to a datetime, where %Y indicates the
position of the 4 digit year, %m the two digit month, and %d the two digit date.
See examples below for working with system dates:

aDate = datetime.datetime.strptime('20190123', '%Y%m%d').date()


aDate
Out[45]: datetime.date(2019, 1, 23)
aDate.year
Out[46]: 2019
aDate.month
Out[47]: 1
aDate.day
Out[48]: 23
import calendar
calendar.month_name[aDate.month]
Out[50]: 'January'
datetime.datetime.today()
Out[51]: datetime.datetime(2019, 9, 18, 9, 27, 31, 143983)
datetime.datetime.today().month
Out[52]: 9

iv. Add a __repr__ method to return the string representation of an Order. See
sample run for format.
b. Write a the following functions in python file, Lab09.py :
i. load_orders (): function takes a filename as a parameter. The function
should read the data from the file, create Orders from the file and add them to a
list. Return the list of Orders. Read and understand the data in the file and the
functionality provided in the Order class to create the orders.
ii. get_orders_by_price (): takes a list of Orders, a minimum value and a
maximum value as parameters. The function should search the list and return ALL
orders whose total price is between the minimum and maximum value.
c. Write a script that does the following:
i. Load a list of Orders with data from the file order_list.txt. The file is
whitespace delimited.
ii. Display the list of Orders.
iii. Sort and display the list of Orders by their delivery dates in ascending order.
iv. For each Order in the list, sort the items by name. (use functionality defined in the
Order class).
v. Input a minimum and maximum price and display all orders whose total is between
the min and max.
vi. Input an item to search, and for the first Order in the list, display the information
(name and price) of the item.
Sample Run:
Orders:
[
Johnson Mark 2021-09-18 [('bananas', 10.96), ('apples', 2.5), ('milk', 3.95)] Total: 17.41,
Kirkdal Shirley 2021-12-16 [('oranges', 5.96), ('apples', 2.5), ('tea', 7.95)] Total: 16.41,
Mackie Inaayah 2021-09-05 [('pears', 2.96), ('apples', 2.5), ('milk', 3.95)] Total: 9.41,
Freeman Keenan 2021-12-20 [('watermelon', 15.96), ('apples', 2.5), ('milk', 3.95)] Total: 22.41,
Woolley Janine 2021-05-30 [('oranges', 5.96), ('apples', 2.5), ('milk', 3.95)] Total: 12.41,
Michael Eadie 2021-12-19 [('chicken_breast', 25.96), ('apples', 2.5), ('milk', 3.95)] Total: 32.41,
Dermott Kris 2021-12-18 [('bread', 3.5), ('apples', 2.5), ('milk', 3.95)] Total: 9.95,
Terrell Melvin 2021-11-22 [('mozzarella', 15.45), ('potatoes', 2.5), ('milk', 3.95)] Total: 21.90,
Allison Naeem 2021-09-07 [('tomatoes', 8.95), ('parsley', 1.5), ('coffee', 45.95)] Total: 56.40,
Johnson Trey 2021-02-21 [('oranges', 5.96), ('apples', 2.5), ('milk', 3.95)] Total: 12.41,
Plummer Elana 2021-12-17 [('spaghetti', 10.0), ('zucchini', 8.5), ('parmesan', 73.95)] Total: 92.45,
Allison Joelle 2021-09-18 [('sparkling_water', 8.95), ('lime', 12.5), ('oranges', 3.95)] Total: 25.40,
Smither Sandy 2021-12-17 [('tuna', 25.5), ('pickles', 8.5), ('white_cheese', 13.25)] Total: 47.25]

Orders by Date:
[
Johnson Trey 2021-02-21 [('oranges', 5.96), ('apples', 2.5), ('milk', 3.95)] Total: 12.41,
Woolley Janine 2021-05-30 [('oranges', 5.96), ('apples', 2.5), ('milk', 3.95)] Total: 12.41,
Mackie Inaayah 2021-09-05 [('pears', 2.96), ('apples', 2.5), ('milk', 3.95)] Total: 9.41,
Allison Naeem 2021-09-07 [('tomatoes', 8.95), ('parsley', 1.5), ('coffee', 45.95)] Total: 56.40,
Johnson Mark 2021-09-18 [('bananas', 10.96), ('apples', 2.5), ('milk', 3.95)] Total: 17.41,
Allison Joelle 2021-09-18 [('sparkling_water', 8.95), ('lime', 12.5), ('oranges', 3.95)] Total: 25.40,
Terrell Melvin 2021-11-22 [('mozzarella', 15.45), ('potatoes', 2.5), ('milk', 3.95)] Total: 21.90,
Kirkdal Shirley 2021-12-16 [('oranges', 5.96), ('apples', 2.5), ('tea', 7.95)] Total: 16.41,
Plummer Elana 2021-12-17 [('spaghetti', 10.0), ('zucchini', 8.5), ('parmesan', 73.95)] Total: 92.45,
Smither Sandy 2021-12-17 [('tuna', 25.5), ('pickles', 8.5), ('white_cheese', 13.25)] Total: 47.25,
Dermott Kris 2021-12-18 [('bread', 3.5), ('apples', 2.5), ('milk', 3.95)] Total: 9.95,
Michael Eadie 2021-12-19 [('chicken_breast', 25.96), ('apples', 2.5), ('milk', 3.95)] Total: 32.41,
Freeman Keenan 2021-12-20 [('watermelon', 15.96), ('apples', 2.5), ('milk', 3.95)] Total: 22.41]

Enter minimum price: 30

Enter maximum price: 50


[
Smither Sandy 2021-12-17 [('pickles', 8.5), ('tuna', 25.5), ('white_cheese', 13.25)] Total: 47.25,
Michael Eadie 2021-12-19 [('apples', 2.5), ('chicken_breast', 25.96), ('milk', 3.95)] Total: 32.41]

Enter name of item to search: milk


Matching item for first order:
('milk', 3.95)

You might also like