×
Reviews 4.9/5 Order Now

Program to Implement Recipe Manager in Python Assignment Solution

June 14, 2024
Dr. Nicole
Dr. Nicole
🇬🇧 United Kingdom
Python
Dr. Nicole, an accomplished professional with 6 years of experience, obtained her Ph.D. from Princeton University. Having completed over 400 Python assignments, she brings a wealth of expertise and insight to the table. With a focus on clarity and precision, Dr. Nicole is committed to providing comprehensive support to students seeking assistance with their Python projects.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Focus on responsive design and clean code structure. Use frameworks like Bootstrap or Tailwind CSS to save time, and ensure your website is optimized for mobile and desktop.
News
Lit A lightweight library for building fast, reusable components, embracing the Web Components standard for modular and maintainable code.

Instructions

Objective

Write a python assignment program to implement recipe manager

Requirements and Specifications

program-to-implement-recipe-manager-in-python (1)
program-to-implement-recipe-manager-in-python 1 (1)

Source Code

import python.util.HashSet; import python.util.Set; public class MyHashMap { // Holds an array of linked list of hash nodes private Object[] nodes = new Object[8]; // Remove all elements public void clear() { for (int i = 0; i < nodes.length; i++) { nodes[i] = null; } } // Check if key exists public boolean containsKey(K key) { int index = hash(key); if (nodes[index] == null) { return false; } HashNode currentNode = (HashNode) nodes[index]; while (currentNode != null) { if (currentNode.getKey().equals(key)) { return true; } currentNode = currentNode.getNext(); } return false; } // Check if value exists public boolean containsValue(V value) { for (Object object : nodes) { if (object != null) { HashNode currentNode = (HashNode) object; while (currentNode != null) { if (currentNode.getValue().equals(value)) { return true; } currentNode = currentNode.getNext(); } } } return false; } // Get the value associated with the key public V get(K key) { int index = hash(key); if (nodes[index] == null) { return null; } HashNode currentNode = (HashNode) nodes[index]; while (currentNode != null) { if (currentNode.getKey().equals(key)) { return currentNode.getValue(); } currentNode = currentNode.getNext(); } return null; } // Check if empty public boolean isEmpty() { for (Object object : nodes) { if (object != null) { return false; } } return true; } // Return the set of keys public Set keySet() { Set set = new HashSet<>(); for (Object object : nodes) { if (object != null) { HashNode currentNode = (HashNode) object; while (currentNode != null) { set.add(currentNode.getKey()); currentNode = currentNode.getNext(); } } } return set; } // Add or update a new pair public void put(K key, V value) { int index = hash(key); // No collisions for fresh slot if (nodes[index] == null) { nodes[index] = new HashNode<>(key, value); return; } // Search if the key already exists HashNode currentNode = (HashNode) nodes[index]; while (currentNode != null) { if (currentNode.getKey().equals(key)) { // Do update ofkey already exists currentNode.setValue(value); return; } currentNode = currentNode.getNext(); } // Do add in front HashNode node = new HashNode<>(key, value); node.setNext((HashNode) nodes[index]); nodes[index] = node; } // Remove the key and returnthe associated value public V remove(K key) { int index = hash(key); if (nodes[index] == null) { return null; } HashNode previousNode = null; HashNode currentNode = (HashNode) nodes[index]; while (currentNode != null) { if (currentNode.getKey().equals(key)) { if (currentNode == nodes[index]) { // Case deleting the head nodes[index] = ((HashNode) nodes[index]).getNext(); } else { // Case deleting in-between previousNode.setNext(currentNode.getNext()); } return currentNode.getValue(); } previousNode = currentNode; currentNode = currentNode.getNext(); } return null; } // Return the numberof elements public int size() { int overallSize = 0; for (int i = 0; i < nodes.length; i++) { overallSize += listSize(i); } return overallSize; } // Print out the table public void printTable() { int conflicts = 0; for (int i = 0; i < nodes.length; i++) { conflicts += printList(i); } System.out.println("Total # of conflicts: " + conflicts); } // Print the list content private int printList(int index) { System.out.print("Index " + index + ": "); int totalConflicts = 0; if (nodes[index] == null) { System.out.println(" (0 conflicts), []"); } else { int conflicts = listSize(index) - 1; totalConflicts += conflicts; System.out.print(" (" + conflicts + " conflicts), ["); HashNode currentNode = (HashNode) nodes[index]; while (currentNode != null) { System.out.print(currentNode.getKey() + ", "); currentNode = currentNode.getNext(); } System.out.println("]"); } return totalConflicts; } // Return how many elements are in the linked list private int listSize(int index) { if (nodes[index] == null) { return 0; } int count = 0; HashNode currentNode = (HashNode) nodes[index]; while (currentNode != null) { count++; currentNode = currentNode.getNext(); } return count; } // Hash the key private int hash(K key) { int hashCode = key.hashCode(); int index = hashCode % nodes.length; return Math.abs(index); } }

Similar Samples

Check out our diverse range of programming homework samples to see the quality and precision we deliver. Each sample demonstrates our expertise in various programming languages and problem-solving skills. These examples highlight our commitment to helping you excel in your programming assignments.