Category: Python

  • “Analyzing Optimization Problems: Strategies and Mathematical Models”

    Analyze problems 1, 5, 6, and 7.
    –For problem 1, store the solution to part A as a separate file before starting part B. We will return to the part A program later on. What strategies did you try to identify 
    alternate optima?
    –Problem 5 is an extension of problem 1. To work on it, return to the solution in part A.
    –There are no special instructions for problem 6.
    –For problem 7, develop a mathematical model only. Do not program and solve it.
    — Combine the solutions to the problems into a single Word document for 
    submission. Include your programming code for each problem  as an appendix. 
    I have attached a sample code outline as well as the textbook outline. 

  • Tic-Tac-Toe Game with AI “Enhancing Tic-Tac-Toe: Implementing Minimax and Smart AI Players” Tic-Tac-Toe Game with Smart AI and AI Players MiniMax Tic-Tac-Toe AI

    PA 1. Tic-Tac-Toe Game
    Topics Covered
    Modules
    Classes, Inheritance, and Polymorphism
    Recursions
    Game AI and Minimax Algorithm
    Instructions
    Tic-Tac-Toe Game
    Objective: practicing with classes, modules, and recursions
    Description
    In this assignment you will write a program for a popular game Tic-Tac-Toe. Your program will generate a board with nine cells using the traditional 3 x 3 layout and ask each of two users to choose one cell at a time. The users take turns, and each user marks the board cells with a naught O or a cross X. The game continues until either one of the users won or all cells on the board are marked with O or X. The user won when the three marks (O or X) are located on one line horizontally, vertically, or diagonally as shown on the picture below:
    X
    X
    X
    X X X
    X
    X
    X
    There are 8 winning cases in total: 3 horizontally arranged marks, 3 vertically arranged marks, and two diagonally arranged marks. The program writes who is the winner or it is a tie. After that it will ask the users if they want to play again. Here are the snippets of the program output, be aware that your program output should match this format exactly. Please read the instructions carefully and completely before starting to work on your program!
    In the beginning the program should output the following messages and draws the board:
    Welcome to TIC-TAC-TOE Game!
    A B C +—+—+—+
    1| | | |
    +—+—+—+
    2| | | |
    +—+—+—+
    3| | | |
    +—+—+—+
    Bob, X: Enter a cell [A-C][1-3]:If the user (the default first user is Bob) chose cell a1, the program updates the board and produces the following output:
    A B C +—+—+—+
    1| X | | |
    +—+—+—+
    2| | | |
    +—+—+—+
    3| | | |
    +—+—+—+
    Alice, O: Enter a cell [A-C][1-3]:If the user (the default second user is Alice) chose cell a1, the program does not update the board because this cell is already marked with an X. It asks the user to enter valid input in the following way:
    You did not choose correctly.
    Alice, O: Enter a cell [A-C][1-3]:Notice that the second sentence is the same prompt used before. If the user (Alice or Bob) does not choose valid input, the program outputs the same messages as before until the user enters valid input. Valid input is a two-character string, which has the first character a letter A, B, or C (uppercase or lowercase) and the second character is 1, 2, or 3. The program should analyze if input is valid or invalid.
    If Alice enters b2, the program updates the board and produces the following output:
    A B C +—+—+—+
    1| X | | |
    +—+—+—+
    2| | O | |
    +—+—+—+
    3| | | |
    +—+—+—+
    Bob, X: Enter a cell [A-C][1-3]:As you can see, the program makes turns for players: after Bob chose a cell, Alice chooses a cell, and after Alice chose a cell, Bob chooses a cell. When the game is over, the program prints one of the following messages:
    Bob is a winner!
    Would you like to play again? [Y/N]or
    Alice is a winner!
    Would you like to play again? [Y/N]or
    It is a tie!
    Would you like to play again? [Y/N]If the user types ‘Y’ or ‘y’ the program starts a new game, draws the empty board, and prints the following message again:
    A B C +—+—+—+
    1| | | |
    +—+—+—+
    2| | | |
    +—+—+—+
    3| | | |
    +—+—+—+
    Bob, X: Enter a cell [A-C][1-3]:Otherwise it prints the following new message and terminates:
    Goodbye! Tic-Tac-Toe Game AI
    To implement the full version of tic-tac-toe, you need to create an AI (Artificial Intelligence) Player that can play against the user. In this assignment, you have to create three different types of AI: a simple AI that chooses moves randomly, a sophisticated SmartAI that never looses a game because it is based on heuristic approaches, and a MiniMax that also never looses a game because it precalculates all possible moves. All three AI classes should be placed in the same module, the file player.py, where the class Player is written.
    AI should be a subclass of the class Player and should inherit all properties from its superclass Player. The init and choose methods should be overridden (modified). The simplest implementation of an AI player is to use a random choice for generating a valid move. For this strategy, you need to create all possible moves: in the beginning of the game, all moves are empty cells on the board, so you can create a list of all cells and then remove the occupied cells from the board as the game progresses. You can import choice from the random module to randomly choose a cell (a move) from the list of all possible cells (moves).
    The output of the program should be the same as before, the user plays as Alice and the AI plays as Bob. The only difference from the previous tic-tac-toe game is that the user does not have to play for Bob, the AI (your computer program) plays instead of the user. To do so, you need to modify tictac.py to create an AI object: you can achieve it by substituting player1 = Player(“Bob”, “X”) to player1 = AI(“Bob”, “X”, board) and the import statement from player import Player to from player import Player, AI.
    Minimax Algorithm
    To improve the performance of a random-choice AI, you can create another class, MiniMax that uses a minimax algorithm based on recursion. MiniMax should be a subclass of AI and inherit all methods from its superclass AI. The method choose should be overridden and should call the recursive minimax method. The pseudocode for minimax is shown below, “self” refers to the MiniMax player and “opponent” – to its opponent:
    Minimax:
    1. Check the base case: if the game is over, then return -1 if self lost, 0 if it is a tie, or 1 if self won.
    2. Set the min score to infinity and max score to -infinity
    3. Choose a cell (or make a move): a. iterate through all available cells (9 cells)
    b. check if the cell is empty
    c. mark the cell with X or O (if self then mark it with its sign, otherwise mark it with another sign)
    d. get score by calling the minimax recursively (you need to alternate between self and opponent)
    e. update score: if self then use the max score (compare it to the max score), otherwise use the min score (compare it to the min score)
    f. update move: update the cell index
    g. unmark the cell (make it a space again ” “) and reset all other variables that were affected by the game play
    4. If it is the start level (the last level to be executed completely and the last score to be returned) return the move. Let see how it works. At the start, the program chooses the first cell (e.g., ‘A1’) and marks it with X, then on the next level it chooses the next cell (e.g., ‘B1’) and marks it with O, on the third level it chooses the next cell (e.g., ‘C1’) and marks it with X, and so on until there will be a winning condition or a tie. i.e., the game is over. After that, the program returns to the previous upper level and updates the min and max scores depending on whose turn is. The process continues until it reaches the beginning level. At this point the program should return the optimal move as a cell. You can read more about a minimax algorithm here: Minimax – WikipediaLinks to an external site..
    Smart AI Extra Credit (5 points)
    You can improve the performance of a random-choice AI in a different way by creating another class, SmartAI. SmartAI should be placed in the same module, the file player.py, where the class Player and AI are written. SmartAI should be a subclass of the class AI and should inherit all properties from its superclass AI. Only the choose method should be overridden (modified). The output of the program should be the same as before, but this time the user plays as Bob and the SmartAI plays as Alice. The only difference from the previous tic-tac-toe game is that the user does not have to play for Alice, the SmartAI (your computer program) plays instead of the user.
    You need to modify the tictac.py to create a SmartAI object: you can achieve it by substituting player2 = Player(“Alice”, “O”) to player2 = SmartAI(“Alice”, “O”, board) and the import statement from player import Player, AI to from player import Player, AI, SmartAI. The simplest heuristic approach is to check all possible winning conditions. If we assume that the player is Alice and her sign is O, then the program should find two Os in a row, column, or a diagonal and add the missing O to complete them and win the game. The program should also check the winning conditions of the opponent and place O in patterns made of two Xs to prevent the opponent from winning the game. At the beginning of the game, you can start at positions that have high probability of winning (the center or corners). You can read about additional heuristic rules here: Tic-tac-toe – WikipediaLinks to an external site.. After you successfully implemented both SmartAI (Alice) and AI (Bob), you can even make them to play against each other.
    Programming Approaches
    In this assignment you need to create two base classes Board and Player, each class should be written in its own file named board.py and player.py. They should be located in the same directory as the main program tictac.py.
    Class Board should have six methods init, get_winner, set, isempty, isdone, and show. Please read the following code and instructions carefully. You can type or copy and paste this code into your file board.py. The file board.py should be located in the same directory as tictac.py (the main program) and player.py.
    class Board:
    def __init__(self):
    # board is a list of cells that are represented # by strings (” “, “O”, and “X”)
    # initially it is made of empty cells represented # by ” ” strings
    self.sign = ” ”
    self.size = 3
    self.board = list(self.sign * self.size**2)
    # the winner’s sign O or X
    self.winner = “”
    def get_size(self): # optional, return the board size (an instance size)
    def get_winner(self):
    # return the winner’s sign O or X (an instance winner) def set(self, cell, sign):
    # mark the cell on the board with the sign X or O
    # you need to convert A1, B1, …, C3 cells into index values from 1 to 9
    # you can use a tuple (“A1”, “B1″,…) to obtain indexes # this implementation is up to you def isempty(self, cell):
    # you need to convert A1, B1, …, C3 cells into index values from 1 to 9
    # return True if the cell is empty (not marked with X or O)
    def isdone(self):
    done = False
    self.winner = ”
    # check all game terminating conditions, if one of them is present, assign the var done to True
    # depending on conditions assign the instance var winner to O or X
    return done
    def show(self):
    # draw the boardA class Player should have four methods init, get_sign, get_name and choose. Please read the code and instructions carefully. You can type or copy and paste this code into your file player.py. The file player.py should be located in the same directory as tictac.py (the main program) and board.py.
    class Player:
    def __init__(self, name, sign):
    self.name = name # player’s name
    self.sign = sign # player’s sign O or X
    def get_sign(self):
    # return an instance sign
    def get_name(self):
    # return an instance name
    def choose(self, board):
    # prompt the user to choose a cell
    # if the user enters a valid string and the cell on the board is empty, update the board
    # otherwise print a message that the input is wrong and rewrite the prompt
    # use the methods board.isempty(cell), and board.set(cell, sign)In the main program tictac.py, write the following code. Your code should match this code precisely!!!
    # author: Larissa Munishkina
    # date: May 21, 2020
    # file: tictac.py a Python program that implements a tic-tac-toe game
    # input: user responses (strings)
    # output: interactive text messages and a tic-tac-toe board
    from board import Board
    from player import Player
    # main program
    print(“Welcome to TIC-TAC-TOE Game!”)
    while True:
    board = Board()
    player1 = Player(“Bob”, “X”)
    player2 = Player(“Alice”, “O”)
    turn = True
    while True:
    board.show()
    if turn:
    player1.choose(board)
    turn = False
    else:
    player2.choose(board)
    turn = True
    if board.isdone():
    break
    board.show()
    if board.get_winner() == player1.get_sign():
    print(f”{player1.get_name()} is a winner!”)
    elif board.get_winner() == player2.get_sign():
    print(f”{player2.get_name()} is a winner!”)
    else:
    print(“It is a tie!”)
    ans = input(“Would you like to play again? [Y/N]n”).upper()
    if (ans != “Y”):
    break
    print(“Goodbye!”)You can use the following code snippet to start working on MiniMax:
    class MiniMax(?):
    def choose(self, board):
    print(f”n{self.?}, {self.?}: Enter a cell [A-C][1-3]: “)
    cell = MiniMax.minimax(self, board, True, True)
    print(cell)
    board.set(cell, self.sign)
    def minimax(self, board, self_player, start):
    # check the base conditions
    if board.isdone():
    # self is a winner
    if board.get_winner() == ?
    return 1
    # is a tie
    elif board.get_winner() == ?:
    return 0
    # self is a looser (opponent is a winner)
    else:
    return -1
    # make a move (choose a cell) recursively
    # use the pseudocode given to you above to implement the missing code

  • Final Project Submission for Word Guessing Game “Utilizing File Input and Advanced Topics in a Program: Improving Fundamentals and Error Handling”

    For my final project, I started on working on a word guessing game, now I just need to add whats missing for the final project and make sure all the fundamentals are met. 
    Directions: 
    The Final Project is your last opportunity to demonstrate Fundamentals, and it is your only opportunity to earn Ethics and Advanced Topics points (as described below).  The Final Project takes the place of the required final exam, as per university policy.
    Note that this means that even if you have covered all of the Fundamentals points through your Consolidation projects, you should still submit a Final Project that includes a license, etc. in order to get all possible points.  But if your prior projects were very complete, you may need to make only a small addition or two for the Final.
    Submission and Requirements Your project must include: 
    1. Your entire code project submitted as a git repository. This can be accomplished by: – Compressing your entire git repo (folder) as a ZIP file and submitting that, OR – including a link to a GitHub page or other hosting site. 
    2. Your code: – Your program code should be in one or more files with the .py extension. 
    3. A README document – Your README should describe how a user would run and interact with your program. – For example, if your program implements a game, it should describe how the game work. – It’s more important for your README to accurate describe how your program actually behaves instead of just how you think it should behave. – Your README should also credit any other people or sources that provided contributions to your code. (Failure to do so may result in a loss of points or more serious academic consequences.) – Your README should be titled exactly either “README.txt” (for plain text) or “README.md” (for Markdown-formatted text). 
    4. A license as a file called “LICENSE.txt” – You should pick an appropriate license (for example, see here: https://choosealicense.com/Links to an external site. ). If you are unsure, I recommend the MIT License. – You should copy and paste the contents of the license into your LICENSE.txt file. – You should modify the license text to include your name and the current year, as appropriate. 
    5. Any other files needed for your program to work. – For example, if you are using a bank of words or other data for your program to read in, you need to include those files, or you may not earn points for the portions of code that require those files. 
    Grading This project will earn you points towards Fundamentals, according to the example scoring sheet here: There are also 8 points for Ethics and 10 points for Advanced Topics, described below. 
    Advanced Topics 
    You can earn your 10 points in Advanced Topics: Including code that uses the Pandas or Matplotlib libraries to analyze or graph data generated in your program. This could be as simple as graphing final scores, or computing the top score over a record of scores, if you you are implementing a game.
    feed back, 
    there are several things that you’re missing that you should aim to incorporate into your Final if you want a good grade. Here are my suggestions: 
    1. Make sure you include a LICENSE.txt as per the instructions of the Final Project. That alone will get you 8 points. 
    2. Make sure you are submitting your code as a git repo, with a history of several commits. That will get you 4 points. 
    3. Include docstrings for all of your functions is another easy 2 points. 
    4. Completing the End of Course Reflections is another easy 2 points. 
    5. One of the things we did in the Word Counting assignments was to read in a file (using the “with open(…) as” construction). One thing you could do here would be to read in one or more of your word lists from a file. For example, make a file where each line is a word in your list, and then the readlines() function (instead of the simpler read() function) will read all of the lines into a list. Or you could use read() and then split on the line return character “n”. Either way, showing that you can read from a file and using what you read in your program would get you the points for Fundamentals items 4.1 and 4.3, which is another 3 points. 
    6. Another thing that you missed by not turning in Word Counting 2.0 or 3.0 is error handling (items 5.2 and 5.3 on the Fundamentals score sheet). But that’s only 2 points total, so maybe less of a priority. 
    7. Finally, the Advanced Topics points is the other largest point value item. One possibility would be using matplotlib to make a graph showing the final number of guesses or something like that. But this is higher effort than most of the other things from above. 

  • “Drawing a Rectangle with Asterisks”

    “””1. Escribe una función que reciba la altura y el ancho de un rectángulo y lo dibuje utilizando asteriscos.”””
    def rectangulo(al, an):
    for i in range(al):
    print(“*”*an)
    al = int(input(“an: “))
    an = int(input(“al: “))
    rectangulo(al, an)

  • Convolutional Neural Networks Introduction: Convolutional Neural Networks (CNNs) are a type of deep learning model that is commonly used for image recognition and classification tasks. They are inspired by the structure and functioning of the human visual cortex, and

    I already written the code but I want it to be rewritten to be more accurate and clear with no ChatGPT dedicated if found. I attached the files need mine is COE292 and the original assignment is named assignment4 • The assignment is about the Convolution Neural network
    • Use the attached notebook file to solve the assignment (check the first reply)
    • Submit your solution as a python file (.py) not as a notebook (.ipynb). Your submission will not be graded if you submit a notebook (i.e. you will get a 0 if you submit a .ipynb file). Follow the submission direction in the attached notebook to know how to convert a .ipynb file to .py file

  • “Creating an Excel Worksheet: A Step-by-Step Guide”

    follow the instructions for the excel worksheet and how the final product is supposed to be is down below