import csv, sys, random #import cgitb cgitb.enable() results = [[],[]] matched = False #MENU for 2011 food_array = [1.5,1.75,2.25,2.5,3.95,4.75,5.75,6.25,6.5,6.75,6.95,7.35,7.5,7.75,7.95,8.25,8.5,8.75,8.95,9.5,9.75,9.95,10.5,10.75,10.95,11,11.25,11.5,11.75,11.95,14.25,14.5,16] menu = [ ['Soda'], ['Steamed Long Grain Rice'], ['Chicken with Rice Soup','Ya Ka Mein','Chicken Noodle Soup','Veg Soup'], ['Egg Drop','Hot & Sour','Wonton'], ['Pan Fried Noodles'], ['Egg Roll'], ['CW'], ['CR', 'Veg Chop Suey', 'Mixed Chinese Veg', 'Chicken Egg Foo Yong', 'Pork Egg Foo Yong', 'Subgum Egg Foo Yong'], ['Pork Chop Suey', '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', 'Pork Lo Mein', 'House Lo Mein Special'], ['Veg Chow Yoke with Pork', 'Beef with Veg', 'Beef with Bean Sprouts', 'SS Chicken'], ['Kun Boa Pork', 'Kun Boa Chicken', 'Chicken Curry', 'Pepper Steak'], ['Moo Shi Pork', 'Moo Shi Chicken', 'Moo Shi Beef', 'Moo Shi Shrimp', 'Shrimp Lo Mein', 'Szechuan Lo Mein'], ['SS Shrimp', 'Shrimps and Lobster Sauce', 'Beef with Broccoli', 'Chicken with Broccoli', 'Pork Mushroom Chow Yoke', 'Szechuan Chicken'], ['GG Chicken', 'Szechuan Beef', 'Roast Pork with Veg', 'Orange Chicken', 'Sesame Chicken', 'Kun Boa Beef'], ['Char Sue Snow Peas', 'Shrimp Macao', 'Butterfly Shrimp', 'Hawaiian Beef', 'Subgum Shrimp Kew'], ['Beef with Black Bean Sauce', 'Orange Beef'], ['Beef with Snow Peas', 'Beef with Oyster Sauce', 'Beef and Mushroom Chow', 'Tidbits'], ['Kun Boa Shrimp'], ['Szechuan Jumbo Shrimps', 'Shrimp Curry'], ['Wor Bor', 'Beef with Black Mushrooms'], ['Szechuan Three Delights'], ['Seafood Curry'], ['Shrimps with Mushrooms'], ['Kun Boa Three Delights', 'Shrimps with Pea Pods', 'Chicken Superb'], ['Shrimps with Black Bean Sauce', 'Garlic Shrimps'], ['Seafood Wonder'], ['Gai Poo Lo Mein', 'Steak Kew', 'Steak Hong Kong', 'Dragon Delight', 'Broil Sirloin Steak'], ['Pu Pu Platter'] ] drink_array = [5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 5.75, 3.25, 3.25, 3.25, 6.00, 16.00, 5.00, 13.00, 4.25, 2.00, 4.00] drink_menu = [ ['Mai Tai', '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 2012 menu' if fdtype == 'drink': food_to_print = drink_array menu_to_print = drink_menu print 'Drink 2012 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