Skip to content

Instantly share code, notes, and snippets.

@dfeng
dfeng / Lint.py
Created January 8, 2015 17:31
List Integer
from collections import defaultdict
class Lint(defaultdict):
def __init__(self, item):
super(Lint, self).__init__(int)
if isinstance(item, int) or isinstance(item, long):
st = str(item)[::-1]
d = {i: int(st[i]) for i in xrange(len(st))}
elif isinstance(item, dict):
d = item
else:
@dfeng
dfeng / marriage.txt
Created October 20, 2014 18:27
Marriage Limerickroll
marriage is a long-term contract
and if u put it in nominal terms
by eating recklessly,
he is shortening the benefits of this contract
@dfeng
dfeng / Amazing.R
Created October 14, 2014 17:31
Amazing'r
# Source: http://stackoverflow.com/questions/11671731/in-r-how-can-i-make-a-running-count-of-runs
x <- data.frame(end.group=c(0,0,1,0,0,1,1,0,0,0,1,1,1,0,1))
# create groups
x$group <- rev(cumsum(rev(x$end.group)))
# re-number groups from smallest to largest
x$group <- abs(x$group-max(x$group)-1)
x$group.count <- ave(x$end.group, x$group, FUN=seq_along)
# Even Better: http://stackoverflow.com/questions/9961700/how-to-partition-when-ranking-on-a-particular-column
@dfeng
dfeng / p458.py
Last active August 29, 2015 13:56
458
from numpy import *
# The Matrix!
x = matrix([
[1,6,0,0,0,0],
[1,1,5,0,0,0],
[1,1,1,4,0,0],
[1,1,1,1,3,0],
[1,1,1,1,1,2],
[1,1,1,1,1,1],
@dfeng
dfeng / p93.py
Created February 8, 2014 20:36
Derek's 93
import itertools
# using reverse polish notation
def calculate(expr):
stack = list()
for e in expr:
if type(e) == int:
stack.append(e)
else:
@dfeng
dfeng / 60.R
Created February 8, 2014 03:03
Dana's 60
N <- 100000000
isprime <- rep(1,N)
for (i in 2:(floor(sqrt(N)))) {
if (isprime[i]==1) {
for (k in i:(floor(N/i))) {
isprime[i*k] <- 0
}
}
print(i)
}
@dfeng
dfeng / p60.py
Last active August 29, 2015 13:56
Derek's 60
# The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
# Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
def primes(n):
ret = list()
multiples = set()
for i in xrange(2, n+1):
if i not in multiples:
ret.append(i)
@dfeng
dfeng / a.py
Created November 30, 2013 05:48
GCJ13 Round 1B a
import sys
sys.setrecursionlimit(100000)
# dynamic programming
def fun(A, motes):
# print A, motes
if A == 1:
return len(motes)
if len(motes) == 0:
return 0
@dfeng
dfeng / screenshot_interactive
Created November 14, 2013 21:57
Dropbox Screenshot (Interactive)
#!/bin/sh
FILE=$(date +%Y%m%d-%H%M%S)
cd ~/Dropbox/Public/Screenshots
screencapture -i $FILE.png
echo "https://dl.dropboxusercontent.com/u/*/Screenshots/$FILE.png" | pbcopy
@dfeng
dfeng / randomize.R
Last active December 11, 2015 20:58
# randomize 1:nSongs across each iteration (?)
# be aware of the songs in the basedir, so that we don't resing songs
beMarkov <- TRUE # flag: don't do previous songs
Sys.setlocale("LC_ALL", "chs")
basedir <- "D:/Karaoke/To Sing/" #"C:/Users/Susan/Dropbox"
outdir <- "D:/Karaoke/To Sing/"
addLeadZero <- function(x, n=2) {
x <- as.character(x)