Array#each not performing as expected with nested while loop - ruby

The method below randomly generates the letters for the 6 sides of each of 16 dice meant to be used for Boggle based off the letter_frequencies hash. When the code is run, it iterates through the first element of boggle_dice, an empty array, filling it with random letters.
However, it proceeds to use that first iteration to fill the other 15 elements in boggle_dice with the same letters it has randomly chosen for the first element.
What is causing this to happen, and how should the iterative process be altered to work as intended?
At the end of iteration, each of the keys should appear in boggle_dice the number of times equal to the value associated with it in the hash.
def create_board
letter_frequencies = {
'e' => 10,
'a' => 8,
'i' => 7,
'o' => 6,
'l' => 5,
'n' => 5,
's' => 5,
't' => 5,
'd' => 4,
'r' => 4,
'u' => 4,
'b' => 3,
'c' => 3,
'g' => 3,
'h' => 3,
'm' => 3,
'p' => 3,
'y' => 3,
'f' => 2,
'k' => 2,
'v' => 2,
'w' => 2,
'j' => 1,
'q' => 1,
'x' => 1,
'z' => 1
}
boggle_dice = Array.new(16, [])
boggle_dice.each do |die|
while die.length < 6 do
random_letter = letter_frequencies.keys.sample
letter_frequencies[random_letter] <=0 ? next : die << random_letter
letter_frequencies[random_letter] -= 1
end
end
end

That is a common beginner's mistake. It is because you did Array.new(16, []), the same array is modified repeatedly. It can be fixed by changing it to Array.new(16){[]}.

Related

Merge two hashes with alternation in Ruby (2.x.x)

I have two hashes:
h1 = {'a' => 33, 'b' => 4, 'c' => 6}
h2 = {'d' => 10, 'e' => 1, 'f' => 12}
Now they should be merged into one, with alternation, so the final hash should be like:
{'a' => 33, 'd' => 10, 'b' => 4, 'e' = 1, 'c' => 6, 'f' => 12}
What's the best way to do that. Probably a single liner?
Thanks!
Here is my try
Hash[*[h1.to_a, h2.to_a].transpose.flatten]
# => {"a"=>33, "d"=>10, "b"=>4, "e"=>1, "c"=>6, "f"=>12}
# or
Hash[*h1.to_a.zip(h2.to_a).flatten]
# => {"a"=>33, "d"=>10, "b"=>4, "e"=>1, "c"=>6, "f"=>12}
Even with more STARS. :-)
Hash[*[*h1, *h2].transpose.flatten]
# => {"a"=>"b", "c"=>"d", "e"=>"f", 33=>4, 6=>10, 1=>12}
No stars!
h1 = {'a' => 33, 'b' => 4, 'c' => 6}
h2 = {'d' => 10, 'e' => 1, 'f' => 12}
h1.to_a.zip(h2.to_a).flatten(1).to_h
#=> {"a"=>33, "d"=>10, "b"=>4, "e"=>1, "c"=>6, "f"=>12}
For Ruby versions < 2.0:
Hash[h1.to_a.zip(h2.to_a).flatten(1)]
Note Array#transpose and Enumerable#zip are always interchangeable when manipulating arrays.
For Ruby versions < 1.9, the ordering of key/value pairs in hashes was not specified. For those versions, the closest you could come to answering your question would be to provide a desired ordering of keys:
keys_in_order = ['a', 'd', 'b', 'e', 'c', 'f']
retrieve the associated values:
values = h1.merge(h2).values_at(*keys_in_order)
#=> [33, 10, 4, 1, 6, 12]
and pair these with zip:
keys_in_order.zip(values)
#=> [["a", 33], ["d", 10], ["b", 4], ["e", 1], ["c", 6], ["f", 12]]

Ruby - extracting the unique values per key from an array of hashes

