Skip to content

Instantly share code, notes, and snippets.

View Kroisse's full-sized avatar

Eunchong Yu Kroisse

View GitHub Profile
// http://euler.synap.co.kr/prob_detail.php?id=10
// rustc 0.6
use core::num::{NumCast, Round};
fn seive(n: uint, blk: &fn(&uint) -> bool) {
let mut v = vec::from_fn(n - 2, |i| i + 2);
let mut i = 0;
let limit = NumCast::from(float::sqrt(n as float).ceil());
while v.len() > 0 && i <= limit {
// http://euler.synap.co.kr/prob_detail.php?id=11
// rustc 0.6
static data: &'static str ="
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
// http://www.smartstudy.co.kr/data/dev_2012.html
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_round() {
assert!(round(1.2) == 1);
assert!(round(1.7) == 2);
// http://www.smartstudy.co.kr/data/dev_2012.html
use core::to_bytes::{ToBytes, IterBytes};
pub fn round(value: float) -> int {
let base = value as int;
let frac = value - base as float;
if frac <= -0.5 {
base - 1
import contextlib
@contextlib.contextmanager
def connection(self):
c = self.pool.acquire()
try:
yield c
except httplib.HTTPException:
self.pool.abandon(c)
@Kroisse
Kroisse / pool.py
Last active December 17, 2015 17:39 — forked from eungju/pool.py
class KyotoTycoon(object):
...
def get(self, key):
with self.pool.acquire() as c:
return c.get(key, db=self.db)
# example #2
def set(self, key, value):
conn = self.pool.acquire()
with conn:
#!/usr/bin/env python
import sys
import text
if __name__ == '__main__':
for line in sys.stdin:
line = line.decode('utf-8').rstrip(u'\n')
while True:
a, line = text.east_asian_partition(line, 44)
a = a.rstrip(u'\0')
@Kroisse
Kroisse / gist:6363281
Created August 28, 2013 08:00
Python indexer sample like C#
class indexer(object):
def __init__(self, getter, setter=None, deleter=None, doc=None):
self._getter = getter
self._setter = setter
self._deleter = deleter
self.__doc__ = doc or getter.__doc__
self._delegates = {}
def setter(self, func):
self._setter = func
@Kroisse
Kroisse / gist:6473348
Last active December 22, 2015 12:38
from_bytes()
use std::to_bytes::*;
use std::num::{Zero, Add, Shl, NumCast};
use N = std::i16;
trait FromBytes {
fn from_bytes(bytes: &[u8], lsb0: bool) -> Self;
}
impl<N: Zero + Add<N, N> + Shl<N, N> + NumCast> FromBytes for N {