So, it appears to me that my function does what it is supposed to do, but that the extra == cart == bit in the middle prevents me from passing the test. Is that so? And if yes, what can I do?
It looks like the test wants you to update the cart dictionary itself and that your code creates a new dictionary instead of updating the existing dictionary.
Thanks! I see, though I still find it non-obvious, as I use the same cart variable the whole time. It seems I also have to use functions (different than the mergewith) that update rather than produce new stuff. I will try it out.
Well, the code below works, but I like it a lot less, and I still wonder what the essential difference is. In both cases I take the cart variable, do something with it, and return it. Why is one an update and the other not?
function additems!(cart, items)
for i in items
if haskey(cart,i)
cart[i] += 1
else
cart[i] = 1
end
end
cart
end
Thank you for trying this new exercise, which was just added 3 days ago.
Note that the function name is additems!, not additems. In Julia (and several other languages) there is a strong convention that:
Most functions do not mutate their inputs.
Mutating functions have names ending with !, as a warning to anyone reading your code.
Sometimes there are reasons why we need to mutate (as here), but non-mutating code is safer and easier to reason about. This will be especially true when running code in parallel on multiple threads: common in real-world Julia, though not yet enabled on the test runner.
If you don’t like your current code, you might want to investigate some alternatives:
foreach() as a simpler way of looping.
A function, mentioned in the Introduction, which will either return a value by key, or a default value if the key is missing.
If you want to use mergewith(), the mutating version is mergewith!(). The Julia standard library provides both options.
It sounds like you’re confusing objects with variables.
a = Dict(1 => "One")
This creates a “One” dictionary and stores a reference to it in a.
b = a
This stores a reference to the same dictionary in b. Now both a and b point to the same dictionary.
Note that two variables are references the same one dictionary.
a = Dict(2 => "Two")
This creates a new “Two” dictionary and stores a reference to it in a. Now a references “Two” and b references "One`.
Note that the one a variable has references two different dictionaries. While it’s the same variable, the objects are different objects. This is what your initial solution did – it stored different dictionaries in the same cart variable. It did not update the original object.
Thanks! Adding the exclamation mark to mergewith() was one way to pass the test, and I am very happy with that. I will also make sure to consider foreach() next time I feel my loops are inefficient.
Related issue: I just found a problem with the stub for update_store_inventory(), which has the arguments accidentally reversed. PR to fix it going in now…
It should be update_store_inventory(inventory, cart) because this is what the tests expect.
Apologies especially to @alterpatzer, who managed to get a community solution working despite the argument mismatch in the stub. I guess with some trial and error…
Looking at the community solutions would be a good way to spend a couple of minutes (if you’ve not already done so). There are only 4, but already quite a variety of approaches.