Hash Tables: Ice Cream Parlor

  • + 0 comments

    Python3

    def whatFlavors(cost, money):
        mapping = {}
        
        for i in range(len(cost)):
            current_price = cost[i]
            if current_price in mapping.keys():
                if mapping[current_price] < i + 1:
                    # Print complement price index first
                    print("{} {}".format(mapping[current_price], i + 1))
                else:
                    print("{} {}".format(i + 1, mapping[cost[i]]))
            # Map current price index to complement price
            mapping[money - current_price] = i + 1