Count digits in Ruby
In this exercise we had to count digits in a file.
The algorightm
We need to go over the file line-by-line. For each line then we need to go over it character-by-character. If it is a digit, we need to increment the appropriate counter. We could use 10 different variables (c0, c1, ... c9) to count the number of occurances of the respective digits, but there is a much more comfortable solution using arrays. Specifically we use an array called count with 10 elements in it. In count[0] we will count the number 0s, in count[1] we will count the number of 1s, ... up till count[9] in which we will count the number of 9s.
The solution in Ruby
In this solution written in Ruby, we start by getting the name of the file to be process on the command line.
At first we check if ARGV has the right number of arguments. If not we print an error message and exit.
Then we declare the count array.
Get the name of the file from the first element of the ARGV array containing the values from the command line and open it for reading and iterate over it using each.
In each iteration line will contain a single line. We need to go over it character-by-character. There are a number of ways to do this. In this solution I've decided to split the string into individual characters and then to iterate over the characters.
Using the split method with an empty string we cut up the string at every place where the empty string matches. Meaning we split up the string between any two charctes. The result is a list of charcters assigned to the chars array.
Then we iterate over the elements of this array. In each iteration c is a single character from the source file. Based on our assumption, it is either a digit (0-9) or space.
If it is not a space if c != ' ' then we would like to increment the appropriate counter.
If this is our first encounter with the particular digit we need to initialize the array element that will count that digit.
if not count[c.to_i] then count[c.to_i] = 0 end
Then in every case we increment it by one using the following expression:
count[c.to_i] += 1
At the end of the loop we have counted all the digits. We need to display the results.
For that we iterate over all the numbers between 0 and 9 and print out the number, and the counter of that number.
We could have printed the content of count[i]conditional operator.
examples/ruby/count_digits.rb
if ARGV.length < 1
puts "Missing filename from the command line"
exit;
end
count = []
filename = ARGV[0]
fh = open filename
fh.each do |line|
chars = line.split('')
chars.each do |c|
if c != ' ' then
if not count[c.to_i] then
count[c.to_i] = 0
end
count[c.to_i] += 1
end
end
end
(0..9).each do |i|
print i, ' ', ( count[i] ? count[i] : 0), "\n"
end
Published on 2015-11-03