Lesson 4
HashMaps in Action: Solving Real-World Problems in Python
Introduction and Goal Setting

Hello there! In this lesson we will apply HashMaps, a data structure, to real-world challenges. Our focus will be on solving tasks such as cataloging books in a library, counting votes in an election, and tracking inventories.

Real-World Scenarios Calling for HashMaps

HashMaps are beneficial in real-life applications, such as the ones mentioned above, due to their ability to rapidly retrieve data with unique keys and efficiently handle larger datasets. Let's understand their efficiency with some actual examples.

Solving Real-World Task 1: Cataloging Books in a Library

Suppose you're asked to manage the cataloging of books in a library. In Python, we represent HashMaps using dictionaries. Here, the book ID serves as the key, while the details of the book, such as the title, author, and year of publication, are stored as values.

This approach allows us to add, search for, and remove books from our library catalog using just a few lines of Python code.

Python
1library_catalog = {} # Initializing HashMap 2 3# Details of a book 4book_id = "123" 5book_details = {"title": "To Kill a Mockingbird", "author": "Harper Lee", "year_published": 1960} 6 7library_catalog[book_id] = book_details # Adding a book to library catalog 8 9if book_id in library_catalog: # Searching for a book 10 print(library_catalog[book_id]) 11 12del library_catalog[book_id] # Removing a book from library

As you can see, HashMaps make the task of cataloging books in the library simpler and more efficient!

Solving Real-World Task 2: Counting Votes in an Election

Imagine a scenario in which we need to count votes in an election. We employ a HashMap, where each name is a unique key, and the frequency of that name serves as the associated value. Let's write some Python code to better understand this.

Python
1votes_list = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"] # Cast votes 2vote_counts = {} # Initializing a HashMap 3 4# Counting the votes 5for name in votes_list: 6 if name in vote_counts: # If name is already a key 7 vote_counts[name] += 1 # increment its value (count) 8 else: # If name doesn't exist as a key 9 vote_counts[name] = 1 # add it as a key and set value as 1 10 11print(vote_counts) 12# Prints {'Alice': 3, 'Bob': 2, 'Charlie': 1}

HashMaps facilitate the efficient counting of votes.

Solving Real-World Task 3: Tracking Store Inventories

Finally, consider a task that involves managing a store's inventory. Here, we can use a HashMap in which product names are keys and quantities are values. This approach allows us to easily add new items, adjust the quantity of items, check whether an item is in stock, and much more.

Python
1store_inventory = {"Apples": 100, "Bananas": 80, "Oranges": 90} # Initializing an inventory 2 3store_inventory["Pears"] = 50 # Adding a new product to inventory and setting its quantity 4 5store_inventory["Apples"] += 20 # Updating number of apples in inventory 6 7prod = "Apples" # A product to be checked 8print("Total", prod, "in stock:", store_inventory[prod]) 9 10# Check if a product is in stock 11prod = "Mangoes" 12if prod in store_inventory: # If mangoes exist in inventory 13 print(prod, "are in stock.") 14else: # If mangoes doesn't exist in inventory 15 print(prod, "are out of stock.")

Thus, when managing inventory data, HashMaps offer an efficient solution!

Lesson Summary and Practice

In this lesson, we bridged the gap between the theory of HashMaps and their practical applications. We explored real-world problems that can be solved using HashMaps and implemented Python code to address them.

Now, get ready for hands-on practice exercises that will help reinforce these concepts and hone your HashMap problem-solving skills. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.