Inventory Management, Return the entire content of the inventory

To be honest I’m not sure, what that means

See Posting code or preformatted text - Using Discourse - Discourse Meta

it works:

the solution is:

function list_inventory(inventory)
    output = Pair{String, Int64}[]
#    output = []
            @show typeof(output)
    for (key, value) in inventory
        if value == 0
            delete!(inventory, key)
        else
            @show("append ",key => value)
            push!(output, key => value)
#            append!(output, [key => value])
        end
    end
    sort(output)
end

Thanks for your input…
finally got it!

uufff!

The issue you were originally having:

output = Array{Pair{String, Int64}}

Here, Array{Pair{String, Int64}} is a type, not an instance of an array. You have defined output as an alias to the type.

Julia is saying that it doesn’t make sense to append a value to a type.

BTW, I edited your posts to properly quote the code

thanks for reediting the markdown part!
… while still looking for it how it is done! ;-)

1 Like

like this:

```julia
here is some Julia code
```

A question for you: do you need to modify the input inventory?

thanks for the explanation again
before I tried
output = Array{Pair{String, Int64}}
what also didn’t work

Well, Values of zero shouldn’t be considered

Why?

The purpose of the function is to build up the output vector. Deleting from the input inventory doesn’t really get you closer to that goal.

Plus, it’s bad practice to modify the input. The user won’t expect that their inventory will suddenly be missing some keys after calling an innocent “list” function.

$ julia
...
julia> function myfilter(inventory)
         for (key, value) in inventory
           if value == 0
             delete!(inventory, key)
           else
             @show((key, value))
           end
         end
       end
myfilter (generic function with 1 method)

julia> inventory = Dict("a"=>1, "b"=>0, "c"=>3)
Dict{String, Int64} with 3 entries:
  "c" => 3
  "b" => 0
  "a" => 1

julia> myfilter(inventory)
(key, value) = ("c", 3)
(key, value) = ("a", 1)

julia> inventory
Dict{String, Int64} with 2 entries:
  "c" => 3
  "a" => 1
2 Likes

Mutating inputs was recently discussed on another thread. To repeat the main points:

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.

I strongly recommend that you look at the Community Solutions after finishing the exercise. Julia provides very efficient ways to implement this function in a single line, and an important aim of the syllabus is to help you learn about them.

Good luck!

Dear Collin,

thanks for your answer and your reminder.
I already changed that as I also thought this might be problematic…
while being on a walk.

I’m really happy that you answered to me (as the creator of this exercise).
:slight_smile:

Well I just saw you already read about updating and expanding the
explanations of the exercise

I hope that you don’t feel critisized!

I would be very happy if you think about that

sincerely,

Nisang
strong text I just saw: all of the exercises I did so far are from you!
thanks a lot for your engagement!!

2 Likes

I wrote most of the concepts. Creating the exercises was shared with @depial.

We’ll certainly look into clarifying the documentation, as this is the second time recently there has been some confusion about mutating inputs.

1 Like

Well my problem was not about mutating inputs (what I realized in nature is not a good idea,
but because the concept of variable declaration is relatively different from the one I was use to
an some concepts needed to solve the exercise are missing (as you read in the other thread I wrote)…

but it’s your choice!

not to forget: I like most of the exercises and how the stories are put around them.
It makes much more fun to solve them because of the way they are!

thanks

and have a good night,

Nisang

2 Likes

I can see what @AnandNisang means, since we don’t have a concept which explicitly covers sorting and related functions. I think there’s plenty of material here for a potential concept, so we could look into doing that in the future now that the syllabus is filling out.

Along those lines, we could see about doing a concept on mutating functions, which may be good as a separate concept since we could focus on why the default is not to mutate as well as actual functions, but I could also see it being a mention in the proposed sorting concept too.

I’ll open an issue related to this.

3 Likes

Dear depial,

thanks for that.

I feel “having been listened to me”. ;-)

Today I talked to a friend who is coding in Julia for “ages” and we found more conceptional stuff which is not explained prior to the exercises and which seems to be crucial to us.

For completeness I would like to outline them here…
and also to please you to add them to your concept.

If you’d like I can also take over this part (in a couple of weeks) as I develop my Julia skills.

these are:

  • basics declaration of local variables (Variable scope, mem-allocation, errors, …) and consequences of different allocation
  • basic declaration of arrays, multi dimensional arrays, arrays of dicts, creating an empty Array (of Pairs/Dicts). when to use or ()
  • how to sort
  • can delete be used for both Pairs and Arrays or only for Arrays

