- 대부분 translate이 가장 빠르다고 설명을 하지만, 상황에 따라선 그렇지 않은 경우도 있음. 결국 실제 테스트 후 비교 필요
- ref
- http://iam312.pe.kr/348
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
// 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/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
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
// 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
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
# -*- 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
# 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
package tutorial; | |
message Person { | |
required string name = 1; | |
required int32 id = 2; | |
optional string email = 3; | |
enum PhoneType { | |
MOBILE = 0; | |
HOME = 1; |