Skip to content

Instantly share code, notes, and snippets.

View hoangong's full-sized avatar
🏠
Working from home

Hoang Ong hoangong

🏠
Working from home
View GitHub Profile
@hoangong
hoangong / JennyFruitBasket.scala
Created December 8, 2015 18:01
Fruit basket challenge
/**
* 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
@hoangong
hoangong / Tree.scala
Created December 8, 2015 17:58
Lowest common ancestor. One of a very common binary tree puzzle. The implementation in scala is in about 10 lines
/**
* 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)