import csv, sys, random #import cgitb cgitb.enable() results = [[],[]] matched = False #MENU for 2011 food_array = [1.25,1.40,1.95,2.5,3.75,4.5,5.50,6.00,5.75,6.25,6.50,6.75,7.25,7.50,7.75,7.95,8.25,8.70,8.5,8.75,8.95,9.25,9.50,9.75,10.25,10.75,10.95,11,11.25,11.5,11.75,11.95,13.75,14.5,16,24] menu = [ ['Soda'], ['White Rice'], ['Chicken with Rice Soup','Yatka Mein','Chicken Noodle Soup','Veg Soup'], ['Egg Drop','Hot & Sour','Wonton'], ['Pan Fried Noodles'], ['Egg Roll'], ['Chicken Wing'], ['CR','Chicken Egg Foo Yong','Pork Egg Foo Yong'], ['Veg Chop Suey', 'Mixed Chinese Veg'], ['Chicken Chop Suey', 'Chicago Chop Suey', 'Ham Fried Rice', 'Veg Chow Mein ', 'Chicago Chow Mein'], ['Beef Chop Suey'], ['Teriyaki Beef', 'Teriyaki Chicken', 'Shrimp Egg Foo Yong', 'Chicken Fingers', 'Fried Jumbo Shrimps', 'Szechuan Veg'], ['House Fried Rice Special','Veg Lo Mein', 'Shrimp Chow Mein', 'Subgum Chicken Chow Mein', 'Chow Mein Cantonese', 'SS Pork', 'Egg Foo Yong House Special'], ['Beef Lo Mein', 'Chicken Lo Mein', 'House Lo Mein Special'], ['Veg Chow Yoke with Pork', 'Beef with Veg', 'Beef with Bean Sprouts', 'S&S Chicken', 'Pepper Steak'], ['Kun Boa Pork', 'Kun Boa Chicken'], ['Moo Shi Pork', 'Moo Shi Chicken', 'Moo Shi Beef', 'Moo Shi Shrimp', 'Shrimp Lo Mein', 'Szechuan Lo Mein'], ['S&S Shrimp'], ['Beef with Broccoli', 'Chicken with Broccoli', 'Pork Mushroom Chow Yoke'], ['Shrimps and Lobster Sauce'], ['GG Chicken', 'Szechuan Beef'], ['Char Sue Snow Peas', 'Shrimp Macao', 'Butterfly Shrimp', 'Hawaiian Beef'], ['Beef with Black Bean Sauce','Beef with Snow Peas'], ['Beef with Oyster Sauce', 'Tidbits'], ['Kun Boa Shrimp'], ['Szechuan Jumbo Shrimps', 'Shrimp Curry'], #10.75 ['Wor Bor'], #10.95 ['Szechuan Three Delights'], #11.00 ['Seafood Curry', 'Shrimps with Mushrooms'],#11.25 ['Kun Boa Three Delights', 'Shrimps with Pea Pods'], #11.50 ['Shrimps with Black Bean Sauce'], #11.75 ['Garlic Shrimps'], #11.95 ['Seafood Wonder'], #13.75 ['Steak Kew','Steak Hong Kong'], #14.50 ['Pu Pu Platter for 2'], ['Pu Pu Platter for 3'] ] drink_array = [5.75, 5.50, 5.50, 5.50, 5.50, 5.50, 5.50, 3.25, 3.25, 3.25, 5.00, 16.00, 4.00, 12.00, 4.00, 2.00, 4.00] drink_menu = [ ['Pina Colada'], ['Mai Tai'], ['Mai Tai'], ['Mai Tai'], ['Mai Tai'], ['Mai Tai'], ['Mai Tai'], ['Beer'], ['Beer'], ['Beer'], ['Johnnie Walker Black'], ['Great Western Champagne'], ['Gin and Tonic', 'Vodka Collins', 'Tom Collins'], ['Scorpion'], ['Pineapple Daiquiri'], ['1/2 shot'], ['Shot'] ] def subset_sum_recursive(numbers,target,partial,fdtype): s = sum(partial) global matched R = [] #print matched #print s if s == target and matched != True: matched = True #results[0].append(partial) #Print out results print_out(target,partial,matched,fdtype) #return #return ("done with this one") #sys.exit("Found the Match!") #while matched != True: elif s > target: return # do nothing elif s < target and matched == False: #print "i am in here" #while s <= target and s!=target: for i in range(len(numbers)): n = numbers[i] remaining = numbers[i+1:] #print n difference = target - s if difference < 0.75 and difference > 0: if len(results) > 4: #print "results" + results break else: #print "i am in small difference " + str(difference) R.append(partial) results.append(partial) return else: #print "i am else" subset_sum_recursive(remaining,target,partial + [n], fdtype) def subset_sum(numbers,target,fdtype): #we need an intermediate function to start the recursion. #the recursion start with an empty list as partial solution. subset_sum_recursive(numbers,target,list(),fdtype) def print_out(tar,menu_made,print_remainder_flag,fdtype): #global food_array #global menu if fdtype == 'food': food_to_print = food_array menu_to_print = menu print 'Food 2010-2011 menu' if fdtype == 'drink': food_to_print = drink_array menu_to_print = drink_menu print 'Drink 2010-2011 menu' print '\n********************************' print 'You Entered:\t$' + str(tar) print '--------------------------------' for i in range(len(menu_made)): menu_index = food_to_print.index(menu_made[i]) print '$' + "%.2f" % food_to_print[menu_index] + '\t' + str(random.choice(menu_to_print[menu_index])) #print 'Liquor:\t' + row[3] remaining = tar - sum(menu_made) #if remaining > 0: if print_remainder_flag == False: print '$' + str(remaining) + '\tRemaining Make it up!' print '--------------------------------' print '$'+"%.2f" % (tar) + '\tSub Total' print '$'+ "%.2f" % (tar*0.0625) + '\tTax' print '--------------------------------' print '$'+ "%.2f" % (tar*1.0625) + '\tTotal' #print '--------------------------------' if __name__ == "__main__": matched = False x='food' v= 15.00 if x == 'food': subset_sum(random.sample(food_array, len(food_array)),float(v),x) if x == 'drink': subset_sum(random.sample(drink_array, len(drink_array)),float(v),x) #subset_sum(food_array[::-1],float(element)) #if matched == True: # return if results and matched == False: #print "results is not empty" matched = False print_out(float(v),random.choice(results),matched,x)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question