This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Challenge: https://www.reddit.com/r/dailyprogrammer/comments/3v4zsf/20151202_challenge_243_intermediate_jennys_fruit/ | |
* Description | |
Little Jenny has been sent to the market with a 5 dollar bill in hand, to buy fruits for a gift basket for the new neighbors. Since she's a diligent and literal-minded kid, she intends to spend exactly 5 dollars - not one cent more or less. | |
The fact that the market sells fruits per piece at non-round prices, does not make this easy - but Jenny is prepared. She takes out a Netbook from her backpack, types in the unit prices of some of the fruits she sees, and fires off a program from her collection - and voil\u00e0, the possible fruit combinations for a $5 purchase appear on the screen. | |
Challenge: Show what Jenny's program might look like in the programming language of your choice. | |
The goal is aways 500 cents (= $5). | |
Solutions can include multiple fruits of the same type - assume they're available in unlimited quantities. | |
Solutions do not need to include all available t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Lowest common ancestor. One of a very common binary tree puzzle. The implementation in scala is in about 10 lines | |
*/ | |
object Tree { | |
def findLCA(tree: Tree, x: Int, y: Int): Option[Tree] = { | |
if (tree.value == x || tree.value == y) Some(tree) | |
else (if (tree.left != None) findLCA(tree.left.get, x, y) else None, if (tree.right != None) findLCA(tree.right.get, x, y) else None) match { | |
case (Some(r), Some(l)) => Some(tree) | |
case (Some(r), None) => Some(r) | |
case (None, Some(l)) => Some(l) |
NewerOlder