Skip to content

Instantly share code, notes, and snippets.

@hyunjun
hyunjun / MaximizingXor.scala
Created December 11, 2014 00:45
[hackerrank] maximizing xor
// https://www.hackerrank.com/challenges/maximizing-xor
object Solution {
def maximizingXor(l: Int, r: Int): Int = {
val s = for ( i <- l to r;
j <- i to r )
yield i ^ j
s.reduceLeft(_ max _)
}
def main(args: Array[String]) {
var _l: Int = Console.readInt
@hyunjun
hyunjun / UtopianaTree.scala
Created December 11, 2014 00:42
[hackerrank] utopian tree
// https://www.hackerrank.com/challenges/utopian-tree
object Solution {
def utopianTree(n: Int): Int = {
var c: Int = 1
for ( i <- 1 to n )
if ( i % 2 == 1 )
c *= 2
else
c += 1
c
@hyunjun
hyunjun / IsFiboFast.scala
Created December 11, 2014 00:39
[hackerrank] is fibo
// https://www.hackerrank.com/challenges/is-fibo
object Solution {
val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map(p => p._1 + p._2)
def isFibo(n: BigInt): String = {
val f = fibs.dropWhile(_ < n)(0)
if ( f == n )
"IsFibo"
else
"IsNotFibo"
}
@hyunjun
hyunjun / findDigits.scala
Last active August 29, 2015 14:11
[hackerrank] find digits
object Solution {
def findDigit(s: String): Int = {
var cnt: Int = 0
val n: BigInt = BigInt(s)
for (c <- s) {
val i = c.asDigit
if ( i != 0 && n % i == 0 )
cnt += 1
}
cnt
@hyunjun
hyunjun / closure.c
Created December 8, 2014 01:08
closure
// http://en.wikipedia.org/wiki/First-class_function#Non-local_variables_and_closures
#include <stdio.h>
typedef struct {
int (*f)(int, int, int);
int *a;
int *b;
} closure_t;
void map(closure_t *closure, int x[], size_t n) {
@hyunjun
hyunjun / test_queue1.py
Created December 2, 2014 07:40
python multiprocessing
import multiprocessing
def consumer(input_q):
while True:
item = input_q.get()
print(item)
input_q.task_done()
@hyunjun
hyunjun / python_string_concat.py
Created December 1, 2014 05:41
python string concatenation
# -*- coding: utf-8 -*-
from itertools import product
import operator
import timeit
CNT = 1000
def m10():
res = ''
@hyunjun
hyunjun / timer.py
Created November 18, 2014 22:41
timer using contextmanager
# https://docs.python.org/dev/library/contextlib.html#contextlib.ContextDecorator
# http://preshing.com/20110924/timing-your-code-using-pythons-with-statement/
$ python2.7
Python 2.7.6 (default, Nov 26 2013, 12:13:15)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from contextlib import contextmanager
>>> import time
>>> @contextmanager
... def timer():
@hyunjun
hyunjun / addressbook.proto
Last active August 29, 2015 14:09
protocol-buffer
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
@hyunjun
hyunjun / python_strip_punctuation_from_string.md
Created November 11, 2014 08:24
python strip punctuation from string