some more bullsh*t

This commit is contained in:
madmaurice 2014-11-04 13:02:25 +01:00
parent 8368c2e76d
commit 71c1288c1d
10 changed files with 233 additions and 21 deletions

19
1000games.py Normal file
View file

@ -0,0 +1,19 @@
from tictactoe import *
wins = {}
game = Game([AI("X"),AI("O")])
for i in range(1000):
winner = game.run(out=False)
game.reset()
if winner in wins:
wins[winner] += 1
else:
wins[winner] = 1
for winner,count in wins.items():
if winner == Board.draw:
print("%d draws." % count)
else:
print("%s has won %d times." % (str(winner),count))

Binary file not shown.

21
ackermann.py Normal file
View file

@ -0,0 +1,21 @@
cache = {}
def ack(m,n):
if (m,n) in cache:
return cache[(m,n)]
elif m == 0:
ans = n+1
elif n == 0:
ans = ack(m-1,1)
else:
ans = ack(m-1, ack(m, n-1))
cache[(m,n)] = ans
return ans
if __name__ == "__main__":
for i in range(6):
for j in range(6):
try:
print("ack(%d,%d) = %d" % (i,j, ack(i,j)))
except RuntimeError:
print("ack(%d,%d) = <StackOverflow>" % (i,j))

34
binomial.py Normal file
View file

@ -0,0 +1,34 @@
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 binomial(n):
i = n
j = 0
for c in pascal(n):
yield (c,i,j)
i -= 1
j += 1
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)

9
flooring.rkt Normal file
View file

@ -0,0 +1,9 @@
#lang racket
(define (f i N M)
(modulo (* (expt i 4) (floor (/ N i))) M))
(define (flooring N M)
(modulo (for/sum ([i (in-range N)]) (f (+ i 1) N M)) M))
(displayln (flooring 12412 5123))

17
logic.rkt Normal file
View file

@ -0,0 +1,17 @@
#lang racket
(define (_not p)
(if (equal? p 1) 0 1))
(define (_or p q)
(if (equal? p 1) 1 (if (equal? q 1) 1 0)))
(define (_and p q)
(_not (_or (_not p) (_not q))))
(define (_xor p q)
(_and (_or p q) (_not (_and p q))))
(for* ([p '(0 1)] [q '(0 1)])
(display p) (display q)
(displayln (_xor p q)))

24
looknsay.py Normal file
View file

@ -0,0 +1,24 @@
def looknsay(n):
s = str(n)
sn = ""
q = 0
c = None
for x in s:
if c != x:
if q > 0:
sn += "%d%s" % (q,c)
q = 1
c = x
else:
q+=1
if q > 0:
sn += "%d%s" % (q,c)
return int(sn)
if __name__ == "__main__":
c = 1
print(c)
for i in range(5):
c = looknsay(c)
print(c)

20
number_of_ways.py Normal file
View file

@ -0,0 +1,20 @@
def number_of_ways(numbers):
s = sum(numbers)
if s%3 != 0:
return 0
s = s/3
t = 2*s
u,v,w = 0,0,0
for x in numbers[:-1]:
print( (u,v,w) )
u += x
if u==t:
w+=v
if u==s:
v+=1
return w
if __name__ == "__main__":
print( number_of_ways([0]*4) )

38
pivottable.py Normal file
View file

@ -0,0 +1,38 @@
import urllib.request
dataurl = "https://gist.githubusercontent.com/coderd00d/ca718df8e633285885fa/raw/eb4d0bb084e71c78c68c66e37e07b7f028a41bb6/windfarm.dat"
class Data(object):
days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
def __init__(self):
self.data = {}
def addData(self, tower, day, energy):
if tower not in self.data:
self.data[tower] = { day : 0 for day in self.days }
self.data[tower][day] += energy
def __str__(self):
s = "| Tower | " + " | ".join(self.days) + " |\n"
for tower in sorted(self.data.keys()):
s += ("| %-5d | " % tower) + " | ".join(["%5d" % self.data[tower][day] for day in self.days]) + " |\n"
return s
@staticmethod
def get_from_url(url):
data = Data()
req = urllib.request.urlopen(url)
rawdata = req.read().decode("utf-8")
req.close()
for line in rawdata.split("\n"):
try:
(tower, day, energy) = line.split(" ")
data.addData(int(tower),day,int(energy))
except Exception:
pass
return data
if __name__ == "__main__":
print(str(Data.get_from_url(dataurl)))

View file

@ -1,5 +1,8 @@
import random import random
def rotate(l):
return l[1:] + l[:1]
class Player(object): class Player(object):
def __init__(self,rep): def __init__(self,rep):
@ -11,10 +14,18 @@ class Player(object):
def count_win_chances(self, board): def count_win_chances(self, board):
chances = 0 chances = 0
for line in board.get_all_lines(): for line in board.get_all_lines():
if all( map( lambda v: v == self or v == Board.empty, line) ): chances += self.win_chance_line(line)
chances += 1
return chances return chances
def win_chance_line(self,line):
c = 1
for p in line:
if p == self:
c += 1
elif p != Board.empty:
return 0
return c
class Board(object): class Board(object):
draw = object() draw = object()
empty = Player(" ") empty = Player(" ")
@ -127,8 +138,38 @@ class Human(Player):
except Exception: except Exception:
print("Try again?") print("Try again?")
def rotate(l): class Random(Player):
return l[1:] + l[:1] def play(self, enemy, board):
fields = list(board.get_empty_fields())
return random.choice(fields)
class Game(object):
def __init__(self, players, size=3):
self.size = size
self.players = players
self.reset()
def reset(self):
self.board = Board(size=self.size)
self.winner = None
def run(self,out=True):
if out:
print(str(self.board))
while self.board.get_winner() is None:
(x,y) = self.players[0].play(self.players[1], self.board)
while self.board.get(x,y) != Board.empty:
print("Fail")
(x,y) = self.players[0].play(players[1], self.board)
self.board.set(x,y,self.players[0])
self.players = rotate(self.players)
if out:
print()
print(str(self.board))
return self.board.get_winner()
def menu(prompt,choices): def menu(prompt,choices):
print("> %s" % prompt) print("> %s" % prompt)
@ -137,34 +178,23 @@ def menu(prompt,choices):
return int(input("Your choice: ")) return int(input("Your choice: "))
if __name__ == "__main__": if __name__ == "__main__":
c = menu("Playmode",["Ai vs Ai","Ai vs Player","Player vs Player"]) c = menu("Playmode",["Ai vs Ai","Ai vs Random","Ai vs Player","Player vs Player"])
if c == 0: if c == 0:
players = [ AI("X"), AI("O") ] players = [ AI("X"), AI("O") ]
elif c == 1: elif c == 1:
players = [ Human("X"), AI("O") ] players = [ AI("X"), Random("O") ]
elif c == 2: elif c == 2:
players = [ Human("X"), AI("O") ]
elif c == 3:
players = [ Human("X"), Human("O") ] players = [ Human("X"), Human("O") ]
else: else:
import sys import sys
sys.exit(0) sys.exit(0)
board = Board(size=3) game = Game(players)
print(str(board)) winner = game.run()
while board.get_winner() is None:
(x,y) = players[0].play(players[1],board)
while board.get(x,y) != Board.empty:
print("Try again!")
(x,y) = players[0].play(players[1],board)
board.set(x,y,players[0])
players = rotate(players)
print()
print(str(board))
winner = board.get_winner()
if winner == Board.draw: if winner == Board.draw:
print("Round draw!") print("Round draw!")