Finding total number of combinations between flavors and toppings - ruby

I was taking a quiz in Ruby that required me to find the total number of unique combinations between flavors and toppings but I was stuck on a particular part. One of the rules was that "chocolate chip ice cream can't have chocolate chips toppings" or some wording similar to that. This wasn't the exact problem but I tried my best to create a similar problem. How would I go about solving this problem?
def combinations(flavors, toppings)
end
flavors = ["fudge", "vanilla", "chocolate chip", "cookie dough"] # 11
toppings = ["chocolate chips", "sprinkles", "mint"]
I first was thinking of doing a nested loop problem but that part that I was stuck on is that "chocolate chip" and "chocolate chips" isn't the same.

Consider an analogous situation. Use #product to find all combinations and then #reject to eliminate the ones that fail to meet the criteria.
In the below example, the second string cannot contain the first one. #index will return nil is the substring is not found.
x = ["a", "b", "c"]
y = ["as", "hello", "world"]
x.product(y).reject { |a, b| b.index(a) }
# => [["a", "hello"], ["a", "world"], ["b", "as"],
# ["b", "hello"], ["b", "world"], ["c", "as"],
# ["c", "hello"], ["c", "world"]]

I don't want to give it away completely if it is a quiz question, but as always when solving programming problems, a good start is to break it down into smaller steps. I think the following should give you a good hint on how to do it.
Define what it means for two flavours to be the same. Is "chocolate chip" the same as "chocolate chips"? (note the 's' that is different) What about "chocolate" ice cream with "chocolate chips"? One way would be to say that two flavours are equal if one is a substring of the other. Another, slightly harder, way would be to say that they must be exactly the same, normalising away plural forms. Write yourself a function that can take two flavours and give you a true/false answer.
Hint: Check out the docs for the #include? method on String if you go with the first definition and remember that you might not know in advance which string is a substring of the other.
Build a list of all possible pairs when taking one element from each of the two arrays.
Hint: Check out the #product method on Array as mentioned by #jvx8ss.
Remove all pairs where both elements of the pair are equal according to the definition from step 1.
Hint: Check out the #reject method on Array.
All of the relevant docs can be found at https://ruby-doc.org/3.1.3/

Building off of Chris's answer and considering not only that the first string should not be contained in the second, but also the other way around. I think it might be slightly better to sort the array first before doing the check with reject.
def combinations(flavors, toppings)
flavors.product(toppings).reject do |combo|
first, second = combo.sort
second.include?(first)
end.count
end
flavors = ["fudge", "vanilla", "chocolate chip", "cookie dough"]
toppings = ["chocolate chips", "sprinkles", "mint"]
pp combinations(flavors, toppings) # => 11

Related

Need help understanding a .sort_by block within my Ruby method

