# Assignment

Discounted Products

This is a programming assignment. Your goal with this assignment is to build a simplified online version of a weekend discount advertisement from a department store.

This program has two parts: the first part is to read discount product information from a text file and populate an appropriate data structure with the product information; and the second part is to display the information on the screen which allows a user to change the order in which discount information is displayed.

The text file should contain discount information for at least 30 different products.

Information about a product should contain at least the following six attributes:

Product name, Locations/Branches where it is available, Original price, Discounted price, Sold out or not, Popularity (0-5 stars)

Users should be allowed to alter the way the discount information is displayed, both in ascending and descending orders, for each of the six attributes mentioned above.

You are required to employ two (2) different sorting techniques in your program:

1. MergeSort
2. Randomized QuickSort

Solution

discount.py

import random

import operator

ORDERING = {True: operator.lt, False: operator.gt}

defmerge_sort(data, key=lambda x: x, ascending=True):

”’

data:      a list of items to be sorted

key:       a function returning the item attribute to sort data list by

(defalut is identity function)

ascending: boolean indicating whether data should be sorted in

ascending (True) or descending (False) order

(default True)

”’

n = len(data)

if n < 2:   # zero or single-element list — no need to sort

return data

# sort the left part of the list

left  =merge_sort(data[:n//2], key, ascending)

# sort the right part of the list

right = merge_sort(data[n//2:], key, ascending)

merged = []

op = ORDERING[ascending]  # determine the operation for the ordering (< or >)

i, j = 0, 0

# merge two sorted lists

# as long as both have elements present

while i <len(left) and j <len(right):

if op( key(left[i]), key(right[j]) ):

merged.append(left[i])

i = i + 1

else:

merged.append(right[j])

j = j + 1

# add remaining elements to the list

# only one of those two loops is executed

while i <len(left):

merged.append(left[i])

i = i + 1

while j <len(right):

merged.append(right[j])

j = j + 1

return merged

defrand_quick_sort(data, key=lambda x: x, ascending=True):

”’

data:      a list of items to be sorted

key:       a function returning the item attribute to sort data list by

(defalut is identity function)

ascending: boolean indicating whether data should be sorted in

ascending (True) or descending (False) order

(default True)

”’

n = len(data)

if n < 2:   # zero or single-element list — no need to sort

return data

# pick a pivot element randomly

j = random.randrange(0, n)

pivot = key(data[j])

op = ORDERING[ascending]  # determine the operation for the ordering (< or >)

left, center, right = [], [], []

# split data list into 3 parts

for item in data:

if pivot == key(item):

center.append(item)

elif op( key(item), pivot ):

left.append(item)

else:

right.append(item)

# recursively sort left and right

left_sorted  =rand_quick_sort(left, key, ascending)

right_sorted = rand_quick_sort(right, key, ascending)

returnleft_sorted + center + right_sorted

# Product name, Locations/Branches where it is available, Original price, Discounted price, Sold out or not, Popularity (0-5 stars)

with open(fname, ‘r’) as fh:

data = []

for line in lines:

items = line.split(‘, ‘)

record = ( items[0], items[1], float(items[2]), float(items[3]), items[4], int(items[5])*’*’ )

data.append(record)

return data

defdisplay_data(data):

header = [‘Product name’, ‘Location’, ‘Original’, ‘Discounted’, ‘State’, ‘Rating’]

print(“{:>15} {:>15} {:>10} {:>12} {:>10} {:>8}”.format(*header))

print(‘-‘*75)

for item in data:

print(“{:>15} {:>15} {:>10.2f} {:>12.2f} {:>10} {:>8}”.format(*item))

def main():

fname = ‘data.txt’

running = True

while running:

print(“”)

print(“Choose an action:”)

print(“1. Display Discount Information”)

print(“2. Quit”)

action = input(‘> ‘)

if action == ‘1’:

print(“”)

print(“Choose an attribute to sort by:”)

print(“1. Product name”)

print(“2. Locations/Branches where it is available”)

print(“3. Original price”)

print(“4. Discounted price”)

print(“5. Sold out or not”)

print(“6. Popularity”)

key_str = input(‘> ‘)

if key_str in [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’]:

key_func = lambda x: x[int(key_str) – 1]

print(“”)

print(“Choose sorting order:”)

print(“1. Ascending”)

print(“2. Descending”)

order = input(‘> ‘)

if order in [‘1’, ‘2’]:

ascending = order == ‘1’

data = merge_sort(data, key_func, ascending)

#data = rand_quick_sort(data, key_func, ascending)

display_data(data)

else:

else: