more shit

This commit is contained in:
madmaurice 2015-04-22 18:33:54 +02:00
parent 9f338ba2d7
commit c6b545836a
10 changed files with 309 additions and 32 deletions

84
akstest.py Normal file
View file

@ -0,0 +1,84 @@
'''
cache = {}
def binomcoeffb(n,k):
nom = 1
denum = 1
for i in range(n-k):
nom *= (n-i)
denum *= (i+1)
v = nom//denum
cache[(n,k)] = v
return v
def binomcoeffc(n,k):
if (n,k) in cache:
return cache[(n,k)]
if n == k or k == 0:
return 1
v = binomcoeffc(n-1,k-1) + binomcoeffc(n-1,k)
cache[(n,k)] = v
return v
def binomcoeff(n,k):
try:
v = binomcoeffc(n,k)
except RuntimeError:
v = binomcoeffb(n,k)
return v
def binomcoeffi():
l = [1,2,1]
i = 3
yield (i,l[1:-1])
while True:
i += 1
nl = [1]
for j in range(len(l)-1):
nl.append( l[j]+l[j+1] )
nl.append(1)
l = nl
yield (i,l[1:-1])
def is_prime(n):
if n <= 1:
return False
for n
bc = binomcoeff(n,k)
r = bc % n
# print("%d %% %d = %d" % (bc,n,r))
if r != 0:
return False
return True
'''
class PrimeGenerator:
def __init__(self):
self.coeffs = [1,1]
self.prime = 1
def nextPrime(self):
self.step()
while not self.check():
self.step()
return self.prime
def step(self):
l = [1]
for j in range(len(self.coeffs)-1):
l.append(self.coeffs[j] + self.coeffs[j+1])
l.append(1)
self.coeffs = l
self.prime += 1
def check(self):
for i in range(1,len(self.coeffs)//2+1):
if self.coeffs[i] % self.prime != 0:
return False
return True
pg = PrimeGenerator()
for i in range(10001):
print("%d: %d" % (i,pg.nextPrime()) )

20
back2black.py Normal file
View file

@ -0,0 +1,20 @@
from sys import stdout as out
p = {}
for i in range(1,91):
p[i] = 0
for i in range(2,91):
n = i
while n <= 90:
p[n]+=1
n += i
for i in range(9):
for j in range(10):
if p[i*10+j+1] % 2 == 0:
out.write("o")
else:
out.write(" ")
out.write("\n")

View file

@ -1,34 +1,13 @@
def pascal(n):
if n == 0:
return [1]
p = [1,1]
for i in range(n-1):
pn = [1]
for j in range(len(p)-1):
pn.append(p[j]+p[j+1])
pn.append(1)
p = pn
return p
def binomcoeff(n,k):
nom = 1
denum = 1
for i in range(n-k):
nom *= (n-i)
denum *= (i+1)
v = nom//denum
return v
def binomial(n):
i = n
j = 0
for c in pascal(n):
yield (c,i,j)
i -= 1
j += 1
def E(n,k):
return binomcoeff(2*n+k,n)*binomcoeff(n+k,n)//2
if __name__ == "__main__":
print("Form: (x+y)^n")
x = float(input("Insert x:"))
y = float(input("Insert y:"))
n = int(input("Insert n:"))
assert(n>0)
res = 0
s = []
for (c,i,j) in binomial(n):
s.append("%d*(%.2f)^%d*(%.2f)^%d" % (c,x,i,y,j))
res += c * x**i * y**j
print(" + ".join(s))
print("Result: %f" % res)
print(E(1,2), E(2,1), E(2,2), E(3,1))

30
check_prime.py Normal file
View file

@ -0,0 +1,30 @@
from sys import argv
def binomcoeff(n,k):
nom = 1
denum = 1
for i in range(n-k):
nom *= (n-i)
denum *= (i+1)
v = nom//denum
return v
def is_prime(n):
if n <= 1:
return False
for k in range(1,n):
bc = binomcoeff(n,k)
r = bc % n
# print("%d %% %d = %d" % (bc,n,r))
if r != 0:
return False
return True
if len(argv) > 1:
p = int(argv[1])
if is_prime(p):
print("%d is a prime" % p)
else:
print("%d is not a prime" % p)
else:
print("%s <number>" % argv[0])

29
crc3.py Normal file
View file

@ -0,0 +1,29 @@
from math import log, floor
tobin = lambda v: v>0 and tobin(v>>1).lstrip('0')+str(v&1) or '0'
def calcremainder(v,coeff):
n = floor(log(coeff,2))
v = v << n
r = v
while (r>>n) > 0:
m = floor(log(r,2))
p = coeff<<(m-n)
r = r ^ p
return v,r
def create(b,coeff=0b1011):
v,r = calcremainder(b,coeff)
return v+r
def check(b,coeff=0b1011):
_,r = calcremainder(b,coeff)
return r == 0
a = int("11010011101100",2)
print( "V:" + tobin( a ) )
c = create(a)+1
print("CRC-Data: " + tobin( c ) )
print("Correct?: " + ( check(c) and "Yes" or "No"))

30
goatdoor.py Normal file
View file

@ -0,0 +1,30 @@
from random import shuffle, randint
result = { "swap": 0, "not_swap": 0 }
doors = [ 0, 0, 1]
door_indices = list(range(3))
for i in range(500):
shuffle(doors)
shuffle(door_indices)
door_chosen, door_opened, door_swap = door_indices
if doors[door_opened] == 1:
door_swap, door_opened = door_opened, door_swap
if doors[door_swap] == 1:
result["swap"] += 1
elif doors[door_chosen] == 1:
result["not_swap"] += 1
else:
print("WTF?")
break
p100 = result["swap"] + result["not_swap"]
p_swap = result["swap"]*100/p100
p_not_swap = result["not_swap"]*100/p100
print("Swap door\t: %.2f%%" % p_swap)
print("Don't swap door\t: %.2f%%" % p_not_swap)

26
johnsbeauty.py Normal file
View file

@ -0,0 +1,26 @@
def countchars(s):
chars = list(map(chr,range(ord('a'),ord('z')+1)))
counts = {}
for c in s.lower():
if c not in chars:
continue
if c not in counts:
counts[c] = 1
else:
counts[c] += 1
keys = [ (c,counts[c]) for c in counts ]
keys.sort(key=lambda c: -c[1])
return keys
def maximumbeauty(s):
clist = countchars(s)
maxbeauty = 0
beauty = 26
for c, count in clist:
maxbeauty += count * beauty
beauty -= 1
return maxbeauty
for s in ["ABbCcc","Good luck in the Facebook Hacker Cup this year!","Ignore punctuation, please :)", "Sometimes test cases are hard to make up.","So I just go consult Professor Dalves"]:
print("%s: %d" % (s, maximumbeauty(s)))

24
piart.py Normal file
View file

@ -0,0 +1,24 @@
import svgwrite as svg
import random
def randomcolor():
r = random.randint(100,200)
g = random.randint(100,200)
b = random.randint(100,200)
return "#%02X%02X%02X" % (r,g,b)
dg = svg.Drawing('piart.svg', size=(1920,1080), profile="full")
#Background
bg = dg.add(dg.g(id='background', fill='#2C2E2F'))
bg.add(dg.rect(insert=(0,0),size=(u'100%',u'100%'),rx=0,ry=0))
#circle
circle = dg.add(dg.g(id='circle'))
for x in range(1,111):
for y in range(1,62):
circle.add(dg.circle(center=(10+5+x*17,10+5+y*17),r=5,fill=randomcolor()))
dg.save()

2
piart.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 324 KiB

53
spr.py Normal file
View file

@ -0,0 +1,53 @@
A = "A"
B = "B"
C = "C"
#D = "D"
E = "E"
F = "F"
nodes = [A,B,C,E,F]
edges = {
(A,B,3),
(A,C,4),
# (B,D,5),
(B,E,2),
# (C,D,1),
# (D,E,2),
# (D,F,4),
(E,F,6)
}
def getadjacent(node, edges):
adjacent = set()
for (s,t,l) in edges:
if s == node:
adjacent.add( (t,l) )
elif t == node:
adjacent.add( (s,l) )
return adjacent
def spr(s,t, nodes, edges):
nodes = set(nodes)
m = { s:(0,[s])}
S = set([s])
S2 = set(S)
while S < nodes:
for s in set(S2):
S2.remove( s )
l,p = m[s]
for (ad,sl) in getadjacent(s, edges):
if ad not in S:
m[ad] = (l+sl,p+[ad])
else:
ad_l,_ = m[ad]
if ad_l > l+sl:
m[ad] = (l+sl,p+[ad])
S.add( ad )
S2.add( ad )
return m[t]
print( spr(A,F, nodes, edges) )