Hash Tables: Ice Cream Parlor

  • + 1 comment
    def find_matched_flavors(m, a):
        print "TOTAL MONEY is:  {0}".format(m)
        cost_map = {}
        for i, cost in enumerate(a):
            print "(i, cost) is ({0},{1})".format(i, cost)
            sunny = cost
            johnny = m - cost
            print "prior cost_map {0}".format(cost_map)
            print "johnny, sunny {0}, {1}".format(johnny, sunny)
            if johnny in cost_map.keys():
                print "FOUND MATCHED FLAVORS IN ORDER OFFSET +1:"
                print cost_map[johnny]+1, i+1
            # BUGFIXED:  following else blocks add of NEW cost:flavorIdx if complement already found pre-loaded
            # else:
            # BUG-UNFIXED:  following with OVERWRITE-LOSE prior flavor with cost duplicating CURRENT flavor cost!
            #               eg. (5, 0) gets overwritten with (5, 1)
            #               i.e. complement must be found on FIRST cost map entry; otherwise we lose all history of
            #               that entry when a duplicate cost entry is added!
            cost_map[cost] = i
            print "updated cost_map {0}".format(cost_map)
      
    # MY TEST SCRIPT 
    
    m = 8
    flavorsToCost = [5, 5, 3, 4, 4]
    find_matched_flavors(m, flavorsToCost)