This file contains 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
# 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(): |
This file contains 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
# -*- coding: utf-8 -*- | |
from itertools import product | |
import operator | |
import timeit | |
CNT = 1000 | |
def m10(): | |
res = '' |
This file contains 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
import multiprocessing | |
def consumer(input_q): | |
while True: | |
item = input_q.get() | |
print(item) | |
input_q.task_done() | |
This file contains 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
// 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) { |
This file contains 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
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 |
This file contains 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
// 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" | |
} |
This file contains 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
// 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 |
This file contains 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
// 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 |
This file contains 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
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 ) |
This file contains 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
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); |