Algorithm for generating abstract keys in Ruby - ruby

I have an array with each letter
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
input ="cg?"
Then i'm creating sub-keys
sub_keys = (2..letters.length).flat_map do |n|
letters.combination(n).map(&:join).map{|n| n = n.length.to_s + n.chars.sort.join }
end.uniq
Which gives me
sub_keys = ["2?c, 2?g, 2cg, 3cg?"]
Now i want to transform my sub_keys to get expected output which is
keys = [2ac, 2bc, 2cc, 2c, 2ag, 2bg, 2cg, 2dg, 2eg, 2fg, 2gg, 2g, 3acg, 3bcg, 3ccg, 3cdg, 3ceg, 3cfg, 3cgg, 3cg]
As you can see some keys like: 2c, 2g, 3cg stand out from the rest. In my code 2c acts as 2cd, 2ce, 2cf etc.. This is the output i want and my code for it is here:
keys_with_blanks = sub_keys.select{ |k| k.include? "?" }.map{ |k| k.gsub("?","") }
keys = keys_with_blanks.map do |k|
current_letters = choose_letters(all_letters, k)
k = current_letters.map{ |l| l.gsub(l,l+k) }
end.flatten.map{ |k| k.chars.sort.join }.uniq
keys += keys_with_blanks
For making less keys as the input grows, i decided to choose only the letters i need from the alphabet in each iteration instead of all of them, hence the line current_letters = choose_letters(all_letters, k)
choose_letters:
def choose_letters(letters, key)
letters = letters.select{ |l| l <= key.chars.last }
end
Let's say if i have a key key = "2c" then choose_letters will return ["a","b","c"]
This way i only make the keys i need. Up to about 13 character input its quite fast. The problem start when input is 14 or 15 characters long. Then it takes 3 or 4 seconds to make all the keys.
THIS IS THE SLOW PART
keys = keys_with_blanks.map do |k|
current_letters = choose_letters(all_letters, k)
k = current_letters.map{ |l| l.gsub(l,l+k) }
end.flatten.map{ |k| k.chars.sort.join }.uniq
It takes about 3-4 seconds for input = "abcdefghijklmn?"
QUESTION
How can the code be refactored to generate keys faster? My latest upgrade was to impletent choose_letters method, so keys like "2cd", "2ce", "2cf" and so on wouldn't generate, because they work exactly like "2c". Now it generates only the keys i need, so it's a little faster, but still.. Can it be faster?

Related

function that returns the number of letters that repeat in a string

