Пример #1
0
from hashmap import HashMap
from linkedlist import LinkedList

#Printing the Welcome Message
print_welcome()

#Write code to insert food types into a data structure here. The data is in data.py
food_categories = Trie()
for type in types:
  food_categories.insert(type)


#Write code to insert restaurant data into a data structure here. The data is in data.py
restaurant_directory = HashMap(len(types))
for type in types:
  restaurant_directory.assign(type, LinkedList())
  
for i in restaurant_data:
  business_hash = HashMap(len(i)-1)
  business_hash.assign('name',i[1])
  business_hash.assign('price',i[2])
  business_hash.assign('rating',i[3])
  business_hash.assign('address',i[4])
  restaurant_directory.retrieve(i[0]).insert_beginning(business_hash)

#print(restaurant_directory.array)


      
#Write code for user interaction here
while True:
Пример #2
0
from hashmap import HashMap
from linkedlist import LinkedList

N = 6

#Insert Data Into HashMap
my_hashmap = HashMap(N)
my_hashmap.assign("Zachary", "Sunburn Sickness")
my_hashmap.assign("Elise", "Severe Nausea")
my_hashmap.assign("Mimi", "Stomach Flu")
my_hashmap.assign("Devan", "Malaria")
my_hashmap.assign("Gary", "Bacterial Meningitis")
my_hashmap.assign("Neeknaz", "Broken Cheekbone")

#Insert Data into LinkedList
my_linked_list = LinkedList(["Zachary", "Sunburn Sickness"])
my_linked_list.insert_beginning(["Elise", "Severe Nausea"])
my_linked_list.insert_beginning(["Mimi", "Stomach Flu"])
my_linked_list.insert_beginning(["Devan", "Malaria"])
my_linked_list.insert_beginning(["Gary", "Bacterial Meningitis"])
my_linked_list.insert_beginning(["Neeknaz", "Broken Cheekbone"])

#Get Zachary's Disease from a HashMap
hashmap_zachary_disease = my_hashmap.retrieve("Zachary") #Checkpoint 1
print("Zachary's disease is {0}".format(hashmap_zachary_disease))
hashmap_runtime = "1" #Checkpoint 2
print("The runtime of retrieving a value from a hashmap is O({0})\n\n".format(hashmap_runtime))

#Get Zachary's Disease from a Linked List
#Write Code here for Checkpoint 3
current_node = my_linked_list.get_head_node()
from trie import Trie
from data import *
from welcome import *
from hashmap import HashMap
from linkedlist import LinkedList

# Printing the Welcome Message
print_welcome()

# Entering cuisine data
food_types = Trie()
eateries = HashMap(len(types))
for food in types:
    food_types.add(food)
    eateries.assign(food, LinkedList())

# restaurant data-point key names:
eatery_cuisine = "cuisine"
eatery_name = "name"
eatery_price = "price"
eatery_rating = "rating"
eatery_address = "address"
# Entering restaurant data
for restaurant in restaurant_data:
    current_eateries_for_cuisine = eateries.retrieve(restaurant[0])
    current_eatery_data = HashMap(len(restaurant))
    current_eatery_data.assign(eatery_cuisine, restaurant[0])
    current_eatery_data.assign(eatery_name, restaurant[1])
    current_eatery_data.assign(eatery_price, restaurant[2])
    current_eatery_data.assign(eatery_rating, restaurant[3])
    current_eatery_data.assign(eatery_address, restaurant[4])
from trie import Trie
from data import *
from welcome import *
from hashmap import HashMap
from linkedlist import LinkedList

#Printing the Welcome Message
print_welcome()

#Write code to insert food types into a data structure here. The data is in data.py
userChoice = HashMap(len(types))
for type in types:
    if userChoice.retrieve(type[0]) is None:
        ll = LinkedList(type)
        userChoice.assign(type[0], ll)
    else:
        ll = userChoice.retrieve(type[0])
        ll.insert_beginning(type)
        userChoice.assign(type[0], ll)

# Write code to insert restaurant data into a data structure here. The data is in data.py
restaurantChoice = HashMap(len(restaurant_data))
for restaurant in restaurant_data:
    if restaurantChoice.retrieve(restaurant[0]) is None:
        ll = LinkedList(restaurant[1:6])
        restaurantChoice.assign(restaurant[0], ll)
    else:
        ll = restaurantChoice.retrieve(restaurant[0])
        ll.insert_beginning(restaurant[1:6])
        restaurantChoice.assign(restaurant[0], ll)
Пример #5
0
# gets a valid response to a yes or no question
def get_valid_response():
  response = input().lower()
  while response not in ('y','n'):
    print('Please enter\'y\' or \'n\'')
    response = input().lower()
  return response
  
