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 0000000..a130043 Binary files /dev/null and b/__pycache__/tictactoe.cpython-34.pyc differ diff --git a/ackermann.py b/ackermann.py new file mode 100644 index 0000000..ee6806d --- /dev/null +++ b/ackermann.py @@ -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) = " % (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!")