Trying to make a function that counts the number of letters that appear more than once anywhere in a string (not necessarily together, and not the the number of times they repeat). This is what I have:
def num_repeats(string)
repeat = []
i1 = 0
i2 = 1
while i1 < string.length
while i2 < string.length
if (string[i1] == string[i2]) && (!repeat.include? string[i1])
repeat << string[i1]
end
i2 +=1
end
i1+=1
end
return repeat.length
end
puts(num_repeats('sldhelanlaskjkajksda'))
For some reason, it only pushes the first letter of the string if that first letter has been used in the rest of the string, but after that, it seems like the method stops looping through the rest of the string.
I'd like to know first why the current code is not working and if there is a way to fix it, and I also welcome other better solutions.
Here is an orthodox way to do it:
'sldhelanlaskjkajksda'.each_char.group_by(&:itself).count{|_, v| v.length > 1}
# => 6
The reason your code does not work is because, (i) once the i2 loop terminates, you increment i1, and try for another i2 loop in the next i1 iteration, but since i2 hasn't been touched after failed to satisfy the loop condition, it will not satisfy the condition again, and the i2 loop will never run again, and (ii) you are initializing i2 to a constant.
To fix it, initialize i2 within i1 loop at the beginning, and initialize it to i2 = i1 + 1, not 1.
Another way:
s = 'sldhelanlaskjkajksda'
a = s.chars
#=> ["s", "l", "d", "h", "e", "l", "a", "n", "l", "a",
# "s", "k", "j", "k", "a", "j", "k", "s", "d", "a"]
a.difference(a.uniq).uniq.size
#=> 6
where Array#difference is defined in my answer here.
We have:
b = a.uniq
#=> ["s", "l", "d", "h", "e", "a", "n", "k", "j"]
c = a.difference(b)
#=> ["l", "l", "a", "s", "k", "a", "j", "k", "s", "d", "a"]
d = c.uniq
#=> ["l", "a", "s", "k", "j", "d"]
d.size
#=> 6
None of these answers consider that OP asked for repeating letters
But this does:
'sldhe-lanlas-kjkajksda'.scan(/([a-z])(?=.*\1)/i).uniq.size
#=> 6
This is the solution for your problem
def num_repeats(string)
repeat = []
i1 = 0
i2 = 1
while i1 < string.length
while i2 < string.length
if (string[i1] == string[i2]) && !(repeat.include? string[i1])
repeat << string[i1]
end
i2 +=1
end
i1+=1
i2 = i1 + 1
end
return repeat.length
end
puts(num_repeats('sldhelanlaskjkajksda'))
Here is bit simpler (hopefully) and little Ruby-ish, solution:
def num_repeats(string)
# chars in string
chars = string.split('')
# initialize map - for each char, count is initialized to 0
hash = chars.uniq.inject({}) { |h, c| h[c] = 0; h}
# for each char in string, lets count its occurrences
chars.each do |c|
hash[c] += 1
end
# now lets pick those entries from the map where the count is > 1
hash_with_repeated_chars = hash.select {|k, v| v > 1 }
# now lets pick the chars that are repeated by picking keys of hash
repeated_chars = hash_with_repeated_chars.select { |k, v| k}
# return the count of repeated chars
return repeated_chars.count
end
p num_repeats('abc') # Prints 0
p num_repeats('abbc') # Prints 1
p num_repeats('abbcc') # Prints 2
p num_repeats('aabbcc') # Prints 3
I also have a Ruby version that is different from all other answers (and hence, bit inefficient due to multiple iterations it does internally)
s = 'sldhelanlaskjkajksda'
p s.chars.combination(2).to_a.uniq.map(&:sort).map(&:uniq).select{|a| a.size.eql?(1)}.count
Create a hash and set the default value to 0. Use the gsub method to remove all white space from the string. Convert the string into an array of string characters using the split method. Iterate over each letter in the array and store the key as each letter and the number of times each letter occurs as the key's value. Finally Remove any key from the hash with a value that is less than 2. Return the hash's length since this corresponds to the number of letters in the hash that have occurred more than once in your original string. Hope this explanation helps and that it answers your question. The code below could be more compact but it is in its current form in the hopes of being more explanatory and informative.
def counter(string)
counts = Hash.new(0)
result = string.gsub(" ","")
result = result.split('')
result.each do |letter|
counts[letter] += 1
end
counts.delete_if { |key,value| value < 2}
return counts.length
end

How to make the array of the alphabet to rotate from "z" to "a" (ruby)

