Your indentation is wack. Make sure you’re not mixing spaces and tabs.
2 spaces per indent. You’re mixing 2 & 3 & possibly tabs at 8.
Your variable names are meaningless. Improve that and you’ll have a
lot better time debugging when it is 4am and you’re tired.
You’re not using (sensible) iterators. I suggest you read that section
in the pickaxe book.
You’re not using good data structures or algorithms. You’re passing
over the elements of ‘a’ (see? better names means better readability!)
a.uniq.size times and doing a.size * a.uniq.size string equality
comparisons.
Take a look at this:
use a hash whose values default to 0 for proper data structure use
use count as the name, since that’s what it is storing.
count = Hash.new 0
iterate over each string only once
strings.each do |string|
increment the count for each string
count[string] += 1
end
p count
If you nuke all the comments, the code makes just as much sense (if
not more). That’s what we try to achieve in ruby whenever we can.
Your indentation is wack. Make sure you’re not mixing spaces and tabs.
2 spaces per indent. You’re mixing 2 & 3 & possibly tabs at 8.
Sorry about that. I’m writing ruby in a virtual Windows on a Linux host.
Formatting got a bit corrupted when copying the code. I’ll watch out for
that next time.
Your variable names are meaningless. Improve that and you’ll have a
lot better time debugging when it is 4am and you’re tired.
Sorry. I just did that to make the example easier. In reality they are
called things like ‘@had_lessons_all’ and ‘@had_lessons_number’.
You’re not using (sensible) iterators. I suggest you read that section
in the pickaxe book.
Good tip. I will do that now.
You’re not using good data structures or algorithms.
No kidding
count = Hash.new 0
iterate over each string only once
strings.each do |string|
increment the count for each string
count[string] += 1
end
p count
Thanks for that. I will try to rewrite my method (for the sake of
learning) in a neater way, using the technique you suggest.
Hello,
Â
I am new to ruby and the rails and it seems that it will not run or open
to let me see if I have install it. is there amy way that you can help
me.
james
From: Fleck Jean-Julien [email protected]
Subject: Re: Count the number of times an element occurs in an array
To: “ruby-talk ML” [email protected]
Date: Monday, October 5, 2009, 3:43 AM
use a hash whose values default to 0 for proper data structure use
use count as the name, since that’s what it is storing.
count = Hash.new 0
iterate over each string only once
strings.each do |string|
increment the count for each string
count[string] += 1
end
p count
If you nuke all the comments, the code makes just as much sense (if
not more). That’s what we try to achieve in ruby whenever we can.
Hi Ryan,
Don’t know if you’ll be reading this, but wanted to say thanks anyway.
After reading the section you suggest from Pickaxe I came up with this,
which is not perfect, but definitely an improvement:
array = [“a”, “b”, “a”, “b”, “b”]
count =[]
for i in 0 … array.uniq.length
temp_count = 0
array.collect{|elem| temp_count +=1 if elem == array.uniq[i]}
count << temp_count
end
p array.uniq, count
Then I worked through your code and realized what a concise solution you
had proposed. I have changed my programme accordingly and it is now
considerably shorter and easier to read.
Thanks again and thanks to everyone else who made suggestions too.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.