I am attempting to improve my coding by working through Code Wars problems and am doing it in a specific manner. First I try working out the code on my own. Failing that I do a few Google searches (Stack Exchange) to troubleshoot. Failing that I unlock the answer to the problem and then thoroughly examine it and articulate the code in writing so I understand the code within the answer. Well, I had to the the latter today.
I understand the lion's share of the code, but I am feeling a bit shaky around my understanding of the code in the fourth line. I have done a bunch of research around the methods used in this code on Rubydocs, but still feel in the dark. Could someone please articulate it part for part for me? I am also looking for any type of advice around my current articulation of the code.
def letter_frequency(text)
chars = text.downcase.scan(/[a-zA-Z]/).sort
chars.map { |x| [x, chars.count(x)] }.uniq
.sort_by { |y| [-y[1], y[0]] }
end
set the "chars" variable to text (the parameter), lowercased,
scan and return any single charecter between the range a-z or A-Z, and
then sort them lowest to high.
use .map to set a key variable and return all of the charecters
inside the submitted array with the total count for the key (x),
and then make all charecteds unique with the .uniq method.
Then sort with the given block using the "y" key to return all
of the negative/positive numbers and their count within the submitted
array.
Thanks a million!
sort_by does pretty much what it sounds like. It lets you "sort" a hash based on some criteria (that you pass to a block). Since hashes don't really have an order, it returns an array sorted by the criteria you passed in the block:
Consider the following hash (count of the characters in 'hello'):
chars = {"h"=>1, "e"=>1, "l"=>2, "o"=>1}
The key is the character and the value is the count. If we wanted a sorted array of characters based on their count, we could run sort_by on the above hash based on the value like so:
chars.sort_by { |key, value| value }
That would return the following:
[["h", 1], ["e", 1], ["o", 1], ["l", 2]]
If we wanted the most common characters first, we'd sort on -value:
chars.sort_by { |key, value| -value }
Which would return the following:
[["l", 2], ["h", 1], ["e", 1], ["o", 1]]
I've rewritten your method with some tweaks:
def letter_frequency(text)
count = Hash.new 0
chars = text.downcase.scan(/[a-zA-Z]/)
chars.each do |char|
count[char] += 1
end
count.sort_by { |_, value| -value }
end
First, count is the hash that stores the counts of the characters of your text, which we then sort by the count of each character (value). The _ in the sort_by block is a bit of shorthand because we don't use the key of the hash for anything.

How do I compare the length of element in array

I have the following code:
sentence_array = ["hello", "hi", "bonjour"]
I'd like to be able to output the word that's the longest of the 3 elements. I know I should probably use sentence_array.each but I'm stuck on how to proceed.
sentence_array.max_by(&:length)
The enumerable module is one you will learn to love.
This is one way:
sentence_array.sort_by {|e| e.length} .last
#=> "bonjour"

Chaining partition, keep_if etc