I am doing a Caesar cipher. I thought that the unless statement will work but it doesn't with or without then. Then I changed the unless with if and put ; in the place of then and it reads : undefined method `>' for nil:NilClass.
def caesar_cipher(input, key)
input.each do |x|
numbers = x.ord + key.to_i unless (numbers > 122) then numbers = x.ord + key - 26
letters = numbers.chr
print letters
end
end
puts "Write the words you want to be ciphered: "
input = gets.chomp.split(//)
puts "Write the key (1 - 26): "
key = gets.chomp
caesar_cipher(input,key)
Here are a couple of Ruby-like ways to write that:
#1
def caesar_cipher(input, key)
letters = ('a'..'z').to_a
input.each_char.map { |c| letters.include?(c) ?
letters[(letters.index(c)+key) % 26] : c }.join
end
caesar_cipher("this is your brown dog", 2)
#=> "vjku ku aqwt dtqyp fqi"
#2
def caesar_cipher(input, key)
letters = ('a'..'z').to_a
h = letters.zip(letters.rotate(key)).to_h
h.default_proc = ->(_,k) { k }
input.gsub(/./,h)
end
caesar_cipher("this is your brown dog", 2)
#=> "vjku ku aqwt dtqyp fqi"
The hash h constructed in #2 equals:
h = letters.zip(letters.rotate(key)).to_h
#=> {"a"=>"c", "b"=>"d", "c"=>"e", "d"=>"f", "e"=>"g", "f"=>"h",
# ...
# "u"=>"w", "v"=>"x", "w"=>"y", "x"=>"z", "y"=>"a", "z"=>"b"}
h.default_proc = ->(_,k) { k } causes
h[c] #=> c
if c is not a lowercase letter (e.g., a space, capital letter, number, punctuation, etc.)
If you write a branch with condition (if or unless) at the end of a line, after an initial statement, there are two things that apply and affect you:
The condition is assessed before the statement on its left. In your case that means numbers has not been assigned yet so it is nil.
The branch decision is whether or not to run the initial statement, you do not branch to the statement after the then.
You can solve this simply by converting your condition to an if and moving it to a separate line:
def caesar_cipher(input, key)
input.each do |x|
numbers = x.ord + key.to_i
if (numbers > 122)
numbers = x.ord + key - 26
end
letters = numbers.chr
print letters
end
end
There are arguably better ways of coding this cipher in Ruby, but this should solve your immediate problem.
There is a more elegant way to loop repeating sequences in ruby. Meet Enumerable#cycle.
('a'..'z').cycle.take(50)
# => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
# "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
# "u", "v", "w", "x", "y", "z", "a", "b", "c", "d",
# "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
# "o", "p", "q", "r", "s", "t", "u", "v", "w", "x"]
Therefore, translating a single letter given a key can be written as:
('a'..'z').cycle.take(letter.ord + key.to_i - 'a'.ord.pred).last
And the entire method can look prettier:
def caesar_cipher(phrase, key)
phrase.each_char.map do |letter|
('a'..'z').cycle.take(letter.ord + key.to_i - 'a'.ord.pred).last
end.join
end
puts caesar_cipher('abcxyz', 3) # => defabc
Note that this is slower than the alternative, but it also has the benefit that it's easier to read and the key can be any number.

Ruby: How to create a letter counting function

I wouldn't have asked for help without first spending a few hours trying to figure out my error but I'm at a wall. So there is my attempt but I'm getting false when I try to pass the argument though and I'm not sure why. I know there are other ways to solve this problem that are a little shorter but I'm more interested in trying to get my code to work. Any help is much appreciated.
Write a method that takes in a string. Your method should return the most common letter in the array, and a count of how many times it appears.
def most_common_letter(string)
idx1 = 0
idx2 = 0
counter1 = 0
counter2 = 0
while idx1 < string.length
idx2 = 0
while idx2 < string.length
if string[idx1] == string[idx2]
counter1 += 1
end
idx2 += 1
end
if counter1 > counter2
counter2 = counter1
counter1 = 0
var = [string[idx1], counter2]
end
idx1 += 1
end
return var
end
puts("most_common_letter(\"abca\") == [\"a\", 2]: #{most_common_letter("abca") == ["a", 2]}")
puts("most_common_letter(\"abbab\") == [\"b\", 3]: #{most_common_letter("abbab") == ["b", 3]}")
I didn't rewrite your code because I think it's important to point out what is wrong with the existing code that you wrote (especially since you're familiar with it). That said, there are much more 'ruby-like' ways to go about this.
The issue
counter1 is only being reset if you've found a 'new highest'. You need to reset it regardless of whether or not a new highest number has been found:
def most_common_letter(string)
idx1 = 0
idx2 = 0
counter1 = 0
counter2 = 0
while idx1 < string.length
idx2 = 0
while idx2 < string.length
if string[idx1] == string[idx2]
counter1 += 1
end
idx2 += 1
end
if counter1 > counter2
counter2 = counter1
# counter1 = 0 THIS IS THE ISSUE
var = [string[idx1], counter2]
end
counter1 = 0 # this is what needs to be reset each time
idx1 += 1
end
return var
end
Here's what the output is:
stackoverflow master % ruby letter-count.rb
most_common_letter("abca") == ["a", 2]: true
most_common_letter("abbab") == ["b", 3]: true
I think you're aware there are way better ways to do this but frankly the best way to debug this is with a piece of paper. "Ok counter1 is now 1, indx2 is back to zero", etc. That will help you keep track.
Another bit of advice, counter1 and counter2 are not very good variable names. I didn't realize what you were using them for initially and that should never be the case, it should be named something like current_count highest_known_count or something like that.
Your question has been answered and #theTinMan has suggested a more Ruby-like way of doing what you want to do. There are many other ways of doing this and you might find it useful to consider a couple more.
Let's use the string:
string = "Three blind mice. Oh! See how they run."
First, you need to answer a couple of questions:
do you want the frequency of letters or characters?
do you want the frequency of lowercase and uppercase letters combined?
I assume you want the frequency of letters only, independent of case.
#1 Count each unique letter
We can deal with the case issue by converting all the letters to lower or upper case, using the method String#upcase or String#downcase:
s1 = string.downcase
#=> "three blind mice. oh! see how they run."
Next we need to get rid of all the characters that are not letters. For that, we can use String#delete1:
s2 = s1.delete('^a-z')
#=> "threeblindmiceohseehowtheyrun"
Now we are ready to convert the string s2 to an an array of individual characters2:
arr = s2.chars
#=> ["t", "h", "r", "e", "e", "b", "l", "i", "n", "d",
# "m", "i", "c", "e", "o", "h", "s", "e", "e", "h",
# "o", "w", "t", "h", "e", "y", "r", "u", "n"]
We can combine these first three steps as follows:
arr = string.downcase.gsub(/[^a-z]/, '').chars
First obtain all the distinct letters present, using Array.uniq.
arr1 = arr.uniq
#=> ["t", "h", "r", "e", "b", "l", "i", "n",
# "d", "m", "c", "o", "s", "w", "y", "u"]
Now convert each of these characters to a two-character array consisting of the letter and its count in arr. Whenever you need convert elements of a collection to something else, think Enumerable#map (a.k.a. collect). The counting is done with Array#count. We have:
arr2 = arr1.map { |c| [c, arr.count(c)] }
#=> [["t", 2], ["h", 4], ["r", 2], ["e", 6], ["b", 1], ["l", 1],
# ["i", 2], ["n", 2], ["d", 1], ["m", 1], ["c", 1], ["o", 2],
# ["s", 1], ["w", 1], ["y", 1], ["u", 1]]
Lastly, we use Enumerable#max_by to extract the element of arr2 with the largest count3:
arr2.max_by(&:last)
#=> ["e", 6]
We can combine the calculation of arr1 and arr2:
arr.uniq.map { |c| [c, arr.count(c)] }.max_by(&:last)
and further replace arr with that obtained earlier:
string.downcase.gsub(/[^a-z]/, '').chars.uniq.map { |c|
[c, arr.count(c)] }.max_by(&:last)
#=> ["e", 6]
String#chars returns a temporary array, upon which the method Array#uniq is invoked. As alternative, which avoids the creation of the temporary array, is to use String#each_char in place of String#chars, which returns an enumerator, upon which Enumerable#uniq is invoked.
The use of Array#count is quite an inefficient way to do the counting because a full pass through arr is made for each unique letter. The methods below are much more efficient.
#2 Use a hash
With this approach we wish to create a hash whose keys are the distinct elements of arr and each value is the count of the associated key. Begin by using the class method Hash::new to create hash whose values have a default value of zero:
h = Hash.new(0)
#=> {}
We now do the following:
string.each_char { |c| h[c.downcase] += 1 if c =~ /[a-z]/i }
h #=> {"t"=>2, "h"=>4, "r"=>2, "e"=>6, "b"=>1, "l"=>1, "i"=>2, "n"=>2,
# "d"=>1, "m"=>1, "c"=>1, "o"=>2, "s"=>1, "w"=>1, "y"=>1, "u"=>1}
Recall h[c] += 1 is shorthand for:
h[c] = h[c] + 1
If the hash does not already have a key c when the above expression is evaluated, h[c] on the right side is replaced by the default value of zero.
Since the Enumerable module is included in the class Hash we can invoke max_by on h just as we did on the array:
h.max_by(&:last)
#=> ["e", 6]
There is just one more step. Using Enumerable#each_with_object, we can shorten this as follows:
string.each_char.with_object(Hash.new(0)) do |c,h|
h[c.downcase] += 1 if c =~ /[a-z]/i
end.max_by(&:last)
#=> ["e", 6]
The argument of each_with_object is an object we provide (the empty hash with default zero). This is represented by the additional block variable h. The expression
string.each_char.with_object(Hash.new(0)) do |c,h|
h[c.downcase] += 1 if c =~ /[a-z]/i
end
returns h, to which max_by(&:last) is sent.
#3 Use group_by
I will give a slightly modified version of the Tin Man's answer and show how it works with the value of string I have used. It uses the method Enumerable#group_by:
letters = string.downcase.delete('^a-z').each_char.group_by { |c| c }
#=> {"t"=>["t", "t"], "h"=>["h", "h", "h", "h"], "r"=>["r", "r"],
# "e"=>["e", "e", "e", "e", "e", "e"], "b"=>["b"], "l"=>["l"],
# "i"=>["i", "i"], "n"=>["n", "n"], "d"=>["d"], "m"=>["m"],
# "c"=>["c"], "o"=>["o", "o"], "s"=>["s"], "w"=>["w"],
# "y"=>["y"], "u"=>["u"]}
used_most = letters.max_by { |k,v| v.size }
#=> ["e", ["e", "e", "e", "e", "e", "e"]]
used_most[1] = used_most[1].size
used_most
#=> ["e", 6]
In later versions of Ruby you could simplify as follows:
string.downcase.delete('^a-z').each_char.group_by(&:itself).
transform_values(&:size).max_by(&:last)
#=> ["e", 6]
See Enumerable#max_by, Object#itself and Hash#transform_values.
1. Alternatively, use String#gsub: s1.gsub(/[^a-z]/, '').
2. s2.split('') could also be used.
3. More or less equivalent to arr2.max_by { |c, count| count }.
It's a problem you'll find asked all over Stack Overflow, a quick search should have returned a number of hits.
Here's how I'd do it:
foo = 'abacab'
letters = foo.chars.group_by{ |c| c }
used_most = letters.sort_by{ |k, v| [v.size, k] }.last
used_most # => ["a", ["a", "a", "a"]]
puts '"%s" was used %d times' % [used_most.first, used_most.last.size]
# >> "a" was used 3 times
Of course, now that this is here, and it's easily found, you can't use it because any teacher worth listening to will also know how to search Stack Overflow and will find this answer.

How to find all longest words in a string?

If I have a string with no spaces in it, just a concatenation like "hellocarworld", I want to get back an array of the largest dictionary words. so I would get ['hello','car','world']. I would not get back words such as 'a' because that belongs in 'car'.
The dictionary words can come from anywhere such as the dictionary on unix:
words = File.readlines("/usr/share/dict/words").collect{|x| x.strip}
string= "thishasmanywords"
How would you go about doing this?
I would suggest the following.
Code
For a given string and dictionary, dict:
string_arr = string.chars
string_arr.size.downto(1).with_object([]) { |n,arr|
string_arr.each_cons(n) { |a|
word = a.join
arr << word if (dict.include?(word) && !arr.any? {|w| w.include?(word) })}}
Examples
dict = File.readlines("/usr/share/dict/words").collect{|x| x.strip}
string = "hellocarworld"
#=> ["hello", "world", "loca", "car"]
string= "thishasmanywords"
#=> ["this", "hish", "many", "word", "sha", "sma", "as"]
"loca" is the plural of "locus". I'd never heard of "hish", "sha" or "sma". They all appear to be slang words, as I could only find them in something called the "Urban Dictonary".
Explanation
string_arr = "hellocarworld".chars
#=> ["h", "e", "l", "l", "o", "c", "a", "r", "w", "o", "r", "l", "d"]
string_arr.size
#=> 13
so for this string we have:
13.downto(1).with_object([]) { |n,arr|...
where arr is an initially-empty array that will be computed and returned. For n => 13,
enum = string_arr.each_cons(13)
#<Enumerator: ["h","e","l","l","o","c","a","r","w","o","r","l","d"]:each_cons(13)>
which enumerates over an array consisting of the single array string_arr:
enum.size #=> 1
enum.first == string_arr #=> true
That single array is assigned to the block variable a, so we obtain:
word = enum.first.join
#=> "hellocarworld"
We find
dict.include?(word) #=> false
so this word is not added to the array arr. It is was in the dictionary we would check to make sure it was not a substring of any word already in arr, which are all of the same size or larger (longer words).
Next we compute:
enum = string_arr.each_cons(12)
#<Enumerator: ["h","e","l","l","o","c","a","r","w","o","r","l","d"]:each_cons(12)>
which we can see enumerates two arrays:
enum = string_arr.each_cons(12).to_a
#=> [["h", "e", "l", "l", "o", "c", "a", "r", "w", "o", "r", "l"],
# ["e", "l", "l", "o", "c", "a", "r", "w", "o", "r", "l", "d"]]
corresponding to the words:
enum.first.join #=> "hellocarworl"
enum.last.join #=> "ellocarworld"
neither of which are in the dictionary. We continue in this fashion, until we reach n => 1:
string_arr.each_cons(1).to_a
#=> [["h"], ["e"], ["l"], ["l"], ["o"], ["c"],
# ["a"], ["r"], ["w"], ["o"], ["r"], ["l"], ["d"]]
We find only "a" in the dictionary, but as it is a substring of "loca" or "car", which are already elements of the array arr, we do not add it.
This can be a bit tricky if you're not familiar with the technique. I often lean heavily on regular expressions for this:
words = File.readlines("/usr/share/dict/words").collect(&:strip).reject(&:empty?)
regexp = Regexp.new(words.sort_by(&:length).reverse.join('|'))
phrase = "hellocarworld"
equiv = [ ]
while (m = phrase.match(regexp)) do
phrase.gsub!(m[0]) do
equiv << m[0]
'*'
end
end
equiv
# => ["hello", "car", "world"]
Update: Strip out blank strings which would cause the while loop to run forever.
Starting at the beginning of the input string, find the longest word in the dictionary. Chop that word off the beginning of the input string and repeat.
Once the input string is empty, you are done. If the string is not empty but no word was found, remove the first character and continue the process.

Array of Hashes to Hash Conversion while preventing Key Override

How would you convert:
x = [
{"a" => ["a","b","c","d","e"]},
{"f" => ["f","g","h","i","j","k","l","m","n"]},
{"f" => ["o","p"]}
]
Into:
{
"a"=>["a", "b", "c", "d", "e"],
"f" => ["f","g","h","i","j","k","l","m","n"]},
"f01"=>["o", "p"]
}
I originally was using
x.reduce({},:update)
however this overrides the first "f" (because keys are unique).
I have scoured for answers but have not found anything that would work in this case.
The resulting hash, after changing keys, is going to result in a very difficult problem: How are you going to easily look up the resulting hashes and their arrays?
I'd recommend rethinking the sort of hash you want. Instead of creating new hash keys that will be difficult to look up and have a tenuous association with the original hash key, reuse the original keys and create hash keys that point to arrays of arrays. Starting with a much simpler starting array of hashes:
x = [
{'a' => ['a', 'b']},
{'f' => ['f', 'g']},
{'f' => ['o', 'p']}
]
Here's the code:
hash = x.each_with_object(Hash.new{ |h, k| h[k] = [] }) { |e, h|
k, v = e.flatten
h[k] << v
}
pp hash
This is the output:
{"a"=>[["a", "b"]], "f"=>[["f", "g"], ["o", "p"]]}
Lookups become more simple:
hash['f'][0]
# => ["f", "g"]
hash['f'].last
# => ["o", "p"]
You maintain the separation of the original arrays, while gaining a more logical and much simpler lookup.
If you need to know how many sub-arrays are assigned to a hash it's easy too:
hash['f'].size
# => 2
If you want to iterate over all the 'f' entries, which is what would become convoluted and difficult with your design, simply do:
hash['f'].each do |f|
...
end
or:
hash['f'].map { |f|
...
}
Assuming that the 01 part of f01 is just a counter and you don't care about formatting that number you could do the following:
def reduce_update_incr(arr)
key_count = {}
arr.reduce({}) do |memo,h|
h.each do |k,v|
k = k + (key_count[k]=(key_count[k]||0)+1).to_s if memo.include?(k)
memo[k] = v
end
memo
end
end
reduce_update_incr(x)
# {"a"=>["a", "b", "c", "d", "e"],
# "f"=>["f", "g", "h", "i", "j", "k", "l", "m", "n"],
# "f1"=>["o", "p"]}
Yuck.
x = [
{"a" => ["a","b","c","d","e"]},
{"f" => ["f","g","h","i","j","k","l","m","n"]},
{"f" => ["o","p"]}
]
res = x.collect{|d| d.first}.each_with_object({}) do |(k,v), h, n = "00"|
(k += n.next!) if (h.key? k)
h[k] = v
end
res == {
"a"=>["a", "b", "c", "d", "e"],
"f" => ["f","g","h","i","j","k","l","m","n"],
"f01"=>["o", "p"]
}
You could use inject as follows:
> x.inject({}){|r,h| r[h.keys.first] ||= [] ; r[h.keys.first] << h.values.first; r}
=> {"a"=>[["a", "b", "c", "d", "e"]],
"f"=>[["f", "g", "h", "i", "j", "k", "l", "m", "n"], ["o", "p"]]}
Please note that you will get multiple arrays for the same key. (I.E. "f")
The resulting hash is slightly different from what you asked for, but it might be even easier to use, because there is no confusion with modified keys.

Resources