Refactor the following code (it's one of the most straightforward functions you can create in Python): def fact(x): ans = 1 n = 2 while n <= x: ans *= n n += 1 return ans
Also refactor the following to make them faster nPr = lambda x,y: fact(x)/fact(x-y) nCr = lambda x,y: nPr(x,y)/fact(y)
also, please help me compolete this project. I couldn't figure it out :( """ TIC-TAC-TOE In the default implementation of TicTacToe, the Computer opponent never loses. The class hierarchies are as follows: Object ## All classes inherit from Object. Game ## You subclass Game to get the game you want. TicTacToe Player ## You subclass Player to get the player you want. ComputerPlayer HumanPlayer GameRoom """ # TO DO: # Implement the abstract Object class correctly. # Implement the Game class correctly. # Implement a TicTacToe class correctly. # Implement the Player class correctly. # Implement the HumanPlayer class correctly. # Implement the ComputerPlayer class correctly. # Implement the GameRoom class correctly. # Implement variants of the TicTacToe Game where the computer does lose. # Implement computer-only games of TicTacToe # Implement player-only games of TicTacToe # Implement a GUI correctly. #______________________________________________________________________________ # # Imports: import random #______________________________________________________________________________ class Object(object): """ Class Template. All objects inherit from this class. """ def __repr__(self): return '<%s>' % getattr(self, '__name__', self.__class__.__name__) def printClassInfo(self): print self.__doc__ def printClassMethods(self): methodlist = dir(self) dirlength = len(methodlist) for index in xrange(dirlength): print '%d. %s' % (index+1, methodlist[index]) print def test(self): """ Abstract test method. """ print "I don't know how to test your class." #______________________________________________________________________________ class Game(Object): """ Some abstract methods for Game objects. """ def newGame(self): """ Starts a new game. """ print "I don't know how to start a new game." #______________________________________________________________________________ class TicTacToe(Game): """ This class simulates the game of Tic-Tac-Toe with a 3x3 board, storing the state of the board as well as checking to see if the board is in a 'won' state. By default, computer should never lose. The 3x3 Tic-Tac-Toe board is implemented as a list of 3 lists, each containing 3 elements corresponding to the state of the board. 0 corresponds to a blank tile. 1 corresponds to an X on the tile. -1 corresponds to a O on the tile. """ def __init__(self): self.moveset = {7:0,8:1,9:2,4:3,5:4,6:5,1:6,2:7,3:8} self.emptyBoard() self.winner = 0 self.validstates = [-1, 0, 1] self.states = {-1:'O', 0:' ', 1:'X'} self.humanstate = self.states[1] self.computerstate = self.states[-1] def emptyBoard(self): """ Sets the board to the empty, initial state. Used to initialize the board at the beginning of the game, as well as during the initialization of the TicTacToe instance. """ self.board = [[0,0,0],[0,0,0],[0,0,0]] def setBoard(self, board): """ This method enables the specification of a board with predetermined states. """ # Might change it so that only lists of -1, 0, or 1 are allowed self.board = board def getBoard(self): """ Returns a list of lists, with each list corresponding to a row within the Tic-Tac-Toe board. """ return self.board def setTile(self, row, column, state): """ Alters the state of a board tile at a particular position. Returns if the state argument is not a valid state. """ if state in self.validstates: self.board[row][column] = state def getTile(self, row, column): """ Returns the state of the board tile at a particular position. The value returned is an integer that is either -1, 0, or 1. """ return self.board[row][column] def displayBoard(self): """ Prints out the board on the console output. """ print "_____" for row in xrange(3): for column in xrange(3): print "%s" % (self.states[self.getTile(row, column)]), print print "_____" def checkWinner(self): """ This method checks the board to see if the states in the board satisfy the winning conditions of Tic-Tac-Toe. Returns the winning state (-1 or 1) of the board if the game is won. Otherwise, returns 0. """ for i in xrange(3): if (self.getTile(i,0) and self.getTile(i,1) == self.getTile(i,0) and self.getTile(i,2) == self.getTile(i,0)): return self.getTile(i,0) if (self.getTile(0,i) and self.getTile(1,i) == self.getTile(0,i) and self.getTile(2,i) == self.getTile(0,i)): return self.getTile(0,i) if not self.getTile(1,1): return 0 if (self.getTile(1,1) == self.getTile(0,0) and self.getTile(2,2) == self.getTile(0,0)): return self.getTile(0,0) if (self.getTile(1,1) == self.getTile(2,0) and self.getTile(0,2) == self.getTile(1,1)): return self.getTile(1,1) return 0 def displayState(self, statenumber): """ Returns the state (string) corresponding to the statenumber (int). Returns None if the state is blank i.e. 0. """ return self.states[statenumber] def playRound(self): """ Plays one round of the Tic-Tac-Toe game. """ self.emptyBoard() print "Board positions are numbered as:\n1 2 3\n4 5 6\n7 8 9" print "\nOn the numpad, those are:\n7 8 9\n4 5 6\n1 2 3" print "\nYou have %s, I have %s.\n" % (self.humanstate, self.computerstate) def test(self): """ Test method used to test other methods. """ self.setBoard([[1,1,1],[0,0,0],[0,0,0]]) self.displayBoard() print self.checkWinner() self.setBoard([[-1,0,0],[-1,0,0],[-1,0,0]]) self.displayBoard() print self.checkWinner() #______________________________________________________________________________ class Player(Object): """ Template for both the human and the computer player. """ pass class ComputerPlayer(Player): """ Template for the computer player. """ def __init__(self): pass class HumanPlayer(Player): """ Template for the human player. """ def __init__(self): pass #______________________________________________________________________________ class GameRoom(Object): """ An environment for the Tic-Tac-Toe game. """ def __init__(self): pass def announceWinner(self): pass def playGame(self): pass #______________________________________________________________________________ # # Top Level Functions: def main(): """ Entry point. """ thegame = TicTacToe() thegame.playRound() thegame.test() if __name__ == '__main__': main() #______________________________________________________________________________
Join our real-time social learning platform and learn together with your friends!