[1,2,3].partition.inject(0) do |acc, x|
x>2 # this line is intended to be used by `partition`
acc+=x # this line is intended to be used by `inject`
end
I know that I can write above stanza using different methods but this is not important here.
What I want to ask why somebody want to use partition (or other methods like keep_if, delete_if) at the beginning of the "chain"?
In my example, after I chained inject I couldn't use partition. I can write above stanza using each:
[1,2,3].each.inject(0) do |acc, x|
x>2 # this line is intended to be used by `partition`
acc+=x # this line is intended to be used by `inject`
end
and it will be the same, right?
I know that x>2 will be discarded (and not used) by partition. Only acc+=x will do the job (sum all elements in this case).
I only wrote that to show my "intention": I want to use partition in the chain like this [].partition.inject(0).
I know that above code won't work as I intended and I know that I can chain after block( }.map as mentioned by Neil Slater).
I wanted to know why, and when partition (and other methods like keep_if, delete_if etc) becomes each (just return elements of the array as partition do in the above cases).
In my example, partition.inject, partition became each because partition cannot take condition (x>2).
However partition.with_index (as mentioned by Boris Stitnicky) works (I can partition array and use index for whatever I want):
shuffled_array
.partition
.with_index { |element, index|
element > index
}
ps. This is not question about how to get sum of elements that are bigger than 2.
This is an interesting situation. Looking at your code examples, you are obviously new to Ruby and perhaps also to programming. Yet you managed to ask a very difficult question that basically concerns the Enumerator class, one of the least publicly understood classes, especially since Enumerator::Lazy was introduced. To me, your question is difficult enough that I am not able to provide a comprehensive answer. Yet the remarks about your code would not fit into a comment under the OP. That's why I'm adding this non-answer.
First of all, let us notice a few awful things in your code:
Useless lines. In both blocks, x>2 line is useless, because its return value is discarded.
[1,2,3].partition.inject(0) do |x, acc|
x>2 # <---- return value of this line is never used
acc+=x
end
[1,2,3].each.inject(0) do |x, acc|
x>2 # <---- return value of this line is never used
acc+=x
end
I will ignore this useless line when discussing your code examples further.
Useless #each method. It is useless to write
[1,2,3].each.inject(0) do |x, acc|
acc+=x
end
This is enough:
[1,2,3].inject(0) do |x, acc|
acc+=x
end
Useless use of #partition method. Instead of:
[1,2,3].partition.inject(0) do |x, acc|
acc+=x
end
You can just write this:
[1,2,3].inject(0) do |x, acc|
acc+=x
end
Or, as I would write it, this:
[ 1, 2, 3 ].inject :+
But then, you ask a deep question about using #partition method in the enumerator mode. Having discussed the trivial newbie problems of your code, we are left with the question how exactly the enumerator-returning versions of the #partition, #keep_if etc. should be used, or rather, what are the interesting way of using them, because everyone knows that we can use them for chaining:
array = [ *1..6 ]
shuffled_arrray = array.shuffle # randomly shuffles the array elements
shuffled_array
.partition # partition enumerator comes into play
.with_index { |element, index| # method Enumerator#with_index comes into play
element > index # and partitions elements into those greater
} # than their index, and those smaller
And also like this:
e = partition_enumerator_of_array = array.partition
# And then, we can partition the array in many ways:
e.each &:even? # partitions into odd / even numbers
e.each { rand() > 0.5 } # partitions the array randomly
# etc.
An easily understood advantage is that instead of writing longer:
array.partition &:even?
You can write shorter:
e.each &:even?
But I am basically sure that enumerators provide more power to the programmer than just chaining collection methods and shortening code a little bit. Because different enumerators do very different things. Some, such as #map! or #reject!, can even modify the collection on which they operate. In this case, it is imaginable that one could combine different enumerators with the same block to do different things. This ability to vary not just the blocks, but also the enumerators to which they are passed, gives combinatorial power, which can very likely be used to make some otherwise lengthy code very concise. But I am unable to provide a very useful concrete example of this.
In sum, Enumerator class is here mainly for chaining, and to use chaining, programmers do not really need to undestand Enumerator in detail. But I suspect that the correct habits regarding the use of Enumerator might be as difficult to learn as, for instance, correct habits of parametrized subclassing. I suspect I have not grasped the most powerful ways to use enumerators yet.
I think that the result [3, 3] is what you are looking for here - partitioning the array into smaller and larger numbers then summing each group. You seem to be confused about how you give the block "rules" to the two different methods, and have merged what should be two blocks into one.
If you need the net effects of many methods that each take a block, then you can chain after any block, by adding the .method after the close of the block like this: }.each or end.each
Also note that if you create partitions, you are probably wanting to sum over each partition separately. To do that you will need an extra link in the chain (in this case a map):
[1,2,3].partition {|x| x > 2}.map do |part|
part.inject(0) do |acc, x|
x + acc
end
end
# => [3, 3]
(You also got the accumulator and current value wrong way around in the inject, and there is no need to assign to the accumulator, Ruby does that for you).
The .inject is no longer in a method chain, instead it is inside a block. There is no problem with blocks inside other blocks, in fact you will see this very often in Ruby code.
I have chained .partition and .map in the above example. You could also write the above like this:
[1,2,3].partition do
|x| x > 2
end.map do |part|
part.inject(0) do |acc, x|
x + acc
end
end
. . . although when chaining with short blocks, I personally find it easier to use the { } syntax instead of do end, especially at the start of a chain.
If it all starts to look complex, there is not usually a high cost to assigning the results of the first part of a chain to a local variable, in which case there is no chain at all.
parts = [1,2,3].partition {|x| x > 2}
parts.map do |part|
part.inject(0) do |acc, x|
x + acc
end
end

How can I sort this array?