one more suggestion is:

  • re engineer “Inventory Management” to reuse the created inventory of the recent functions in the further functions

thanks ahead

1 Like

Thanks for the suggestions! As the syllabus is still in beta, we’re happy to get feedback. I’ll respond to your suggestions in turn to see if I’ve understood everything correctly.

basics declaration of local variables (Variable scope, mem-allocation, errors, …)

I believe we’ve talked about simple declaration of variables in the Basics concept, but variable scope is likely a good topic for a separate concept, as it has it’s own section in the manual. I’ll add this to an issue on the repo, to see if we can make a concept on it.

I believe talking about memory allocation is, at this point, is not strictly necessary, since (if I’ve understood your point) the exercise does not require explicit knowledge of memory management. This could make up part of another concept however.

basic declaration of arrays, multi dimensional arrays…

I noticed that we’ve not explicitly mentioned how to initialize an empty array in the Vectors concept (e.g. output = []). I believe we can modify the docs to include that information, and I’ll open a PR to do so soon.

That said, we want to be careful not to overload students with things like type declarations at this point, since that is covered in a later concept. So, we don’t expect students to write something like output = Pair{String, Int64}[] at this stage. Also Multidimensional Arrays is a later concept which already exists.

I think that preallocation of vectors (e.g. Vector{Pair}(undef, n)) is also beyond the scope of this exercise.

how to sort

We’ve opened an issue to create a concept on sorting and related functions. So it is already in the works.

can delete be used for both Pairs and Arrays or only for Arrays

delete!() can only be use with collections that use a key (e.g. Dict, Set). There is a similar function for Vectors called deleteat!() which uses the index (these differ because keys in a Dict are unique while entries in a Vector need not be). As we are introducing the method specifically in regards to Dicts, mentioning Vectors would be out of the scope of the exercise.

That said, I can understand your confusion in that we didn’t name the function remove_item() with a !, thus it may be assumed that the inventory should not be mutated, thereby excluding the use of delete!(). This exercise was designed to be “mutation agnostic” (i.e. tests will pass with mutating or non-mutating functions) to keep things “simple”, but I believe it would be best to change the name of the function to remove_item!() to avoid this confusion, so I’ll include that in a future PR.

re engineer “Inventory Management” to reuse the created inventory…

While this structure is followed in many exercises, we may sometimes deviate from that to keep students from becoming overly comfortable with a single exercise format. Here, as an alternative, we could imagine the functions in this exercise as composing a module of independent functions which can then be called to act on inventories, or something to that effect (FYI: Modules is a concept currently under discussion).


I hope I’ve address your concerns here, and I’ll try to get to making the changes I’ve mentioned soon, but please let me know if I’ve missed something or misunderstood something.

1 Like

We’ve agreed that this concept is needed, and I ought to write it. Now I just need to get my old, tired brain fired up and actually write it. Let’s hope…

2 Likes

Dear Depial,

thanks for your detailed answer which is much much more than I could have ever expected!!

I agree in most of the cases

And find the idea of being

very good and very convincing.

but without
output = Array{Pair{String, Int64}}
it didn’t work…
I had permanent errors.
is there a simpler way of doing it?

I guess the sorting and related function is not such a big think… I just mentioned it to complete the explanations.

As I remember the

delete!

function can also be used on pairs (which user a key)

I can also agree to your explanations about having alternatives

So thanks again and have a nice evening and a good time!

with kind regards,

Nisang

You’re most welcome :)

In regards to:

I would guess there is another issue I’m not aware of without seeing what error was thrown. If you can share the error, it would help me debug. If it is related to the error you listed above:

ERROR: MethodError: no method matching append!(::Type{Array{Pair{String, Int8}}}, ::Pair{String, Int64})

I can see an couple of issues. However, I believe the main one is related to how you are using append!() in your code (it looks like it’s being used like push!()). However you’ve corrected that in your example later on.

I used your working example for list_inventory() from above and put it in my solution in place of my function and everything worked using just output = []. More specifically, the following version passes the tests in my solution:

function list_inventory(inventory)
    output = []
            @show typeof(output)
    for (key, value) in inventory
        if value == 0
            delete!(inventory, key)
        else
            @show("append ",key => value)
            push!(output, key => value)
#            append!(output, [key => value])
        end
    end
    sort(output)
end

So there may be an issue elsewhere?

As I remember the delete!() function can also be used on pairs (which uses a key)

I would recommend you go back and read the introduction in a bit more detail. Pairs are distinct from Dicts and keys are mentioned in there somewhere.