Inventory Management, Return the entire content of the inventory

Hi there!

I’v been doing 10 exercises so far, which were solvable more or less easily …
but with at “Inventory Management” I have serious problems.

I cannot solve list_inventory

the code is like that:

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

list_inventory(Dict("coal" => 7, "wood" => 11, "diamond" => 2, "iron" => 7, "silver" => 0))

neither push! nor append! work in the way shown above.

The Error Message is this:

ERROR: MethodError: no method matching append!(::Type{Array{Pair{String, Int8}}}, ::Pair{String, Int64})
The function `append!` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  append!(::BitVector, ::Any)
   @ Base bitarray.jl:786
  append!(::AbstractVector, ::Any)
   @ Base array.jl:1329
  append!(::AbstractVector, ::Any...)
   @ Base array.jl:1331

I think that creation of an empty Array of pairs is not correct and don’t know how do it in the correct way

can you give hints to solve it, please?

happy for any helpful answer or question,

sincerely,

Nisang

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

We have both Int8 and Int64.

Perhaps replace

with

output = []

I quoted that code by having

```julia

above the code, and the same three ` again (without the julia) after the code.

Hi keiraville,

thanks for the fast answer.

yes, of course int 8 and int 64 don’t work together.
that was one desperate try to see, what the message would be (and I didn’t quote the correct error message).

with the propper int64 the message before was of course:

[code]

typeof(output) = UnionAll
ERROR: MethodError: no method matching append!(::Type{Array{Pair{String, Int64}}}, ::Pair{String, Int64})
The function append! exists, but no method is defined for this combination of argument types.

Closest candidates are:
append!(::BitVector, ::Any)
@ Base bitarray.jl:786
append!(::AbstractVector, ::Any)
@ Base array.jl:1329
append!(::AbstractVector, ::Any…)

[/code]

I changed the code according to your suggestions…
and now I at least have:

a different typeof(output) = Vector{Any}
and a different error message:

[code]

typeof(output) = Vector{Any}
ERROR: MethodError: no method matching isless(::Int64, ::String)
The function isless exists, but no method is defined for this combination of argument types.

Closest candidates are:
isless(::Missing, ::Any)

[/code]

one step further!
let’s see what this might mean.

with

[code]
output = Array{Pair{String, Int64}}[]
[/code]

I have
typeof(output) = Vector{Array{Pair{String, Int64}}}

I gues it should be Vector{{Pair{String, Int64}} or
Array{Pair{String, Int64}}
before it was:
::Type{Array{Pair{String, Int64}}}

what do you think?

Do you have any idea?

sincerely Nisang

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