Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using python import random class PuzzlePiece(object): def __init__(self,secret_id=None,connected_pieces=[]): self.cp=connected_pieces self.si=secret_id def connect_to(self,other): other.cp.append(self) self.cp.append(other) def __str__(self): return str(self.si) Define a Puzzle class. This class will

Using python

import random

class PuzzlePiece(object):

def __init__(self,secret_id=None,connected_pieces=[]):

self.cp=connected_pieces

self.si=secret_id

def connect_to(self,other):

other.cp.append(self)

self.cp.append(other)

def __str__(self):

return str(self.si)

image text in transcribed

Define a Puzzle class. This class will actually create and keep track of all the puzzle pieces. The initialization method (or "constructor method", as it is usually called) should create a bunch of puzzle pieces and connect them. For simplicity, let's assume we want to create 100 puzzle pieces on a 10x10 grid, and each piece is connected to the ones directly above, below, to the left, or to the right of it. Thus, most pieces (but not all) will be connected to exactly 4 other pieces. Needless to say, you should create each piece using the PuzzlePiece class. When you create each piece, set its secret_id to be its coordinates on the grid. In the end, after creating and connected all the puzzle pieces, you should store them all in a list using the attribute pieces (feel free to store them in "matrix" format as a list of lists, with each list representing one row of the grid). The following methods must also be defined in this class: a) Define a random_piece method which takes an input exclude (as well as self) that gener- ates and returns a random puzzle piece from the pieces attribute. The input exclude will be a list of puzzle pieces that you wish to exclude from being randomly generated, so make sure you return a random puzzle piece that is *not* in exclude. You can import a module to generate random numbers for this. b) Define a p-solve method which uses the random-piece method above to randomly pick puzzle pieces and see if they go together, following the description in the introduction. First gener- ate a puzzle piece to start with, and from then on generate a new puzzle piece and see if it fits or not. If it doesn't, it gets thrown back to possibly get picked up again later (to keep things simple, its ok if the same piece gets picked up multiple times in a row). Eventually, you build outward from one piece to put all the pieces together. You should return the list of pieces you successfully put together *in the order that you put them together*. Needless to say, this list should include all the puzzle pieces. You should also print the number of times you had to pick a random piece. Needless to say, this number might be fairly large since you will likely pick up a lot of pieces that don't fit at first. Oh, and I almost forgot: for full credit this method must be written recursively! If you find this confusing, you might want to first write it non-recursively and then see if you can convert it to being recursive. Also, let me emphasize that this method should only check individual puzzle pieces to see if they are connected using the connected pieces attribute of the PuzzlePiece classate Wi You should NOT use the secret.id attribute or the puzzle grid or any other outside knowledge tgettings determine if two pieces go together or not. Define a Puzzle class. This class will actually create and keep track of all the puzzle pieces. The initialization method (or "constructor method", as it is usually called) should create a bunch of puzzle pieces and connect them. For simplicity, let's assume we want to create 100 puzzle pieces on a 10x10 grid, and each piece is connected to the ones directly above, below, to the left, or to the right of it. Thus, most pieces (but not all) will be connected to exactly 4 other pieces. Needless to say, you should create each piece using the PuzzlePiece class. When you create each piece, set its secret_id to be its coordinates on the grid. In the end, after creating and connected all the puzzle pieces, you should store them all in a list using the attribute pieces (feel free to store them in "matrix" format as a list of lists, with each list representing one row of the grid). The following methods must also be defined in this class: a) Define a random_piece method which takes an input exclude (as well as self) that gener- ates and returns a random puzzle piece from the pieces attribute. The input exclude will be a list of puzzle pieces that you wish to exclude from being randomly generated, so make sure you return a random puzzle piece that is *not* in exclude. You can import a module to generate random numbers for this. b) Define a p-solve method which uses the random-piece method above to randomly pick puzzle pieces and see if they go together, following the description in the introduction. First gener- ate a puzzle piece to start with, and from then on generate a new puzzle piece and see if it fits or not. If it doesn't, it gets thrown back to possibly get picked up again later (to keep things simple, its ok if the same piece gets picked up multiple times in a row). Eventually, you build outward from one piece to put all the pieces together. You should return the list of pieces you successfully put together *in the order that you put them together*. Needless to say, this list should include all the puzzle pieces. You should also print the number of times you had to pick a random piece. Needless to say, this number might be fairly large since you will likely pick up a lot of pieces that don't fit at first. Oh, and I almost forgot: for full credit this method must be written recursively! If you find this confusing, you might want to first write it non-recursively and then see if you can convert it to being recursive. Also, let me emphasize that this method should only check individual puzzle pieces to see if they are connected using the connected pieces attribute of the PuzzlePiece classate Wi You should NOT use the secret.id attribute or the puzzle grid or any other outside knowledge tgettings determine if two pieces go together or not

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

Discuss the key components of PMSs.

Answered: 1 week ago

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago