The course in Julia Academy have the following exercise:

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

I have made the code:
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?

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, Dicts, and Pairs 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

One way of getting your answer is:

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

Notice that:

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.

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.

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.

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

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.

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