2

I am trying to provide a recursive method that provides a list of all the possible combinations when given a list of courses. E.g course = [Entree, Dessert] This is what I have so far:

Entree = ["pumkinsoup","antipasto"]
Dessert = ["cheesecake", "icecream", "tiramisu", "cheeseplatter"]
courses = [Entree, Dessert]

def make_orders(courses):
    dishes_so_far = []
    recursive_make_orders(dishes_so_far, courses)

def recursive_make_orders(dishes_so_far, courses):
    n = len(courses)
    if n==0 :
        print(dishes_so_far)
    else:
        current_courses = courses[0]

        for D in current_courses:
            dishes_so_far.append(D)
            recursive_make_orders(dishes_so_far , courses[1:len(courses)])

\I am trying to make it so that it prints out the combination like [[pumkinsoup,cheesecake],[punkinsoup, icecream]] and etc but its actually giving me [pumkinsoup, cheesecake, icecream] and so on.

Tried adding it with addition instead of append and it gave me an error.

This is homework, so a recursive method is required.

7
  • Please be more detailed in what you want to accomplish. Example output is fine, but not enough. And don't post your attempt to solve it here. It's distracting and not fun to dissect it. I think this explains downvotes. Commented Sep 17, 2013 at 10:26
  • 3
    @ash actually I disagree - while some may find it distracting, a genuine attempt to solve the problem is more than welcome and is in fact encouraged by the community. No one likes "gimme teh codez" questions... Commented Sep 17, 2013 at 10:28
  • @ash: What? I thought mattgemmell.com/2008/12/08/what-have-you-tried was the approach here on SO? Commented Sep 17, 2013 at 10:29
  • You've convinced me. I retract the last part of my comment. Commented Sep 17, 2013 at 10:31
  • 1
    After the line dishes_so_far.append(D) , print courses, current_courses, dishes_so_far to see their contents before you call recursive_make_orders(dishes_so_far , courses[1:len(courses)]) Commented Sep 17, 2013 at 10:51

2 Answers 2

2

You're not too far off - use itertools.product and *courses to unpack to it:

from itertools import product

for course in product(*courses):
  print course

('pumkinsoup', 'cheesecake')
('pumkinsoup', 'icecream')
('pumkinsoup', 'tiramisu')
('pumkinsoup', 'cheeseplatter')
('antipasto', 'cheesecake')
('antipasto', 'icecream')
('antipasto', 'tiramisu')
('antipasto', 'cheeseplatter')
Sign up to request clarification or add additional context in comments.

3 Comments

sorry i should have stated that recursive method needs to be used, according to the question i have been given
@JonClements List comprehension would be probably more pythonic ;)[[entree, desert] for entree in Entree for desert in Dessert]
@JonClements True, although if I remember correctly, itertools have problems of it's own.
1

If you want recursive version, you can do something like this:

def worker(entree, dessert):
    d = []
    if not entree or not dessert: return d

    d.append((entree[0], dessert[0]))
    d += worker(entree[1:], dessert)
    d += worker(entree, dessert[1:])
    return d

Your version is not working as you said because courses now a list of lists, and courses[0] is just Entree, so you recursively constructiong new list from Entree.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.