Skip to content

Instantly share code, notes, and snippets.

@Tapan
Created April 30, 2013 22:13
Show Gist options
  • Save Tapan/5492356 to your computer and use it in GitHub Desktop.
Save Tapan/5492356 to your computer and use it in GitHub Desktop.
1. Write a command line program that takes an input string, and returns which
character is consecutively repeated the most times. If multiple characters
repeat the same number of time, return all those characters.
For ex: "aaddddffffaa" will return 'f, d'
For ex: "cat dog ___" will return '_'
SOLUTION :
user_input = gets.chomp
list_of_chars = user_input.scan(/./)
ha = Hash.new
p1, p2 = [], []
list_of_chars.each_with_index do |c,index|
if c == list_of_chars[index + 1]
p1 << index
p2 << index + 1
if list_of_chars[index + 1] != list_of_chars[index + 2]
ha["#{list_of_chars[p1.first]}"] = "#{list_of_chars[p1.first..p2.last].count}"
end
else
p1.clear
p2.clear
end
end
puts ha.select{|k,v| v == ha.max[1]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment