Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python An API takes in prefix of a username and returns all usernames which star

ID: 3729473 • Letter: P

Question

 Python An API takes in prefix of a username and returns all usernames which start with prefix sorting lexicographically and truncated at 5 results.  Use API call to dump username database by implementing the "extract" function, making calls to any types of "query". 
    
 def extract(query):     """extract takes in a `query` API function (which returns the first 5 usernames, lexicographically sorted,     that start with a prefix) and returns the sorted list of all usernames in the database.      For example, the `query` function in provided in `main` works as follows:          query("a") #=> ["abracadara", "al", "alice", "alicia", "allen"]     query("ab") #=> ["abracadara"]      The following implementation would pass the assertion in `main`, but is not a correct solution since it     works only for that example `query`:      def extract(query):         return query("ab") + query("al") + query("altercation") + query("b") + query("el") + query("ev") + query("m")      Your goal is to write an `extract` method that is correct for any provided `query`.     """     # YOUR CODE HERE     return [...]  def main():     """Runs your solution -- no need to update (except to maybe try out different databases)."""     # Sample implementation of the autocomplete API     database = ["abracadara", "al", "alice", "alicia", "allen", "alter", "altercation", "bob", "element", "ello", "eve", "evening", "event", "eventually", "mallory"]     query = lambda prefix: [d for d in database if d.startswith(prefix)][:5]     assert extract(query) == database  main() 

Explanation / Answer

Python

def extract(query):
   retlist = []
   for word in database:
       if word[0:len(query)] == query:
           retlist.append(word)
       if len(retlist) >= 5:
           break
   return retlist

def main():
   """Run solution"""
   # Sample implementation of the autocomplete API
   sorted(database)   #sorting it would improve the complexity

   print(extract("b"))


#database needs to be global to be accessible
database = ["abracadara", "al", "alice", "alicia", "allen", "alter", "altercation", "bob", "element", "ello", "eve", "evening", "event", "eventually", "mallory"]
main()