I have an array, headlines, that holds several sentences, so like:
headlines = ["I see a tree", "Facebook is slow", "plants need water to grow", "There's an orange", "I think we'll agree"]
first = headlines[0]
second = headlines[1]
third = headlines[2]
I am using the ruby_rhymes gem which provides a method #to_phrase.rhymes which prints out rhyming words for the last word in a string you provide it with. Now to check if the array strings rhyme, I do something like:
> first.to_phrase.rhymes.flatten.join(", ").include?(second.to_phrase.rhymes.flatten.join(", "))
=> false
> second.to_phrase.rhymes.flatten.join(", ").include?(third.to_phrase.rhymes.flatten.join(", "))
=> true
I want to save these to a text file so I want to sort them in the array so that rhyming pairs are subsequent to one another. I know to sort so that strings follow if the last 3 characters are the same is:
headlines.sort! {|a,b| a[-3,3] <=> b[-3,3] }
But I don't know how to do want I want.
By investigating the output of your suggestion you can see that you are on the right track:
p headlines.sort {|a,b| a[-3,3] <=> b[-3,3] }
# => ["Facebook is slow", "There's an orange", "I see a tree", "I think we'll agree", "plants need water to grow"]
"...slow" and "...grow" are the only unordered sentences, caused by the letters 'r' and 'o'. A simple hack would be to reverse the order of the comparison like that:
p headlines.sort {|a,b| a[-3,3].reverse <=> b[-3,3].reverse }
# => ["I see a tree", "I think we'll agree", "There's an orange", "Facebook is slow", "plants need water to grow"]
So I've figured it out:
headlines.sort_by! { |h| h.to_phrase.rhyme_key }
This doesn't work 100% but that's the fault of the dictionary the gem relies on.

What is the "right" way to iterate through an array in Ruby?

