# Introduction to Julia course - loops

The course in Julia Academy have the following exercise:

#exercise 4.1
##Loop over integers between 1 and 100 and print their squares

for a in 1:100
A = a^2
end
a

Nothwisthanfd, I did not achieve to reach the towo following exercises:

##Add to the code above a bit to create a dictionary, squares that holds integers and their squares as key, value pairs such that squares[10] == 100
##Use an array comprehension to create an an array squares_arr that stores the squares for all integers between 1 and 100.

Could you aid me to solve this in order to compreend Julia function of dictionaires?

Can you show us the things you tried that didnâ€™t work? It may help us understand where the gaps in your understanding are.

1 Like

Thanks for your help. I initiated with this code, but it brings me a dicionario with only one vector:

## Making A with a_values

``````B = hcat( collect(1:100), A)
B
``````

100Ă—2 Matrix{Int64}:

• 1 1*
• 2 4*
• 3 9*
• 4 16*
• 5 25*
• 6 36*
• 7 49*
• 8 64*
• 9 81*
• 10 100*
• 11 121*
• 12 144*
• 13 169*
• â‹® *
• 89 7921*
• 90 8100*
• 91 8281*
• 92 8464*
• 93 8649*
• 94 8836*
• 95 9025*
• 96 9216*
• 97 9409*
• 98 9604*
• 99 9801*
• 100 10000*

##Making a dictionario

`squares= Dict( collect(1:100) => B[i, :] for i in 1:length( collect(1:100)))`

Dict{Vector{Int64}, Vector{Int64}} with 1 entry: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 â€¦ 91, 92, 93, 94, 95, 96, 97â€¦ => [100, 10000]

#testing
`@assert squares[10] == 100`

Stacktrace:
[1] getindex(h::Dict{Vector{Int64}, Vector{Int64}}, key::Int64)
@ Base .\dict.jl:498
[2] top-level scope
@ In[25]:1

I see you have a lot of the right ideas, just not how to put the ideas together.

I donâ€™t like this second exercise because while the first exercise is a simple for loop, the second exercise calls for a combination of comprehensions, `Dict`s, and `Pair`s all at once.

Would you prefer I give you the answer (and explain it)? Or take you one step at a time through these three concepts? (`Pair`, then comprehension, then `Dict`.)

Edit: That feels like a silly question for me to askâ€¦ I donâ€™t know how to teach well, so Iâ€™ll do the former

``````Dict(n => n^2 for n in 1:100)
``````

Notice that:

1. The `=>` has on its left just the variable `n`. It represents a single number. In your nice attempt, you have the vector of numbers in the right hand side. And whatâ€™s happening is, your `for` loop keeps rewriting what the vector is pointing to on its left hand side.

2. The `=>` has on its right the square number: `n^2`. In your attempt, you have the call to `B[i, :]` which will result in, e.g. for `i = 2`: `B[i, :] == [2, 4]` which means that inside the dictionary, you will have the pair `collect(1:100) => [2, 4]`, when the form you are looking for is `2 => 4` instead.

3. The comprehension I made (the `for` loop inside the `Dict`) is `for n in 1:100`. I see your attempt has `i in 1:length(collect(1:100))`, which is a lot of hard work that can be simplified down. The `length(collect(1:100))` is of value `100` so you can just make that replacement. Then your `for` loop will become `1:100` which becomes like my loop.

4. Notice I havenâ€™t used any `collect`s, you rarely need to use `collect` in Julia, and using the iteration notation like `1:100` everywhere should be fine.

5. And lastly, the dictionary: Note how itâ€™s just a simple call with the comprehension inside.

Now, that second exercise says to add your dictionary creation after the first exerciseâ€™s answer, i.e. use `A` to create the `Dict`. So my provided answer wonâ€™t be the answer to that second exercise, Iâ€™ll let you give it a shot.

Let me know if anything doesnâ€™t make sense.

3 Likes

Maybe also worth mentioning that since they write â€śprint their squaresâ€ť, I think your answer to the first question should probably be something like

``````for a in 1:100
A = a^2
println(A)
end
``````
2 Likes

I had that thought too, thanks for bringing it up. I made the assumption it mightâ€™ve been fine because it seems he passed that first exercise.

But yes, `print` and `println` are invaluable Julia functions for beginners to learn.

1 Like

Thank you @kapple and @DanielVandH to sharing your time and knowledge with me.

3 Likes
2 Likes