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

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?

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

KeyError: key 10 not found

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, 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 :sweat_smile:

One way of getting your answer is:

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 collects, 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. :blush:

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

PS. See PSA: how to quote code with backticks

2 Likes