The Frugal Gentleman
George likes to appear generous, but he's also very frugal, especially when he buys wine. Here's his strategy: if there are two or more wines to choose from, he buys the second cheapest. That way, he doesn't appear to buy the cheapest, but he avoid buying the more expensive options. Write a function that takes a collection of wines and returns the name of George's choice.
Examples
(choose-wine [{:name "White" :price 10.99}
{:name "Red" :price 8.74}
{:name "Rosé" :price 12.22}]) ;=> "White"
(choose-wine [{:name "White" :price 10.99}]) ;=> "White"
(choose-wine [{:name "White" :price 10.99}
{:name "Rosé" :price 12.22}]) ;=> "Rosé"
Thanks to this site for the challenge idea where it is considered Hard level in Ruby.
Email submissions to eric@purelyfunctional.tv before August 09, 2020. You can discuss the submissions in the comments below.
We are not interested in sorting the more expensive than the second cheapest wine. Hence my proposal:
EDIT:
But apparently it's underperformant compared to @ninjure's proposal.
EDIT2:
Now that's better: