Here is my solution to this problem. Given an input like
[["A", 13], ["B", 5], ["C", 3], ["D", 1]]
The output is
AABABAACABAACABAACABAD
Ruby source of the program is:
require "pp"
def shuffle (total, num)
ret_arr = Array.new
intervel = total/num.to_f
0.upto(num-1) do |i|
val = i * intervel
ret_arr << val.floor
end
return ret_arr
end
freq_table = [["A", 13], ["B", 5], ["C", 3], ["D", 1]]
pp freq_table
total = 0
freq_table.collect {|i| total += i[1] }
final_array = Array.new(total,0)
print final_array.to_s + "\n\n"
placed = 0
freq_table.each do |i|
placements = shuffle(total - placed, i[1])
placements.each do |j|
free_index = -1
0.upto final_array.size do |k|
free_index += 1 if (final_array[k] == 0 || final_array[k] == i[0])
if j == free_index
final_array[k] = i[0]
break
end
end
end
print "Placing #{i[1]} #{i[0]}s over #{total - placed} positions\n"
pp placements
print final_array.to_s + "\n\n"
placed += i[1]
end
The idea is to take the alphabet with highest frequency and distribute it first across an array whose size is the total number of all elements. Next distribute the alphabet with second highest frequency and distribute it across the free spaces and so on.
If you have questions let me know and I am happy to answer.
raj