Julia Set Exercise About Catering

I’ve gotten everything passing except for the singleton_ingredients method. Based on my understanding of what we need to do, I’ve written the following code:

function singleton_ingredients(dishes, intersection)
    ingredient_compilation = compile_ingredients(dishes)
    singletons = symdiff(dishes[1], ingredient_compilation)
    for i in 2:length(dishes)
        singletons = union(singletons, symdiff(dishes[i], ingredient_compilation))
    end
    return singletons
end

I am not sure what I’m doing wrong here. I’m not getting any compilation or runtime errors, but I’m not getting the correct answers for any of the test cases for this method.

I don’t yet have a full answer to this, but I’m a bit worried that you never use the intersection argument. That is an immediate clue that the set logic is based on a misunderstanding.

Maybe think about using setdiff(dish, intersection) in there somewhere?

Slightly off-topic, but useful: looping over the index is rare in Julia. All you need is for dish in dishes, and it becomes easier to read and debug.

The author of this exercise is away at the moment. Once they’re back, we’ll discuss whether we need to make the documentation a bit clearer (either instructions or hints).

Good luck, and thank you for trying the new Julia syllabus.

The Python version of this exercise has an explanation of the issues involved in taking the symmetric difference of more than two sets. It might be useful to include some of it for the Julia exercise as well (apologies for the Python-only example):


A symmetric difference of more than two sets will result in a set that includes both the elements unique to each set AND elements shared between more than two sets in the series (details in the Wikipedia article on symmetric difference).

To obtain only items unique to each set in the series, intersections between all 2-set combinations need to be aggregated in a separate step, and removed:

>>> one = {'black pepper','breadcrumbs','celeriac','chickpea flour',
           'flour','lemon','parsley','salt','soy sauce',
           'sunflower oil','water'}

>>> two = {'black pepper','cornstarch','garlic','ginger',
           'lemon juice','lemon zest','salt','soy sauce','sugar',
           'tofu','vegetable oil','vegetable stock','water'}

>>> three = {'black pepper','garlic','lemon juice','mixed herbs',
             'nutritional yeast', 'olive oil','salt','silken tofu',
             'smoked tofu','soy sauce','spaghetti','turmeric'}

>>> four = {'barley malt','bell pepper','cashews','flour',
            'fresh basil','garlic','garlic powder', 'honey',
            'mushrooms','nutritional yeast','olive oil','oregano',
            'red onion', 'red pepper flakes','rosemary','salt',
            'sugar','tomatoes','water','yeast'}

>>> intersections = (one & two | one & three | one & four | 
                     two & three | two & four | three & four)
...
{'black pepper','flour','garlic','lemon juice','nutritional yeast', 
'olive oil','salt','soy sauce', 'sugar','water'}

# The ^ operation will include some of the items in intersections, 
# which means it is not a "clean" symmetric difference - there
# are overlapping members.
>>> (one ^ two ^ three ^ four) & intersections
{'black pepper', 'garlic', 'soy sauce', 'water'}

# Overlapping members need to be removed in a separate step
# when there are more than two sets that need symmetric difference.
>>> (one ^ two ^ three ^ four) - intersections
...
{'barley malt','bell pepper','breadcrumbs', 'cashews','celeriac',
  'chickpea flour','cornstarch','fresh basil', 'garlic powder',
  'ginger','honey','lemon','lemon zest','mixed herbs','mushrooms',
  'oregano','parsley','red onion','red pepper flakes','rosemary',
  'silken tofu','smoked tofu','spaghetti','sunflower oil', 'tofu', 
  'tomatoes','turmeric','vegetable oil','vegetable stock','yeast'}
1 Like