Stuck with code please help


#1

Hello META RUBY,
Can somebody please explain the following lines:

if counts[counts_idx][0] == letter
  counts[counts_idx][1] += 1

I actually don’t understand the notation:

counts[counts_idx][0] 

I understand this part - counts[counts_idx] - which refers to a an element in the array counts at the location [counts_idx] but I don’t get the [0] or [1] at the end.

Thank you,
V/r,
MC


#2

We can’t see what counts looks like, but from the code you posted we can assume that you have a 2D array, also known as multi-dimensional array.

A multi-dimensional array has multiple dimensions or layers if you prefer.

Every time you use a [0], where 0 can be any valid number, you are stripping off one of the layers, so if you do [0][0] you are going two layers deep.

What’s happening in this example is that you are trying to count letters.

Every letter has it’s own place inside the outtermost layer of this multi-layer array. The letter itself is stored on position 0 (that’s why you see [0]), and the count is stored on position 1.

Hope that helps!


#3

Hello Jesus,
That actually does help a lot thank you so much. I don’t have experience with multidimensional arrays so I am trying to learn as much as possible from this code. Here’s the whole thing. In case you had anything else to add. But again thank you for you explanation.
Cheers.


# the string contains only lowercase letters. Count the number of
# letters that repeat, not the number of times they repeat in the
# string.
#
# Difficulty: hard.

def num_repeats(string)
  counts = []

  str_idx = 0
  while str_idx < string.length
    letter = string[str_idx]

    counts_idx = 0
    while counts_idx < counts.length
      if counts[counts_idx][0] == letter
        counts[counts_idx][1] += 1
        break
      end
      counts_idx += 1
    end

    if counts_idx == counts.length
      # didn't find this letter in the counts array; count it for the
      # first time
      counts.push([letter, 1])
    end

    str_idx += 1
  end

  num_repeats = 0
  counts_idx = 0
  while counts_idx < counts.length
    if counts[counts_idx][1] > 1
      num_repeats += 1
    end

    counts_idx += 1
  end

  return num_repeats
end

# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #num_repeats")
puts("===============================================")
    puts('num_repeats("abdbc") == 1: ' + (num_repeats('abdbc') == 1).to_s)
    # one character is repeated
    puts('num_repeats("aaa") == 1: ' + (num_repeats('aaa') == 1).to_s)
    puts('num_repeats("abab") == 2: ' + (num_repeats('abab') == 2).to_s)
    puts('num_repeats("cadac") == 2: ' + (num_repeats('cadac') == 2).to_s)
    puts('num_repeats("abcde") == 0: ' + (num_repeats('abcde') == 0).to_s)
puts("===============================================")


#4

I know Jesus has already answered your question, I just wanted to make mention that (to me) the easiest way to think about what you’re seeing is “an array within an array”.

The main reason I think about it this way is because in dynamic languages like Ruby, you’re not forced into your array structure. What I mean is it’s perfectly valid to have something like this:

x = ["Hello", ["from", "planet", "earth"]]

In which case x[0] would be a string, but x[1] would be an array. Hence: array within array :stuck_out_tongue:


#5

@ncrause That was great. Thanks