#Inserting food types into a data structure
type_map = HashMap(30)
for cuisine in types:
  # value_list is a list of all cuisines starting with the same letter
  value_list = type_map.retrieve(cuisine[0])
  if value_list is None:
    # def: assign(self, key, value)
  	type_map.assign(cuisine[0],[cuisine])
  else:
    value_list += [cuisine]
    type_map.assign(cuisine[0],value_list)

    
#Inserting restaurant data into a data structure
restaurant_map = HashMap(100)
for restaurant in restaurant_data:
  # cuisine type as key and restaurant name as value
  restaurant_type = restaurant[0]
  # restaurant_list is a list of restaurants with the same cuisine (I.E. Korean)
  restaurant_list = restaurant_map.retrieve(restaurant_type)
  if restaurant_list is None:
    restaurant_map.assign(restaurant_type,[restaurant])
  else:
# Utilizing a Stack to upload the restaurant list of lists in one go so that we don't
# have to iterate over it more than once which keeps us at O(N) asyptotic notation.
# The Stack module was not available in the project directory, so I had to create it
# from the lesson. I also added a "fill" class var that initializes the Stack from a list arg.
restack = Stack(fill=restaurant_data)
food_type_hashmap = HashMap(30)
restaurant_hashmap = HashMap(30)

# Unpack the stack
types.sort()
while not restack.is_empty():
  rest = restack.pop()
  # If the food_type hasn't been hashmap'd for a single char to a LinkedList, assign it, else insert.  
  if not food_type_hashmap.retrieve(rest[0][0]):
    ll_char = LinkedList(rest[0])
    food_type_hashmap.assign(rest[0][0], ll_char)
  else:
    fth_list = food_type_hashmap.retrieve(rest[0][0])
    if not fth_list.exists(rest[0]):
      fth_list.insert_beginning(rest[0])
  # If the food_type hasn't been hashmap'd for a two char to a LinkedList, assign it.
  # Future work: If we need to go beyond 2 chars, we'll insert here, and then grab a third.
  if not food_type_hashmap.retrieve(rest[0][:2]):  
    ll_char2 = LinkedList(rest[0])
    food_type_hashmap.assign(rest[0][:2], ll_char2)
      
  # Create the food type hashmap of LinkedListed restaurants. 
  if not restaurant_hashmap.retrieve(rest[0]):
    ll_rest = LinkedList(rest)
    restaurant_hashmap.assign(rest[0], ll_rest)
  else:

#Printing the Welcome Message
print_welcome()

#Write code to insert food types into a data structure here. The data is in data.py
# gather all first letters for food types
first_letters = [food_type[:1] for food_type in types]

# initialize hash map to map first letters to linked list of food types
letter_food_map = HashMap(len(first_letters))

# map first letter of food types to a linked list of food types
for food_type in types:
    if letter_food_map.retrieve(food_type[:1]) is None:
        letter_food_map.assign(food_type[:1], LinkedList(food_type))
    else:
        old_linked_list = letter_food_map.retrieve(food_type[:1])
        old_linked_list.insert_beginning(food_type)
        letter_food_map.assign(food_type[:1], old_linked_list)

## TEST CASE ##
#print(letter_food_map.retrieve('c').stringify_list())

#Write code to insert restaurant data into a data structure here. The data is in data.py

# will map food type to a linked list of restaurants with given type
type_restaurant_map = HashMap(len(types))

for restaurant in restaurant_data:
    type, name, price, rating, address = restaurant
#Printing the Welcome Message
print_welcome()

#Write code to insert food types into a data structure here. The data is in data.py
trie = Trie()
for food_type in types:
  trie.insert(food_type)

#Write code to insert restaurant data into a data structure here. The data is in data.py
hashmap = HashMap(len(types))
for food_type in types:
  linkedlist = LinkedList()
  for restaurant in restaurant_data:
    if food_type == restaurant[0]:
      linkedlist.insert_beginning(restaurant)
      hashmap.assign(food_type, linkedlist)

#Write code for user interaction here
while True:
    user_input = str(input("\nSearch for a food type here. \nType 'quit' anytime to exit.\n")).lower()
    if user_input == 'quit':
      exit()
    #Search for user_input in food types data structure here
    if len(trie.find_words(user_input)) < 1:
      print('No food types found. Try again')
    else:
      if len(trie.find_words(user_input)) > 1:
    	  print("Your choices are: {}".format(trie.find_words(user_input)))
      else:
        user_input_select = str(input("\nThe closest search result is: {}\n Would you like to look at this type? \n 'y' for yes or 'n' for no.\nType 'quit' to exit.\n".format(trie.find_words(user_input)[0]))).lower()
        if user_input_select == 'y':
#Write code to insert food types into a data structure here. The data is in data.py
food_types = LinkedList()
for food in types:
    food_types.insert_beginning(food)
#print(food_types.stringify_list())
#print(type(food_types))

