Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / NumOfDeletion.java
Created December 11, 2014 02:32
[hackerrank] alternating characters
import java.util.Scanner;
public class Test {
public static int numOfDeletion(final String s) {
int cnt = 0;
char prev = s.charAt(0);
for ( int i = 1; i < s.length(); ++i ) {
char c = s.charAt(i);
if ( prev == c )
@hyunjun
hyunjun / numOfReduction.java
Created December 12, 2014 01:26
number of reduction to make palindrome
import java.util.Scanner;
public class Solution {
public static int numOfReduction(final String s) {
int cnt = 0;
int l = 0;
int r = s.length() - 1;
while ( l < r ) {
int lNum = s.charAt(l);
@hyunjun
hyunjun / DistributionOfCandies.java
Created December 16, 2014 04:25
[hackerrank] angry children
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static int distributionOfCandies(final int K, final List<Integer> packets) {
int min = Integer.MAX_VALUE;
Collections.sort(packets);