Friday, June 12, 2009

Character Frequency Checker in Ruby

Character frequency checker which is implemented in Ruby, can be used to find the number of occurrences of each consecutive series of the same character in a given string. In the inspected string, that consecutive series of the each character is replaced by a two element array where the first element is a string containing the character, and the second element is the number of repetitions. For example,if the method receives a parameter like "aaassssddddfgh", then the result will be stored in an array where its elements are:
[[a,3],[s,4],[d,4],f,g,h]
class CharacterFrequency

  def char_frequency(sequence)
    return_array = Array.new
    if (!sequence.eql?(nil) && sequence.size>0)
      puts "String to check for character frequency is : "+sequence
      last_checked_char_index = 0
      for i in 0...sequence.size
        next_char_index = last_checked_char_index + 1
        repeating_char_count = 1
        if(i >= last_checked_char_index)
          for j in i...sequence.size
            if(sequence[j].eql?(sequence[next_char_index]))
              repeating_char_count = repeating_char_count + 1
              next_char_index = next_char_index + 1
              last_checked_char_index = next_char_index
            else
              last_checked_char_index = next_char_index
              arr = Array.new
              if repeating_char_count > 1
                arr = sequence[i].chr.to_s+","+repeating_char_count.to_s
              elsif repeating_char_count == 1
                arr = sequence[i].chr.to_s
              end
              return_array.push(arr)
            break
          end
        end        
      end      
    end
    if(!return_array.empty? && return_array.length >0)
      for indice in 0...return_array.size
        puts return_array[indice]
      end
    end
    else
      puts "Empty or null sequence!"
  end
  end
end

app = CharacterFrequency.new
app.char_frequency("laaamburtiiifikasssssyon")
app.char_frequency("aaassssddddfgh")
app.char_frequency("abcdeee")
app.char_frequency("a")
app.char_frequency("ab")
app.char_frequency("abBgGvV122")
app.char_frequency("asdadddssf")
app.char_frequency("")
app.char_frequency(nil)


Save your script into frequency.rb file and run as follows. It will loop through the created array and display elements at console. Tested with ruby 1.8.7 on ubuntu9.04.
$ ruby frequency.rb

1 comment:

  1. here is the output when i run it:
    String to check for character frequency is : laaamburtiiifikasssssyon
    l
    a,3
    m
    b
    u
    r
    t
    i,3
    f
    i
    k
    a
    s,5
    y
    o
    n
    String to check for character frequency is : aaassssddddfgh
    a,3
    s,4
    d,4
    f
    g
    h
    String to check for character frequency is : abcdeee
    a
    b
    c
    d
    e,3
    String to check for character frequency is : a
    a
    String to check for character frequency is : ab
    a
    b
    String to check for character frequency is : abBgGvV122
    a
    b
    B
    g
    G
    v
    V
    1
    2,2
    String to check for character frequency is : asdadddssf
    a
    s
    d
    a
    d,3
    s,2
    f
    Empty or null sequence!
    Empty or null sequence!

    ReplyDelete