Please complete this question on your preferred IDE and send me back a link with your code and answer. # In this problem, you are given a set of house listings and your job is to return a list of the most recent listing of each address. # There could be multiple listings of the same address, and you must ensure your ouput contains only one listing per address (the most # recent one). A listing contains a name, address, and date. # For example: # Input: # L4, 123 kings road,2022 # L1, 123 kings road,2020 # L2, 20 queen road,1990 # L3, 20 queen road,1992 # Expected Output: # (L4, L3) # In the above example, there are two listings with the same address "123 kings road", and two listing with the same address "20 queen road". # The output includes the most recent listing with the address "123 kings road" (2022 > 2020) which is L4, and the most recent listing with # the address "20 queen road" (1992 > 1990) which is L3, hence (L4, L3).
Anonymous
listings = {"L4, 123 kings road,2022", "L1, 123 kings road,2020", "L2, 20 queen road,1990", "L3, 20 queen road,1992"} dict = {} #iterate through the set for item in listings: #split the the string 'item' into a list (listing #, address, date) lst = item.replace(" ", "").split(",") #Check if the address already exists in the dictionary #if true compare the dates if lst[1] in dict.keys(): #Compare the dates if lst[2] > dict.get(lst[1])[1]: dict[lst[1]] = (lst[0], lst[2]) #if false add a new key else: dict[lst[1]] = (lst[0], lst[2]) #create a list latestListings = [] for key in dict.keys(): latestListings += [dict.get(key)[0]] print(latestListings)
Check out your Company Bowl for anonymous work chats.