Skip to content
Justice Tech School

Practice: Word Frequency

Split, downcase and tally.

Example
text = "the cat and the hat and the bat"
freq = text.split.tally.sort_by { |w, c| [-c, w] }
freq.each { |w, c| puts "#{w}: #{c}" }

Result

the: 3
and: 2
bat: 1
cat: 1
hat: 1

Exercise

Split on spaces.

text.

Quiz Test

Which counts items?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.