# Sort elements

Hello, I’m newbie so I apologize if my question it’s stupid. I want to
write a program that counts how many times a word appears in a text.
This is my code:

a = ‘Apple car caR house tree ice ice ice house’
b = a.downcase.split(’ ')
b.uniq.each do |element|
puts “#{b.count(element)}\t#{element}”
end

But this code produces this:

1 apple
2 car
2 house
1 tree
3 ice

and I want something like this:

3 ice
2 car
2 house
1 apple
1 tree

Any ideas?

On Mar 23, 2010, at 02:47 , Juan Gf wrote:

2 car
1 apple
1 tree

Any ideas?

Read aloud what the code says, translated to natural language (English
or otherwise, doesn’t matter… just raise it to human thought level).
Then say aloud what you want it to do, step by step. What’s the
difference? Translate that difference back down to code.

Ryan D. wrote:

On Mar 23, 2010, at 02:47 , Juan Gf wrote:

2 car
1 apple
1 tree

Any ideas?

Read aloud what the code says, translated to natural language (English
or otherwise, doesn’t matter… just raise it to human thought level).

CONVERT THE TEXT IN LOWER-CASE AND THEN SPLIT THE TEXT INTO SINGLE
WORDS! THEN COUNT HOW MANY TIMES EVERY SINGLE WORD APPEARS!

Then say aloud what you want it to do, step by step.

CONVERT THE TEXT IN LOWER-CASE AND THEN SPLIT THE TEXT INTO SINGLE
WORDS! THEN COUNT HOW MANY TIMES EVERY SINGLE WORD APPEARS! THEN SORT
THE RESULTS: FIRST THE MORE COMMON WORDS AND AFTER THE LESS COMMON WORDS
FINALLY, BLOODY COMPUTER, BRING ME A PIZZA!

What’s the difference?

the difference is “THEN SORT THE RESULTS: FIRST THE MORE COMMON WORDS
AND AFTER THE LESS COMMON WORDS FINALLY BLOODY COMPUTER BRING ME A
PIZZA!”

Translate that difference back down to code.

I tried to use .sort like this:

“b.uniq.each do |element|
puts “#{(b.count(element)).sort}\t#{element}”
end”

but obviously it doesn’t work.

Ryan, thanks for your time and excuse for the “pizza joke” (is a bad
joke no doubt)

On Tue, 23 Mar 2010 19:35:02 +0900
Juan Gf [email protected] wrote:

(English or otherwise, doesn’t matter… just raise it to human
WORDS FINALLY, BLOODY COMPUTER, BRING ME A PIZZA!

“b.uniq.each do |element|
puts “#{(b.count(element)).sort}\t#{element}”
end”

but obviously it doesn’t work.

Ryan, thanks for your time and excuse for the “pizza joke” (is a bad
joke no doubt)

I would create a new array that contains the number of elements found:

b.uniq.map { |uw| [b.count(uw), uw] }
=> [[1, “apple”], [2, “car”], [2, “house”], [1, “tree”], [3, “ice”]]

Then sort it:

b.uniq.map { |uw| [b.count(uw), uw] }.sort_by { |e| e[0] }
=> [[1, “apple”], [1, “tree”], [2, “car”], [2, “house”], [3, “ice”]]

…finally reverse and print:

b.uniq.map { |uw| [b.count(uw), uw] }.sort_by { |e|
e[0] }.reverse.each{ |e| puts “#{e[0]} #{e[1]}” }
3 ice
2 house
2 car
1 tree
1 apple
=> [[3, “ice”], [2, “house”], [2, “car”], [1, “tree”], [1, “apple”]]

Use your phone to get a pizza…

Another approach:

irb(main):001:0> s = “car car ice ice house ice house tree”
=> “car car ice ice house ice house tree”
irb(main):002:0> h = Hash.new(0)
=> {}
irb(main):006:0> s.split.each {|x| h[x] += 1}
=> [“car”, “car”, “ice”, “ice”, “house”, “ice”, “house”, “tree”]
irb(main):007:0> h
=> {“ice”=>3, “house”=>2, “car”=>2, “tree”=>1}
irb(main):008:0> h.sort_by {|k,v| -v}
=> [[“ice”, 3], [“house”, 2], [“car”, 2], [“tree”, 1]]
irb(main):009:0> h.sort_by {|k,v| -v}.each {|k,v| puts “#{v} #{k}”}
3 ice
2 house
2 car
1 tree

With the uniq and the count you are traversing the array many times.

Jesus.

Thank you Martin! I’m learning Ruby and I love it, Thank you Martin and
Ryan again

Thank you JesÃºs & Gavin, I now have enough concepts for studying this
week!!! pretty amazing how many different ways of doing the same thing

On Mar 23, 3:47 am, Juan Gf [email protected] wrote:

Hello, I’m newbie so I apologize if my question it’s stupid. I want to
write a program that counts how many times a word appears in a text.

Here’s another variation, just for the learning experience:

irb(main):001:0> s = “car car ice ice house ice house tree”
=> “car car ice ice house ice house tree”

irb(main):002:0> words = s.scan /\w+/
=> [“car”, “car”, “ice”, “ice”, “house”, “ice”, “house”, “tree”]

irb(main):003:0> groups = words.group_by{ |word| word }
=> {“car”=>[“car”, “car”], “ice”=>[“ice”, “ice”, “ice”],
“house”=>[“house”, “house”], “tree”=>[“tree”]}

irb(main):005:0> counted = groups.map{ |word,list|
[list.length,word] }
=> [[2, “car”], [3, “ice”], [2, “house”], [1, “tree”]]

irb(main):007:0> sorted = counted.sort_by{ |count,word| [-
count,word] }
=> [[3, “ice”], [2, “car”], [2, “house”], [1, “tree”]]

irb(main):008:0> sorted.each{ |count,word| puts “%d %s” % [ count,
word ] }
3 ice
2 car
2 house
1 tree
=> [[3, “ice”], [2, “car”], [2, “house”], [1, “tree”]]

Of course you don’t need all those intermediary variables if you don’t
want them and don’t need to debug the results along the way:

s.scan(/\w+/).group_by{|w| w }.map{|w,l| [l.length,w] }.sort_by{ |c,w|
[-c,w] }.each{ |a| puts “%d %s” % a }

But I’d really do it the way Jesús did.

On Mar 23, 2010, at 03:35 , Juan Gf wrote:

or otherwise, doesn’t matter… just raise it to human thought level).
a = ‘Apple car caR house tree ice ice ice house’
b = a.downcase.split(’ ')
b.uniq.each do |element|
puts “#{b.count(element)}\t#{element}”
end

CONVERT THE TEXT IN LOWER-CASE AND THEN SPLIT THE TEXT INTO SINGLE
[a list of] WORDS! THEN COUNT [and print] HOW MANY TIMES EVERY SINGLE WORD APPEARS!

I’d say that is mostly correct. You’re glossing over the uniq part:

Then walk over each unique word and print how many times it occurs in
the list of words.

Then say aloud what you want it to do, step by step.

CONVERT THE TEXT IN LOWER-CASE AND THEN SPLIT THE TEXT INTO [a list of] SINGLE WORDS! THEN COUNT HOW MANY TIMES EVERY SINGLE WORD APPEARS! THEN SORT
[the list] THE RESULTS: FIRST THE MORE COMMON WORDS AND AFTER THE LESS COMMON WORDS

better.

“b.uniq.each do |element|
puts “#{(b.count(element)).sort}\t#{element}”
end”

but obviously it doesn’t work.

see how I modified your description to “THEN SORT [the list]”? That’s
what you’re missing. You’re not paying attention to what your each is
iterating over. As others have pointed out, there are a lot of ways to
do this, my favorite is to change the description to:

Convert the text to lower-case and split into a list of words. Create a
hash to count the words (default to 0). Enumerate the list of words and
increment the hash by one for every word seen. Enumerate the hash sorted
by the word counts (descending) and name (ascending) and print the word
and occurances.

input = ‘Apple car caR house tree ice ice ice house’
count = Hash.new 0
input.downcase.split(’ ').each do |word|
count[word] += 1
end
count.sort_by { |word, count| [-count, word] }.each do |word, count|
puts “%4d: %s” % [count, word]
end

which outputs:

3: ice
2: car
2: house
1: apple
1: tree

2010/3/23 Juan Gf [email protected]:

Thank you Jesús & Gavin, I now have enough concepts for studying this
week!!! pretty amazing how many different ways of doing the same thing

Welcome to the wonderful world of Ruby! Here’s why:

Well, OK, that is not really an explanation - but it describes the
situation with Ruby rather accurately.

Kind regards

robert

I’m overwhelmed for your support, that’s very cool from you guys