PHP, for all its warts, is pretty good on this count. There's no difference between an array and a hash (maybe I'm naive, but this seems obviously right to me), and to iterate through either you just do
foreach (array/hash as $key => $value)
In Ruby there are a bunch of ways to do this sort of thing:
array.length.times do |i|
end
array.each
array.each_index
for i in array
Hashes make more sense, since I just always use
hash.each do |key, value|
Why can't I do this for arrays? If I want to remember just one method, I guess I can use each_index (since it makes both the index and value available), but it's annoying to have to do array[index] instead of just value.
Oh right, I forgot about array.each_with_index. However, this one sucks because it goes |value, key| and hash.each goes |key, value|! Is this not insane?
This will iterate through all the elements:
array = [1, 2, 3, 4, 5, 6]
array.each { |x| puts x }
# Output:
1
2
3
4
5
6
This will iterate through all the elements giving you the value and the index:
array = ["A", "B", "C"]
array.each_with_index {|val, index| puts "#{val} => #{index}" }
# Output:
A => 0
B => 1
C => 2
I'm not quite sure from your question which one you are looking for.
I think there is no one right way. There are a lot of different ways to iterate, and each has its own niche.
each is sufficient for many usages, since I don't often care about the indexes.
each_ with _index acts like Hash#each - you get the value and the index.
each_index - just the indexes. I don't use this one often. Equivalent to "length.times".
map is another way to iterate, useful when you want to transform one array into another.
select is the iterator to use when you want to choose a subset.
inject is useful for generating sums or products, or collecting a single result.
It may seem like a lot to remember, but don't worry, you can get by without knowing all of them. But as you start to learn and use the different methods, your code will become cleaner and clearer, and you'll be on your way to Ruby mastery.
I'm not saying that Array -> |value,index| and Hash -> |key,value| is not insane (see Horace Loeb's comment), but I am saying that there is a sane way to expect this arrangement.
When I am dealing with arrays, I am focused on the elements in the array (not the index because the index is transitory). The method is each with index, i.e. each+index, or |each,index|, or |value,index|. This is also consistent with the index being viewed as an optional argument, e.g. |value| is equivalent to |value,index=nil| which is consistent with |value,index|.
When I am dealing with hashes, I am often more focused on the keys than the values, and I am usually dealing with keys and values in that order, either key => value or hash[key] = value.
If you want duck-typing, then either explicitly use a defined method as Brent Longborough showed, or an implicit method as maxhawkins showed.
Ruby is all about accommodating the language to suit the programmer, not about the programmer accommodating to suit the language. This is why there are so many ways. There are so many ways to think about something. In Ruby, you choose the closest and the rest of the code usually falls out extremely neatly and concisely.
As for the original question, "What is the “right” way to iterate through an array in Ruby?", well, I think the core way (i.e. without powerful syntactic sugar or object oriented power) is to do:
for index in 0 ... array.size
puts "array[#{index}] = #{array[index].inspect}"
end
But Ruby is all about powerful syntactic sugar and object oriented power, but anyway here is the equivalent for hashes, and the keys can be ordered or not:
for key in hash.keys.sort
puts "hash[#{key.inspect}] = #{hash[key].inspect}"
end
So, my answer is, "The “right” way to iterate through an array in Ruby depends on you (i.e. the programmer or the programming team) and the project.". The better Ruby programmer makes the better choice (of which syntactic power and/or which object oriented approach). The better Ruby programmer continues to look for more ways.
Now, I want to ask another question, "What is the “right” way to iterate through a Range in Ruby backwards?"! (This question is how I came to this page.)
It is nice to do (for the forwards):
(1..10).each{|i| puts "i=#{i}" }
but I don't like to do (for the backwards):
(1..10).to_a.reverse.each{|i| puts "i=#{i}" }
Well, I don't actually mind doing that too much, but when I am teaching going backwards, I want to show my students a nice symmetry (i.e. with minimal difference, e.g. only adding a reverse, or a step -1, but without modifying anything else).
You can do (for symmetry):
(a=*1..10).each{|i| puts "i=#{i}" }
and
(a=*1..10).reverse.each{|i| puts "i=#{i}" }
which I don't like much, but you can't do
(*1..10).each{|i| puts "i=#{i}" }
(*1..10).reverse.each{|i| puts "i=#{i}" }
#
(1..10).step(1){|i| puts "i=#{i}" }
(1..10).step(-1){|i| puts "i=#{i}" }
#
(1..10).each{|i| puts "i=#{i}" }
(10..1).each{|i| puts "i=#{i}" } # I don't want this though. It's dangerous
You could ultimately do
class Range
def each_reverse(&block)
self.to_a.reverse.each(&block)
end
end
but I want to teach pure Ruby rather than object oriented approaches (just yet). I would like to iterate backwards:
without creating an array (consider 0..1000000000)
working for any Range (e.g. Strings, not just Integers)
without using any extra object oriented power (i.e. no class modification)
I believe this is impossible without defining a pred method, which means modifying the Range class to use it. If you can do this please let me know, otherwise confirmation of impossibility would be appreciated though it would be disappointing. Perhaps Ruby 1.9 addresses this.
(Thanks for your time in reading this.)
Use each_with_index when you need both.
ary.each_with_index { |val, idx| # ...
The other answers are just fine, but I wanted to point out one other peripheral thing: Arrays are ordered, whereas Hashes are not in 1.8. (In Ruby 1.9, Hashes are ordered by insertion order of keys.) So it wouldn't make sense prior to 1.9 to iterate over a Hash in the same way/sequence as Arrays, which have always had a definite ordering. I don't know what the default order is for PHP associative arrays (apparently my google fu isn't strong enough to figure that out, either), but I don't know how you can consider regular PHP arrays and PHP associative arrays to be "the same" in this context, since the order for associative arrays seems undefined.
As such, the Ruby way seems more clear and intuitive to me. :)
Here are the four options listed in your question, arranged by freedom of control. You might want to use a different one depending on what you need.
Simply go through values:
array.each
Simply go through indices:
array.each_index
Go through indices + index variable:
for i in array
Control loop count + index variable:
array.length.times do | i |
Trying to do the same thing consistently with arrays and hashes might just be a code smell, but, at the risk of my being branded as a codorous half-monkey-patcher, if you're looking for consistent behaviour, would this do the trick?:
class Hash
def each_pairwise
self.each { | x, y |
yield [x, y]
}
end
end
class Array
def each_pairwise
self.each_with_index { | x, y |
yield [y, x]
}
end
end
["a","b","c"].each_pairwise { |x,y|
puts "#{x} => #{y}"
}
{"a" => "Aardvark","b" => "Bogle","c" => "Catastrophe"}.each_pairwise { |x,y|
puts "#{x} => #{y}"
}
I'd been trying to build a menu (in Camping and Markaby) using a hash.
Each item has 2 elements: a menu label and a URL, so a hash seemed right, but the '/' URL for 'Home' always appeared last (as you'd expect for a hash), so menu items appeared in the wrong order.
Using an array with each_slice does the job:
['Home', '/', 'Page two', 'two', 'Test', 'test'].each_slice(2) do|label,link|
li {a label, :href => link}
end
Adding extra values for each menu item (e.g. like a CSS ID name) just means increasing the slice value. So, like a hash but with groups consisting of any number of items. Perfect.
So this is just to say thanks for inadvertently hinting at a solution!
Obvious, but worth stating: I suggest checking if the length of the array is divisible by the slice value.
If you use the enumerable mixin (as Rails does) you can do something similar to the php snippet listed. Just use the each_slice method and flatten the hash.
require 'enumerator'
['a',1,'b',2].to_a.flatten.each_slice(2) {|x,y| puts "#{x} => #{y}" }
# is equivalent to...
{'a'=>1,'b'=>2}.to_a.flatten.each_slice(2) {|x,y| puts "#{x} => #{y}" }
Less monkey-patching required.
However, this does cause problems when you have a recursive array or a hash with array values. In ruby 1.9 this problem is solved with a parameter to the flatten method that specifies how deep to recurse.
# Ruby 1.8
[1,2,[1,2,3]].flatten
=> [1,2,1,2,3]
# Ruby 1.9
[1,2,[1,2,3]].flatten(0)
=> [1,2,[1,2,3]]
As for the question of whether this is a code smell, I'm not sure. Usually when I have to bend over backwards to iterate over something I step back and realize I'm attacking the problem wrong.
In Ruby 2.1, each_with_index method is removed.
Instead you can use each_index
Example:
a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }
produces:
0 -- 1 -- 2 --
The right way is the one you feel most comfortable with and which does what you want it to do. In programming there is rarely one 'correct' way to do things, more often there are multiple ways to choose.
If you are comfortable with certain way of doings things, do just it, unless it doesn't work - then it is time to find better way.
Using the same method for iterating through both arrays and hashes makes sense, for example to process nested hash-and-array structures often resulting from parsers, from reading JSON files etc..
One clever way that has not yet been mentioned is how it's done in the Ruby Facets library of standard library extensions. From here:
class Array
# Iterate over index and value. The intention of this
# method is to provide polymorphism with Hash.
#
def each_pair #:yield:
each_with_index {|e, i| yield(i,e) }
end
end
There is already Hash#each_pair, an alias of Hash#each. So after this patch, we also have Array#each_pair and can use it interchangeably to iterate through both Hashes and Arrays. This fixes the OP's observed insanity that Array#each_with_index has the block arguments reversed compared to Hash#each. Example usage:
my_array = ['Hello', 'World', '!']
my_array.each_pair { |key, value| pp "#{key}, #{value}" }
# result:
"0, Hello"
"1, World"
"2, !"
my_hash = { '0' => 'Hello', '1' => 'World', '2' => '!' }
my_hash.each_pair { |key, value| pp "#{key}, #{value}" }
# result:
"0, Hello"
"1, World"
"2, !"

Resources