1- Make sure you check the inputs and validate them. We check your code with different inputs. In other words, we try to break your code. Your code must be resistance to different kinds of user...

1 answer below »
python


1- Make sure you check the inputs and validate them. We check your code with different inputs. In other words, we try to break your code. Your code must be resistance to different kinds of user inputs. Unless I have mentioned something is out of scope, you assume all the possible inputs. ************************************************************************************ You already have seen the Binary search algorithm on Day 07 notes. You see how we can bisect/ eliminate half of the items in the problem domain (in a list or any iterable object) in each iteration and come up with a shorter list to explore. This is the main idea of binary search: Eliminating part of the problem space and narrow it down to a smaller space to find the answer faster. In question 1, below, I want you to have this matter in your mind. You must not forget that the binary search works on the sorted lists. Question 1) Write a code that finds the elements of a sorted list that its index is equal to the item in that index. For example if the list is lst= [-2,0,2,3,6,7,9] then code has to return 2 and 3 since lst[2]=2 and lst[3]=3 Hint1: do you think the above question has any relation to the binary search? Think about it. It is a sorted list, so it has the first condition. Do you want to search all the items on the list? What if the list has 1 million elements? Do you want to test 1 million items to see if lst[i]=i? (imagine the last item is the answer and you have to go through the entire list to get to the last item) Can you use this fact that if lst[j]>j then no entry after j can satisfy the given criterion? This is because each element in the list is at least 1 greater than the previous element. For the same reason if lst[j]45 in the above case. If your idea is to compare the very left positions, then you must be careful since 10>2 even though 1<2 make sure your code does not break if the string is empty. assume we do not give a negative number or none-numeric characters to the program, so you do not need to check them. however, they can be equal to each other or equal to zero. print the appropriate message to use if they are equal. question 3) let’s say we want to check if the sum of two numbers in a list is equal to 10. the list can be a very big list. we do not want to iterate through the list more than once. do not use the brute force method. hint: let’s say our list is [3,4,1,2,9]. in a brute force approach, you can check the first item (i.e. 3) with all the other items in the list and if that is 10, print those two items. otherwise, try the second item and compare it with third, fourth and so on. you have to do this comparison for all the items as it is possible no two items in the list add up to 10. the problem is if the list has n list, you check each item with the other n-1 items in each iteration. since you have n items, in total you perform n(n-1) comparisons (since you pick one of the n numbers and compare that with the remaining (n-1) numbers). but the question asks you not to iterate through the list more than once (n). how can we do that? you should use a more innovative solution. for example, you can use a dictionary that the keys are the list items and the value is 1 if (value=10-item) does not exist. for example, for a list like this: [3,4,1,2,9] the 10-3(first value in the list)=7. we do not have 7 in the dictionary yet (we just started to populate the dictionary). so we insert the 3 in the dictionary and put 1 in its value key value 3 1 then we calculate 10-4(second value in the list)=6. we have just 3 in the dictionary, so we go ahead and add that to the dictionary too: key value 3 1 4 1 then 10-1=9. we have 3 and 4 in the dictionary. so, let’s add that to the dictionary: key value 3 1 4 1 1 1 the next one is 10-2=8 key value 3 1 4 1 1 1 2 1 now we come to the last item: 10-9=1. this time we can see that we already have 1 in the dictionary (see the item in the last but one position. so, we found the pair (9 and 1) the beauty of this solution is we did not iterate the list more than once. now you implement this solution. question 4) i have shown you how to make a dictionary suggestion in the day 07 notes. now i want you to use that knowledge in answering the following question: we want to implement an autocomplete feature that provides suggestions as a user types a phrase in the search field. the autocomplete is like the google search suggests a feature. when you start searching for a keyword in the google search, it suggests a few items to you to make life easier for you. the list we have in mind currently has a product catalog of around 3 million objects and only want to autocomplete the product name. use the products.json in the following link: https://github.com/bestbuyapis/open-data- set that is the bestbuy product names. if you save the products.json on the local desktop and open that by ms excel, you get some idea about its format. this is what it looks like: https://github.com/bestbuyapis/open-data-set https://github.com/bestbuyapis/open-data-set it has a list (i.e. the entire product is in []) that each of its elements is a python dictionary with keys and values. you have seen a similar data model when we tried to model the spotify website in the class. you are just interested to pick the key =name. column c and d sound good candidate to build the dictioney structure. you do not care about the rest. to simplify the solution you can limit the search suggestions to 5 suggestions to users. i have written a code for you to not to worry about loading the keys and values. run this code in the same folder that your json file is located: if you run this code you get this result: as you see in the above screenshot you have repeated names like corliving. please merge them into one dictionary record. for example, one key corliving with more than one value. so you will have a dictionary with a key that may have many values and each value is an item in a list. you know how to search and suggest a python dictionary data structure when you worked on the notes on day_07 dictionary practice 1. use the same technique to answer this question. make="" sure="" your="" code="" does="" not="" break="" if="" the="" string="" is="" empty.="" assume="" we="" do="" not="" give="" a="" negative="" number="" or="" none-numeric="" characters="" to="" the="" program,="" so="" you="" do="" not="" need="" to="" check="" them.="" however,="" they="" can="" be="" equal="" to="" each="" other="" or="" equal="" to="" zero.="" print="" the="" appropriate="" message="" to="" use="" if="" they="" are="" equal.="" question="" 3)="" let’s="" say="" we="" want="" to="" check="" if="" the="" sum="" of="" two="" numbers="" in="" a="" list="" is="" equal="" to="" 10.="" the="" list="" can="" be="" a="" very="" big="" list.="" we="" do="" not="" want="" to="" iterate="" through="" the="" list="" more="" than="" once.="" do="" not="" use="" the="" brute="" force="" method.="" hint:="" let’s="" say="" our="" list="" is="" [3,4,1,2,9].="" in="" a="" brute="" force="" approach,="" you="" can="" check="" the="" first="" item="" (i.e.="" 3)="" with="" all="" the="" other="" items="" in="" the="" list="" and="" if="" that="" is="" 10,="" print="" those="" two="" items.="" otherwise,="" try="" the="" second="" item="" and="" compare="" it="" with="" third,="" fourth="" and="" so="" on.="" you="" have="" to="" do="" this="" comparison="" for="" all="" the="" items="" as="" it="" is="" possible="" no="" two="" items="" in="" the="" list="" add="" up="" to="" 10.="" the="" problem="" is="" if="" the="" list="" has="" n="" list,="" you="" check="" each="" item="" with="" the="" other="" n-1="" items="" in="" each="" iteration.="" since="" you="" have="" n="" items,="" in="" total="" you="" perform="" n(n-1)="" comparisons="" (since="" you="" pick="" one="" of="" the="" n="" numbers="" and="" compare="" that="" with="" the="" remaining="" (n-1)="" numbers).="" but="" the="" question="" asks="" you="" not="" to="" iterate="" through="" the="" list="" more="" than="" once="" (n).="" how="" can="" we="" do="" that?="" you="" should="" use="" a="" more="" innovative="" solution.="" for="" example,="" you="" can="" use="" a="" dictionary="" that="" the="" keys="" are="" the="" list="" items="" and="" the="" value="" is="" 1="" if="" (value="10-item)" does="" not="" exist.="" for="" example,="" for="" a="" list="" like="" this:="" [3,4,1,2,9]="" the="" 10-3(first="" value="" in="" the="" list)="7." we="" do="" not="" have="" 7="" in="" the="" dictionary="" yet="" (we="" just="" started="" to="" populate="" the="" dictionary).="" so="" we="" insert="" the="" 3="" in="" the="" dictionary="" and="" put="" 1="" in="" its="" value="" key="" value="" 3="" 1="" then="" we="" calculate="" 10-4(second="" value="" in="" the="" list)="6." we="" have="" just="" 3="" in="" the="" dictionary,="" so="" we="" go="" ahead="" and="" add="" that="" to="" the="" dictionary="" too:="" key="" value="" 3="" 1="" 4="" 1="" then="" 10-1="9." we="" have="" 3="" and="" 4="" in="" the="" dictionary.="" so,="" let’s="" add="" that="" to="" the="" dictionary:="" key="" value="" 3="" 1="" 4="" 1="" 1="" 1="" the="" next="" one="" is="" 10-2="8" key="" value="" 3="" 1="" 4="" 1="" 1="" 1="" 2="" 1="" now="" we="" come="" to="" the="" last="" item:="" 10-9="1." this="" time="" we="" can="" see="" that="" we="" already="" have="" 1="" in="" the="" dictionary="" (see="" the="" item="" in="" the="" last="" but="" one="" position.="" so,="" we="" found="" the="" pair="" (9="" and="" 1)="" the="" beauty="" of="" this="" solution="" is="" we="" did="" not="" iterate="" the="" list="" more="" than="" once.="" now="" you="" implement="" this="" solution.="" question="" 4)="" i="" have="" shown="" you="" how="" to="" make="" a="" dictionary="" suggestion="" in="" the="" day="" 07="" notes.="" now="" i="" want="" you="" to="" use="" that="" knowledge="" in="" answering="" the="" following="" question:="" we="" want="" to="" implement="" an="" autocomplete="" feature="" that="" provides="" suggestions="" as="" a="" user="" types="" a="" phrase="" in="" the="" search="" field.="" the="" autocomplete="" is="" like="" the="" google="" search="" suggests="" a="" feature.="" when="" you="" start="" searching="" for="" a="" keyword="" in="" the="" google="" search,="" it="" suggests="" a="" few="" items="" to="" you="" to="" make="" life="" easier="" for="" you.="" the="" list="" we="" have="" in="" mind="" currently="" has="" a="" product="" catalog="" of="" around="" 3="" million="" objects="" and="" only="" want="" to="" autocomplete="" the="" product="" name.="" use="" the="" products.json="" in="" the="" following="" link:="" https://github.com/bestbuyapis/open-data-="" set="" that="" is="" the="" bestbuy="" product="" names.="" if="" you="" save="" the="" products.json="" on="" the="" local="" desktop="" and="" open="" that="" by="" ms="" excel,="" you="" get="" some="" idea="" about="" its="" format.="" this="" is="" what="" it="" looks="" like:="" https://github.com/bestbuyapis/open-data-set="" https://github.com/bestbuyapis/open-data-set="" it="" has="" a="" list="" (i.e.="" the="" entire="" product="" is="" in="" [])="" that="" each="" of="" its="" elements="" is="" a="" python="" dictionary="" with="" keys="" and="" values.="" you="" have="" seen="" a="" similar="" data="" model="" when="" we="" tried="" to="" model="" the="" spotify="" website="" in="" the="" class.="" you="" are="" just="" interested="" to="" pick="" the="" key="name." column="" c="" and="" d="" sound="" good="" candidate="" to="" build="" the="" dictioney="" structure.="" you="" do="" not="" care="" about="" the="" rest.="" to="" simplify="" the="" solution="" you="" can="" limit="" the="" search="" suggestions="" to="" 5="" suggestions="" to="" users.="" i="" have="" written="" a="" code="" for="" you="" to="" not="" to="" worry="" about="" loading="" the="" keys="" and="" values.="" run="" this="" code="" in="" the="" same="" folder="" that="" your="" json="" file="" is="" located:="" if="" you="" run="" this="" code="" you="" get="" this="" result:="" as="" you="" see="" in="" the="" above="" screenshot="" you="" have="" repeated="" names="" like="" corliving.="" please="" merge="" them="" into="" one="" dictionary="" record.="" for="" example,="" one="" key="" corliving="" with="" more="" than="" one="" value.="" so="" you="" will="" have="" a="" dictionary="" with="" a="" key="" that="" may="" have="" many="" values="" and="" each="" value="" is="" an="" item="" in="" a="" list.="" you="" know="" how="" to="" search="" and="" suggest="" a="" python="" dictionary="" data="" structure="" when="" you="" worked="" on="" the="" notes="" on="" day_07="" dictionary="" practice="" 1.="" use="" the="" same="" technique="" to="" answer="" this="">
Answered Same DayFeb 24, 2021

Answer To: 1- Make sure you check the inputs and validate them. We check your code with different inputs. In...

Rohith answered on Feb 26 2021
159 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here