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 operator as op | |
def ncr(n, x): | |
x = min(n, n-x) | |
if x == 0: return 1 | |
num = reduce(op.mul, xrange(n, n-x, -1)) | |
denom = reduce(op.mul, xrange(1, x+1)) | |
return num // denom |
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
from math import erf | |
def F(x, mew, sd): | |
return 0.5 * (1 + erf((x-mew)/(sd * pow(2, 0.5)))) |
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
def hexdump(src, length=16): | |
''' | |
:param src: https://github.com/ActiveState/code/tree/master/recipes/Python/142812_Hex_dumper/ | |
''' | |
FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)]) | |
N=0 | |
result='' | |
while src: | |
s,src = src[:length],src[length:] |
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 os | |
import socket | |
if os.name != "nt": | |
import fcntl | |
import struct | |
def get_interface_ip(ifname): | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', |
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
def up(inp_str): | |
if ' ' in inp_str: | |
print "No spaces" | |
return | |
else: | |
return ''.join([chr(ord(x) & 95) for x in inp_str]) |
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
def transpose(matrix): | |
return [[row[i] for row in matrix] for i in range(len(matrix[0]))] | |
# this one is a bit slower | |
def t2(matrix): | |
return map(list, zip(*matrix)) |
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
def ranking(arr): | |
cp = [] | |
for x in arr: | |
if cp.count(x) == 0: | |
cp.append(x) | |
cp.sort() | |
ranks = {x:y for x,y in zip(cp, range(1, len(cp)+1))} | |
return [ranks[x] for x in arr] |
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
def main(): | |
n = int(raw_input().strip()) | |
X = map(float, raw_input().strip().split(' ')) | |
Y = map(float, raw_input().strip().split(' ')) | |
x_rank = ranking(X) | |
y_rank = ranking(Y) | |
d_sq = [(x-y)**2 for x,y in zip(x_rank, y_rank)] | |
print round(1-((6.0*sum(d_sq))/(n*(n**2 - 1))), 3) |
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
def main(): | |
n = int(raw_input().strip()) | |
X = map(float, raw_input().strip().split(' ')) | |
Y = map(float, raw_input().strip().split(' ')) | |
x_mean = sum(X) / len(X) | |
y_mean = sum(Y) / len(Y) | |
s_devX = std(X, x_mean) | |
s_devY = std(Y, y_mean) | |
covariance = cov(X, Y, x_mean, y_mean) | |
print round(covariance / (s_devX * s_devY), 3) |
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
n = 5 # take input for total lines | |
X = [] | |
Y = [] | |
for _ in xrange(n): | |
x,y = map(int, raw_input().strip().split(' ')) | |
X.append(x) | |
Y.append(y) | |
# input is done | |
x_sum = sum(X) |
OlderNewer