From a hash like the below one, need to extract the unique values per key
array_of_hashes = [ {'a' => 1, 'b' => 2 , 'c' => 3} ,
{'a' => 4, 'b' => 5 , 'c' => 3},
{'a' => 6, 'b' => 5 , 'c' => 3} ]
Need to extract the unique values per key in an array
unique values for 'a' should give
[1,4,6]
unique values for 'b' should give
[2,5]
unique values for 'c' should give
[3]
Thoughts ?
Use Array#uniq:
array_of_hashes = [ {'a' => 1, 'b' => 2 , 'c' => 3} ,
{'a' => 4, 'b' => 5 , 'c' => 3},
{'a' => 6, 'b' => 5 , 'c' => 3} ]
array_of_hashes.map { |h| h['a'] }.uniq # => [1, 4, 6]
array_of_hashes.map { |h| h['b'] }.uniq # => [2, 5]
array_of_hashes.map { |h| h['c'] }.uniq # => [3]
This is more generic:
options = {}
distinct_keys = array_of_hashes.map(&:keys).flatten.uniq
distinct_keys.each do |k|
options[k] = array_of_hashes.map {|o| o[k]}.uniq
end

Ruby way of summing up dictionary values

I have something like this:
a = [{"group_id" => 1, "student_id" => 3, "candies" => 4},
{"group_id" => 2, "student_id" => 1, "candies" => 3},
{"group_id" => 1, "student_id" => 2, "candies" => 2},
{"group_id" => 3, "student_id" => 4, "candies" => 6},
{"group_id" => 1, "student_id" => 5, "candies" => 1},
{"group_id" => 3, "student_id" => 6, "candies" => 1},
{"group_id" => 4, "student_id" => 8, "candies" => 3}]
I have three groups of students and each student gets a certain number of candies. I wish to count the total number of candies in a group. For that, I need to know the students which belong to a certain group and accumulate their candy count. I can do it using loops and initializing counts to zero:
aa = a.group_by { |a| a["group_id"] }
# =>
{
1 => [
{"group_id"=>1, "student_id"=>3, "candies"=>4},
{"group_id"=>1, "student_id"=>2, "candies"=>2},
{"group_id"=>1, "student_id"=>5, "candies"=>1}
],
2 => [{"group_id"=>2, "student_id"=>1, "candies"=>3}],
3 => [
{"group_id"=>3, "student_id"=>4, "candies"=>6},
{"group_id"=>3, "student_id"=>6, "candies"=>1}
],
4 => [{"group_id"=>4, "student_id"=>8, "candies"=>3}]
}
But I'm not able to accumulate the values within the group_id. I wonder if there are any succinct ways of representing it. How do I sum the total number of candies that are present in a group?
The first your step (grouping) is correct. After that you can use the following:
a.group_by {|g| g['group_id']}.map do |g, students|
{group_id:g, candies:students.map {|st| st['candies']}.inject(&:+)}
end
map function is often used with collections instead of loops to make some operation on each element and return modified version of the collection.
Output:
[{:group_id=>1, :candies=>7},
{:group_id=>2, :candies=>3},
{:group_id=>3, :candies=>7},
{:group_id=>4, :candies=>3}]
Adding to #StasS answer, a more direct hash way to do (with a more cryptic code) is like this:
> Hash[a.group_by{|g| g['group_id']}.map{|g,s| [g, s.inject(0){|a,b| a + b["candies"]}]}]
=> {1=>7, 2=>3, 3=>7, 4=>3}
you can unfold the line like this:
groups = a.group_by{|g| g['group_id']}
id_candies_pairs = groups.map{|g,s| [g, s.inject(0){|a,b| a + b["candies"]}]}
id_candies_hash = Hash[id_candies_pairs]
return id_candies_hash
Riffing on the answer by #StasS, you can also just build a simpler looking hash like:
totals_by_group_id = {}
a.group_by {|g| g['group_id']}.map do |g, students|
totals_by_group_id[g] = students.map {|st| st['candies']}.inject(&:+)
end
The resulting totals_by_group_id hash is:
{1=>7, 2=>3, 3=>7, 4=>3}

ways to call self-written ruby methods

How do I write a ruby method that may be called by appending the method name to an object?
i.e. get the quarter period of a specified date
def quarter(dateObject)
quarters = { 1 => 1, 2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 2, 7 => 3, 8 => 3, 9 => 3, 10 => 4, 11 => 4, 12 => 4 }
quarters[dateObject.month]
end
I can use this method now like this:
quarter(Date.today)
but how do I manage to use it like this:
Date.today.quarter
or, even better, in both ways?
You can patch it into the class. Be wary of what you are doing and make sure the method doesn't already exist when modifying a class.
class Date
def quarter
quarters = { 1 => 1, 2 => 1, 3 => 1, 4 => 2, 5 => 2, 6 => 2, 7 => 3, 8 => 3, 9 => 3, 10 => 4, 11 => 4, 12 => 4 }
quarters[self.month] #self is your instance of the Date object
end
end

