7.5 - Supermarket

Problem

A supermarket needs a program to analyse their items.

The program should:

  • Store the items, categories and prices in parallel arrays
  • Use a function to find and return the category of the most expensive item
  • Use a function to count the number of items in the most expensive category
  • Use a procedure to display
    • the category with the most expensive item
    • the number of items in that category
    • the name and price of each item in the category

Example run

Most expensive category: Electronics
Number of items in this category: 3

Items in this category:
Pro Headphones £149.99
Gaming Mouse £39.99
Mechanical Keyboard £89.0

Arrays

items = ["Pro Headphones", "Travel Mug", "Gaming Mouse", "Notebook",
             "Mechanical Keyboard", "Water Bottle", "Desk Lamp"]

categories = ["Electronics", "Home", "Electronics", "Stationery",
                  "Electronics", "Home", "Home"]

prices = [149.99, 12.50, 39.99, 2.49, 89.00, 9.00, 18.75]

 

1 Find most expensive category IN categories, prices
OUT high_category
2 Count items in category IN categories, target
OUT item_count
3 Display results IN

items, categories, prices,

high_category, item_count

OUT
1.1 set high_price to prices[0]
1.2 set high_category to categories[0]
1.3 for i from 1 to length of prices-1
1.4     if prices[i] > high_price then
1.5         set high_price to prices[i]
1.6         set high_category to categories[i]
1.7     end if
1.8 end for
1.9 return highest_category

 

 

# Main
items = ["Pro Headphones", "Travel Mug", "Gaming Mouse", "Notebook",
             "Mechanical Keyboard", "Water Bottle", "Desk Lamp"]
categories = ["Electronics", "Home", "Electronics", "Stationery",
                  "Electronics", "Home", "Home"]
prices = [149.99, 12.50, 39.99, 2.49, 89.00, 9.00, 18.75]

high_category = find_most_expensive_category(categories, prices)

item_count = count_items_in_category(categories, high_category)

display_results(items, categories, prices, high_category, item_count)
def find_most_expensive_category(categories, prices):
    high_price = prices[0]
    high_category = categories[0]
    for i in range(1, len(prices)):
        if prices[i] > high_price:
            high_price = prices[i]
            high_category = categories[i]
    return high_category

def count_items_in_category(categories, target):
    item_count = 0
    for i in range(len(categories)):
        if categories[i] == target:
            item_count = item_count + 1
    return item_count

def display_results(items, categories, prices, high_category, item_count):
    print("Most expensive category:", high_category)
    print("Number of items in this category:", item_count)
    print()
    print("Items in this category:")
    for i in range(len(categories)):
        if categories[i] == high_category:
            print(items[i], "£" + str(prices[i]))

# Main
items = ["Pro Headphones", "Travel Mug", "Gaming Mouse", "Notebook",
             "Mechanical Keyboard", "Water Bottle", "Desk Lamp"]
categories = ["Electronics", "Home", "Electronics", "Stationery",
                  "Electronics", "Home", "Home"]
prices = [149.99, 12.50, 39.99, 2.49, 89.00, 9.00, 18.75]

high_category = find_most_expensive_category(categories, prices)

item_count = count_items_in_category(categories, high_category)

display_results(items, categories, prices, high_category, item_count)
# Function: find_most_expensive_category
# Purpose: Find the category of the single most expensive item.
# Parameters: categories (array), prices (array)
# Returns: the category of the item with the highest price
def find_most_expensive_category(categories, prices):
    high_price = prices[0]
    high_category = categories[0]
    for i in range(1, len(prices)):
        if prices[i] > high_price:
            high_price = prices[i]
            high_category = categories[i]
    return high_category


# Function: count_items_in_category
# Purpose: Count how many items belong to a given category.
# Parameters: categories (array), target (string)
# Returns: the number of matching items
def count_items_in_category(categories, target):
    item_count = 0
    for i in range(len(categories)):
        if categories[i] == target:
            item_count = item_count + 1
    return item_count


# Procedure: display_results
# Purpose: Display the results to the user.
# Parameters: items (array), categories (array), prices (array),
#             high_category (string), item_count (integer)
# Returns: nothing (outputs to screen)
def display_results(items, categories, prices, high_category, item_count):
    print("Most expensive category:", high_category)
    print("Number of items in this category:", item_count)
    print()
    print("Items in this category:")
    for i in range(len(categories)):
        if categories[i] == high_category:
            print(items[i], "£" + str(prices[i]))


# Main Program
items = ["Pro Headphones", "Travel Mug", "Gaming Mouse", "Notebook",
         "Mechanical Keyboard", "Water Bottle", "Desk Lamp"]
categories = ["Electronics", "Home", "Electronics", "Stationery",
              "Electronics", "Home", "Home"]
prices = [149.99, 12.50, 39.99, 2.49, 89.00, 9.00, 18.75]

# Call 1: find the category of the most expensive item
high_category = find_most_expensive_category(categories, prices)

# Call 2: count how many items belong to that category
item_count = count_items_in_category(categories, high_category)

# Call 3: display the results using a procedure
display_results(items, categories, prices, high_category, item_count)

Extension

Update the program so that it will also display the name of the most expensive item seperatley.

Most expensive item: Pro Headphones £149.99 
Most expensive category: Electronics 
Number of items in this category: 3 

Other items in this category: 
Gaming Mouse £39.99 
Mechanical Keyboard £89.0

Target

Write a function that uses parameters

Demonstrate parallel arrays with a shared index