#Write code to insert restaurant data into a data structure here. The data is in data.py
type_rest = HashMap(len(types))
for food in types:
    rest_list = LinkedList()
    for rest in restaurant_data:
        #print(rest[0])
        if rest[0] == food:
            rest_hash = HashMap(4)
            rest_hash.assign("name", rest[1])
            rest_hash.assign("price", rest[2])
            rest_hash.assign("rating", rest[3])
            rest_hash.assign("address", rest[4])
            #print(rest_hash.retrieve("address"))
            rest_list.insert_beginning(rest_hash)
    type_rest.assign(food, rest_list)  #print(rest_list.stringify_list())

#Write code for user interaction here
while True:
    user_input = str(
        input(
            "\nWhat type of food would you like to eat?\nType the beginning of that food type and press enter to see if it's here.\n"
        )).lower()
    #Search for user_input in food types data structure here
    matches = []
Пример #10
0
# Ok, now create a set of first letters for our hash map
first_letters = {i[0] for i in types}
array_size_letters = len(first_letters)
letter_to_type_hashmap = HashMap(array_size_letters)

#Write code to insert food types into a data structure here. The data is in data.py

for i in first_letters:
    ll = LinkedList()
    for j in types:
        if j.startswith(i) and ll.head_node.get_value():
            ll.insert_beginning(j)
        elif j.startswith(i) and not ll.head_node.get_value():
            ll = LinkedList(j)

    letter_to_type_hashmap.assign(i, ll)

#Write code to insert restaurant data into a data structure here. The data is in data.py

for i in types:
    ll = LinkedList()
    for j in restaurant_data:
        if j[0] == i and ll.head_node.get_value():
            nt = Restaurant(j[0], j[1], j[2], j[3], j[4])
            ll.insert_beginning(nt)
        elif j[0] == i and not ll.head_node.get_value():
            nt = Restaurant(j[0], j[1], j[2], j[3], j[4])
            ll = LinkedList(nt)
    type_to_data_hashmap.assign(i, ll)

#Write code for user interaction here
Пример #11
0
#I went for it. Did a trie. Woo.

food_trie = Trie()

for food in types:
    food_trie.insert(food)

#Write code to insert restaurant data into a data structure here. The data is in data.py

#Create the overall HashMap
resto_styles = HashMap(len(types))

#Create the Linked Lists for the HashMap
for food in types:
    resto_styles.assign(food, LinkedList())


#Create the HashMaps for each restaurant and plug them into the appropriate Linked List
def build_data(dat):
    for restaurant in dat:
        resto = resto_styles.retrieve(restaurant[0])
        resto.insert_beginning(HashMap(4))
        node = resto.get_head_node().get_value()
        node.assign('Name', restaurant[1])
        node.assign('Price', restaurant[2])
        node.assign('Rating', restaurant[3])
        node.assign('Address', restaurant[4])
    return resto_styles

Пример #12
0
from welcome import *
from hashmap import HashMap
from linkedlist import LinkedList

#Printing the Welcome Message
print_welcome()

#Write code to insert food types into a data structure here. The data is in data.py
food_types = Trie()
food_types.make_trie(types)
#Write code to insert restaurant data into a data structure here. The data is in data.py
restaurants = HashMap(len(types))
for restaurant_datum in restaurant_data:
    key, value = restaurant_datum[0], restaurant_datum[1:]
    if not restaurants.has_key(key):
        restaurants.assign(key, LinkedList(value))
    else:
        restaurants.retrieve(key).insert_beginning(value)
#Write code for user interaction here
while True:
    #start the loop by clearing any suggested words
    food_types.reset_words()
    user_input = str(
        input(
            "\nWhat type of food would you like to eat?\nType the beginning of that food type and press enter to see if it's here, or press 'Enter' with no input to see all options.\n"
        )).lower()
    #Search for user_input in food types data structure here
    res = food_types.return_suggestions(user_input)
    if res == -1:
        print("No other strings found with this prefix\n")
    elif res == 0:
Пример #13
0
# Write code to insert food types into a data structure here. The data is in data.py
types_trie = Trie()
types_trie.add_list_values(types)

# Write code to insert restaurant data into a data structure here. The data is in data.py
restaurants_hash = HashMap(len(restaurant_data))

while (len(restaurant_data)) > 0:
    templist = []
    typename = restaurant_data[0][0]
    typelist = LinkedList()
    typelist.remove_node(None)
    for restaurant in restaurant_data:
        if restaurant[0] == typename:
            rest_hash = HashMap(len(restaurant))
            rest_hash.assign("name", restaurant[1])
            rest_hash.assign("price", restaurant[2])
            rest_hash.assign("rating", restaurant[3])
            rest_hash.assign("address", restaurant[4])
            typelist.insert_beginning(rest_hash)
        else:
            templist.append(restaurant)
    restaurants_hash.assign(typename, typelist)
    restaurant_data = templist


def proceed():
    while True:
        user_input3 = str(
            input(
                "\nShow restaurants for this food type? Type 'y' for Yes or 'n' for No.\n"