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 / 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)
@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 / LinkedList.java
Last active January 7, 2019 01:33
Linked List implementation #linkedlist #java
/**
* Created by hoangong on 23/12/2015.
*/
public class LinkedList {
private Node head;
private Node tail;
private int count;
public Node getHead() {
@hoangong
hoangong / LongestPalindrome.java
Last active January 10, 2016 03:19
Find the longest palindrome
package com.ho.lp;
import java.util.Scanner;
/**
* Created by hoangong on 24/12/2015.
*/
public class Main {
private String input;
@hoangong
hoangong / JsonEPSerializerCommon.scala
Created February 16, 2016 06:05
Play2 Json custom datatype serialization
val uuidReads = Reads[UUID](i => i match {
case s: JsString => {
JsSuccess(UUID.fromString(s.value))
}
case _ => {
JsError("error")
}
})
val uuidWrites = Writes[UUID](i => JsString(i.toString))
implicit val uuidFormat: Format[UUID] = Format(uuidReads, uuidWrites)
@hoangong
hoangong / JsonEPSerializer.scala
Last active December 31, 2018 11:10
Play2 Json object serialization #play #scala #serialisation
val accessTokenReads = (
(__ \ "token").read[String] and
(__ \ "createdDate").read[LocalDateTime] and
(__ \ "expiredDate").read[LocalDateTime] and
(__ \ "expired").read[Boolean]
) (AccessToken.apply _)
val accessTokenWrites = (
(__ \ "token").write[String] and
(__ \ "createdDate").write[LocalDateTime] and
@hoangong
hoangong / idea
Created April 18, 2016 23:29 — forked from chrisdarroch/idea
Open a project in IntelliJ IDEA from your command line!
#!/bin/sh
# check for where the latest version of IDEA is installed
IDEA=`ls -1d /Applications/IntelliJ\ * | tail -n1`
wd=`pwd`
# were we given a directory?
if [ -d "$1" ]; then
# echo "checking for things in the working dir given"
wd=`ls -1d "$1" | head -n1`
@hoangong
hoangong / git tool setup
Last active January 7, 2019 01:16
git tool setup #git
git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path /Applications/kdiff3.app/Contents/MacOS/kdiff3
git config --global core.editor "atom --wait"
or
git config --global core.editor nano
git config --global push.default simple
@hoangong
hoangong / HealthCheckEndpoint.scala
Last active December 31, 2018 11:09
Generic pojo marshaller for akka http 2.4 #pojo #marsheller #akka-http
import akka.http.scaladsl.marshalling.{Marshaller, ToEntityMarshaller}
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import com.fasterxml.jackson.databind.ObjectMapper
import com.voi.dto.response.HealthCheck
trait HealthCheckEndpoint {
val mapper = new ObjectMapper()
@hoangong
hoangong / Akka Http 2.4 custom unmarshalling
Last active January 6, 2019 00:35
akka http custom serialisation #akka #scala
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller
import akka.stream.Materializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.voi.dto.response.HealthCheck
import com.voi.micro.services.ImplicitPojoMarshaller
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}