Question

In: Computer Science

Creating a list/tuple in python 2. Using a list of lists containing X’s and O’s to...

Creating a list/tuple in python
2. Using a list of lists containing X’s and O’s to represent a tic tac toe board, write code to check if the board has a winner.

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

def hasWinner(board):
for i in range(len(board)):
winner = True
for j in range(1,len(board[i])):
if not (board[i][j] is board[i][0] and board[i][j] in ('X','O')):
winner = False
break
if winner:
return True

for j in range(len(board[0])):
winner = True
for i in range(1,len(board)):
if not (board[i][j] is board[0][j] and board[i][j] in ('X','O')):
winner = False
break
if winner:
return True

winner = True
for i in range(len(board)):
if not (board[i][i] is board[0][0] and board[i][i] in ('X','O')):
winner = False
break
if winner:
return True
winner = True
for i in range(len(board)):
if not (board[i][len(board)-i-1] is board[0][len(board)-i-1] and board[i][len(board)-i-1] in ('X','O')):
winner = False
break
return winner


Related Solutions

Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g. (“John”, “Doe”) that represents a class. Ask the user for a students first and last name and check if that student is a member of the class.
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
Python: I am currently a function that will return a list of lists, with the frequency...
Python: I am currently a function that will return a list of lists, with the frequency and the zip code. However, I am having a difficult time organizing the list in decreasing order of frequency. We are asked to use sort () and sorted () instead of lambda. The function will find 5 digit zip codes, and when it sees a 9-digit zip code, the function needs to truncate the last 4 digits. The following is an example of the...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
Python This part involves creating a function that will manage a List of unique strings. The...
Python This part involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test program that takes a string as input and then calls the function over and over until the...
"Python lists are power data structures. Describe some of the benefits of Python lists" Answer the...
"Python lists are power data structures. Describe some of the benefits of Python lists" Answer the question above in a few sentences.
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name:...
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name: Andy, classes: ET580 MA471 ET574 name: Tim, classes: ET574 MA441 ET575 name: Diane, classes: MA441 MA471 ET574 name: Lucy, classes: ET574 ET575 MA471 name: Steven, classes: ET574 MA441 ET580 b) Implement a dictionary of courses and set each courses enrollment to 0: ET580: 0 ET574: 0 ET575: 0 MA441: 0 MA471: 0 c) Use a loop and if statements to read class data from...
Can anyone use python to solve this problem: Part B: List of lists The aim of...
Can anyone use python to solve this problem: Part B: List of lists The aim of Part B is to learn to work with list of lists. You may want to open the file lab05b_prelim.py in the editor so that you can follow the task description. The first part of the file lab05b_prelim.py defines a variable called datasets. The variable datasets is a list of 4 lists. This part consists of 2 tasks. Task 1: We ask you to type...
Using Python In this assignment we will try to add tuples, lists, if statements and strings...
Using Python In this assignment we will try to add tuples, lists, if statements and strings to our program. For example, you can ask for a user for a couple items, their name and age – and depending on their age, print out a predefined list. You could ask for some string input and decide to do something with the output. You could ask for three items, 1) age, 2) are you a male or female and 3) are your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT