Part A
query=input("query:") for quote in data_list: found_at = quote.lower().find(query.lower()) if( found_at >= 0): print("Found:", "..."+quote[found_at:found_at+50], "...")
Part B
query=input("query:") terms = query.split(" ") for quote in data_list: find_counter = 0 for term in terms: found_at = quote.lower().find(term.lower()) if(found_at >= 0): find_counter+=1 if find_counter == len(terms): print("Found:", quote)
Part C
query=input("query:") terms = query.split(" ") no_results = True for quote in data_list: find_counter = 0 for term in terms: found_at = quote.lower().find(term.lower()) if(found_at >= 0): find_counter+=1 if find_counter == len(terms): print("Found:", quote) no_results = False if(no_results): print("No results found")
Part D
query=input("query:") #assume that user will not put AND at the beginning of the sentence isAND = query.find("AND") #brute force if-else #AND search if isAND > 0: #assume user always puts AND with a space before and after terms = query.split(" AND ") no_results = True for quote in data_list: find_counter = 0 for term in terms: found_at = quote.lower().find(term.lower()) if(found_at >= 0): find_counter+=1 if find_counter == len(terms): print("Found:", quote) no_results = False if(no_results): print("No results found") #OR search else: terms = query.split(" OR ") no_results = True for quote in data_list: find_counter = 0 for term in terms: found_at = quote.lower().find(term.lower()) if(found_at >= 0): print("Found:", quote) no_results = False if(no_results): print("No results found")