Skip to content

Instantly share code, notes, and snippets.

View volf52's full-sized avatar
👽
I may be slow to respond.

Muhammad Arslan volf52

👽
I may be slow to respond.
  • Sophia-Antipolis, France
View GitHub Profile
@volf52
volf52 / comb.py
Last active September 1, 2017 12:30
Combination nCr in Python
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
@volf52
volf52 / normal_distrib.py
Last active September 1, 2017 12:31
Cumulative Distribution Function for Normal Distributions in Python
from math import erf
def F(x, mew, sd):
return 0.5 * (1 + erf((x-mew)/(sd * pow(2, 0.5))))
@volf52
volf52 / hexdump.py
Last active September 2, 2017 17:45
Create hexdump of a string - Python
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:]
@volf52
volf52 / get_ip.py
Created September 3, 2017 15:14
Get IP address of host
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',
@volf52
volf52 / up.py
Last active September 22, 2017 12:35
Implementation of upper() function using bit-manipulation - Python
def up(inp_str):
if ' ' in inp_str:
print "No spaces"
return
else:
return ''.join([chr(ord(x) & 95) for x in inp_str])
@volf52
volf52 / transpose.py
Last active September 5, 2017 11:31
Transpose a matrix (2D array) in Python
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))
@volf52
volf52 / ranking.py
Created September 5, 2017 08:32
Ranking a data set
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]
@volf52
volf52 / spearman_unique.py
Created September 5, 2017 08:33
Spearman's Rank Correlation Coefficient (Unique Data Sets) - Python
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)
@volf52
volf52 / pearson_corr.py
Created September 5, 2017 08:35
Pearson Correlation Coefficient, Covariance
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)
@volf52
volf52 / linear_regression_normal_method.py
Created September 5, 2017 09:06
Least Square Regression Line - Python
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)