From 71c1288c1dcf3b54f6f45ca67a2b714f408d8944 Mon Sep 17 00:00:00 2001 From: Valentin Gehrke Date: Tue, 4 Nov 2014 13:02:25 +0100 Subject: [PATCH] some more bullsh*t --- 1000games.py | 19 +++++++ __pycache__/tictactoe.cpython-34.pyc | Bin 0 -> 8062 bytes ackermann.py | 21 ++++++++ binomial.py | 34 +++++++++++++ flooring.rkt | 9 ++++ logic.rkt | 17 +++++++ looknsay.py | 24 +++++++++ number_of_ways.py | 20 ++++++++ pivottable.py | 38 ++++++++++++++ tictactoe.py | 72 +++++++++++++++++++-------- 10 files changed, 233 insertions(+), 21 deletions(-) create mode 100644 1000games.py create mode 100644 __pycache__/tictactoe.cpython-34.pyc create mode 100644 ackermann.py create mode 100644 binomial.py create mode 100644 flooring.rkt create mode 100644 logic.rkt create mode 100644 looknsay.py create mode 100644 number_of_ways.py create mode 100644 pivottable.py diff --git a/1000games.py b/1000games.py new file mode 100644 index 0000000..0e38e9e --- /dev/null +++ b/1000games.py @@ -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)) diff --git a/__pycache__/tictactoe.cpython-34.pyc b/__pycache__/tictactoe.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a1300432dd14181d9e903099362abaa7e93f5c89 GIT binary patch literal 8062 zcmb_hOK)4p6`r~G@*#?}WJ{(jKVsUgZP2mhz)hUgRc$$T;uu90BTD0#sKL;@*D@uF zl;%>lL`a*;ZPG!n|3Ehdx+~CMP@q5;-Q8_B^`@($3$OZpXD%;EnQ5A$lx8^3 zGjq;-=P|>7e(w{!r`M^ammVaH|I>cB9JAEY+h z6LmpVz?ZYQ?hs2OWUt6Ea}~GC<`O6-;vw zW)?}OC1<%*KKY!dy;#T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0`hr3g77GlX>3maKymK_}?{~t>2(CWZcW=_M&q{m-Q)~)93XGeaK1c zj6Ujgv-VIoTZ^!T`+N(5DL63SW62%P1iSB|?H5RbT||AUyl2=p8XxbYCqbU zI01NhhboC{kdx*x9I%`*9E1$sIHvAdhXUos1pJFf@UFobbkk@2Ier)Jw>M46diAR% zNQ+{Q#)8%+lpH^-nFZ9%b!2?kz;AH){enNtFbW9GF?Q0T46sX1BX129euq+gu|J?^ z^$~3r(Ne*W6l?H2)oLY6=Y*drf^POkqx2*!-Dq_4wq)&KuV$TCcQSTn7->t9{y5U_ z3r?O>$HXkbi1N|5#B1-V?)aIo^tcw5p0-=3%7?9yCNRt!%reYG(-{GXLou|SaxAi4 zd(2a8Biir?yp12Ef~`g;@|I}{<+Sgxe~wunrXvs#W#{d$GS_IE!pq3`%Sv&kIA1(p a%oS6`" % (i,j)) diff --git a/binomial.py b/binomial.py new file mode 100644 index 0000000..3ecf638 --- /dev/null +++ b/binomial.py @@ -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) diff --git a/flooring.rkt b/flooring.rkt new file mode 100644 index 0000000..7b57467 --- /dev/null +++ b/flooring.rkt @@ -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)) diff --git a/logic.rkt b/logic.rkt new file mode 100644 index 0000000..3a852d1 --- /dev/null +++ b/logic.rkt @@ -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))) diff --git a/looknsay.py b/looknsay.py new file mode 100644 index 0000000..23f4fbc --- /dev/null +++ b/looknsay.py @@ -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) + diff --git a/number_of_ways.py b/number_of_ways.py new file mode 100644 index 0000000..b6c0756 --- /dev/null +++ b/number_of_ways.py @@ -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) ) diff --git a/pivottable.py b/pivottable.py new file mode 100644 index 0000000..ce56b0c --- /dev/null +++ b/pivottable.py @@ -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))) diff --git a/tictactoe.py b/tictactoe.py index 7d3b14e..167b110 100644 --- a/tictactoe.py +++ b/tictactoe.py @@ -1,5 +1,8 @@ import random +def rotate(l): + return l[1:] + l[:1] + class Player(object): def __init__(self,rep): @@ -11,10 +14,18 @@ class Player(object): def count_win_chances(self, board): chances = 0 for line in board.get_all_lines(): - if all( map( lambda v: v == self or v == Board.empty, line) ): - chances += 1 + chances += self.win_chance_line(line) 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): draw = object() empty = Player(" ") @@ -127,8 +138,38 @@ class Human(Player): except Exception: print("Try again?") -def rotate(l): - return l[1:] + l[:1] +class Random(Player): + 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): print("> %s" % prompt) @@ -137,34 +178,23 @@ def menu(prompt,choices): return int(input("Your choice: ")) 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: players = [ AI("X"), AI("O") ] elif c == 1: - players = [ Human("X"), AI("O") ] + players = [ AI("X"), Random("O") ] elif c == 2: + players = [ Human("X"), AI("O") ] + elif c == 3: players = [ Human("X"), Human("O") ] else: import sys 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: print("Round draw!")