Sorting a hash in Ruby by its value first then its key

I am trying to sort a document based on the number of times the word appears then alphabetically by the words so when it is outputted it will look something like this.
Unsorted:
'the', '6'
'we', '7'
'those', '5'
'have', '3'
Sorted:
'we', '7'
'the', '6'
'those', '5'
'have', '3'
Try this:
Assuming:
a = {
'the' => '6',
'we' => '7',
'those' => '5',
'have' => '3',
'hav' => '3',
'haven' => '3'
}
then after doing this:
b = a.sort_by { |x, y| [ -Integer(y), x ] }
b will look like this:
[
["we", "7"],
["the", "6"],
["those", "5"],
["hav", "3"],
["have", "3"],
["haven", "3"]
]
Edited to sort by reverse frequencies.
words = {'the' => 6,'we' => 7,'those' => 5,'have' => 3}
sorted_words = words.sort { |a,b| b.last <=> a.last }
sorted_words.each { |k,v| puts "#{k} #{v}"}
produces:
we 7
the 6
those 5
have 3
You probably want the values to be integers rather than strings for comparison purposes.
EDIT
Oops, overlooked the requirement that it needs to be sorted by the key too. So:
words = {'the' => 6,'we' => 7,'those' => 5,'have' => 3,'zoo' => 3,'foo' => 3}
sorted_words = words.sort do |a,b|
a.last == b.last ? a.first <=> b.first : b.last <=> a.last
end
sorted_words.each { |k,v| puts "#{k} #{v}"}
produces:
we 7
the 6
those 5
foo 3
have 3
zoo 3
When you use the sort method on a hash, you receive two element arrays in your comparison block, with which you can do comparisons in one pass.
hsh = { 'the' => '6', 'we' => '6', 'those' => '5', 'have' => '3'}
ary = hsh.sort do |a,b|
# a and b are two element arrays in the format [key,value]
value_comparison = a.last <=> b.last
if value_comparison.zero?
# compare keys if values are equal
a.first <=> b.first
else
value_comparison
end
end
# => [['have',3],['those',5],['the',6],['we',6]]
Note that the result is an array of arrays because hashes do not have intrinsic order in ruby
Try this:
words = {'the' => 6,'we' => 7,'those' => 5,'have' => 3}
words.sort { |(x_k, x_v), (y_k, y_v)| [y_v, y_k] <=> [x_v, x_k]}
#=> [["we", 7], ["the", 6], ["those", 5], ["have", 3]]
histogram = { 'the' => 6, 'we' => 7, 'those' => 5, 'have' => 3, 'and' => 6 }
Hash[histogram.sort_by {|word, freq| [-freq, word] }]
# {
# 'we' => 7,
# 'and' => 6,
# 'the' => 6,
# 'those' => 5,
# 'have' => 3
# }
Note: this assumes that you use numbers to store the numbers. In your data model, you appear to use strings to store the numbers. I have no idea why you would want to do this, but if you do want to do this, you would obviously have to convert them to numbers before sorting and then back to strings.
Also, this assumes Ruby 1.9. In Ruby 1.8, hashes aren't ordered, so you cannot convert the sorted result back to a hash since that would lose the ordering information, you would have to keep it as an array.
1.9.1
>> words = {'the' => 6,'we' => 7, 'those' => 5, 'have' => 3}
=> {"the"=>6, "we"=>7, "those"=>5, "have"=>3}
>> words.sort_by{ |x| x.last }.reverse
=> [["we", 7], ["the", 6], ["those", 5], ["have", 3]]
word_counts = {
'the' => 6,
'we' => 7,
'those' => 5,
'have' => 3,
'and' => 6
};
word_counts_sorted = word_counts.sort do
|a,b|
# sort on last field descending, then first field ascending if necessary
b.last <=> a.last || a.first <=> b.first
end
puts "Unsorted\n"
word_counts.each do
|word,count|
puts word + " " + count.to_s
end
puts "\n"
puts "Sorted\n"
word_counts_sorted.each do
|word,count|
puts word + " " + count.to_s
end

Resources