1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Makovka662 [10]
3 years ago
12

n a particular board game, there is exactly one row and it comprises N spaces, numbered 0 through N - 1 from left to right. Ther

e are also N marbles, numbered 0 through N - 1, initially placed in some arbitrary order. After that, there are two moves available that only can be done one at a time: Switch: Switch the marbles in positions 0 and 1. Rotate: Move the marble in position 0 to position N - 1, and move all other marbles one space to the left (one index lower). The objective is to arrange the marbles in order, with each marble i in position i. 1. Write a class, MarblesBoard, to represent the game above. The class should be initialized with a particular sequence of Marbles. Write an __init__ function that takes a starting sequence of marbles (the number of each marble listed in the positions from 0 to N - 1). (Notice in the sequence all the marbles are different numbers and are sequential numbered but not in order!) Next, write switch() and rotate() methods to simulate the player's moves as described above. Write a method, is_solved(), that returns True if the marbles are in the correct order, False otherwise. Additionally, write __str__ and __repr__ methods to display the current state of the board. Your class should behave like the following example: >>> board
Computers and Technology
1 answer:
Ganezh [65]3 years ago
3 0

Answer:

class MarblesBoard(object):

   def __init__(self, seq):

        self.seq = list(seq)

   def switch(self):

       temp = self.seq[0]

       self.seq[0] = self.seq[1]

       self.seq[1] = temp

   def rotate(self):

       temp = self.seq[0]

       for i in range(1, len(self.seq)):

           self.seq[i-1] = self.seq[i]

       self.seq[-1] = temp

   def is_solved(self):

       for i in range(len(self.seq)):

           if i != self.seq[i]:

               return False

       return True

   def __str__(self):  

       return ' '.join(list(map(str,self.seq)))

   def __repr__(self):

       return ' '.join(list(map(str,self.seq)))

class Solver(object):  

   def __init__(self, board):

       self.board = board

   def solve(self):      

       steps = 0

       while not self.board.is_solved():        

           if self.board.seq[0] > self.board.seq[1] and self.board.seq[0] != len(self.board.seq) - 1:

               self.board.switch()

           else:

               self.board.rotate()

           print(self.board)

           steps += 1

       print('Total steps:', steps)

Explanation:

The Python class MarblesBoard creates an object of the board game used in the Solver class object instance and it holds data or attributes of the player piece movement and the magic methods (__str__ and __repr__). The Solver object gets the switch and rotate movementt of the player and displays the total steps at the end of the game.

You might be interested in
You have just finished entering field names for your database . You need to choose the primary key or key field but none of your
Luden [163]

C. Let the database assign a unique number in a new field

3 0
3 years ago
Read 2 more answers
Why are rules required for a number system to be useful?
evablogger [386]

Answer

This is because without them no one would know how much each symbol represents, and no one would be able to decipher the message.

Explanation

Number system is a way to represent numbers. It  is a writing system for expressing numbers; that is, a mathematical notation for representing numbers of a given set, using digits or other symbols in a consistent manner.

In computing or in a computer number systems are the techniques which represents numbers in the computer system architecture where   every value that you are saving or getting into/from computer memory has a defined number system.Computer architecture supports  . Binary number system,Octal number system and Decimal number system.

5 0
3 years ago
Read 2 more answers
can I do all my work in a notebook with Penn foster or do I have to type all of the answers in that little box?
dusya [7]
Um whichever is better for you! ummm whats the point of this question?
5 0
3 years ago
Read 2 more answers
A new computer virus can enter the system through email or through the internet. There is a 30 % chance of receiving this virus
SpyIntel [72]

Answer:

form 0% to 50%

Explanation:

6 0
3 years ago
How is the phrase "employability skills" defined?
Digiron [165]

Answer:

Problem-solving. Why is problem-solving so valued? Companies face a lot of obstacles. Those better able to cope

Explanation:

7 0
3 years ago
Read 2 more answers
Other questions:
  • How many fonts are there in a theme?<br> A. 1<br> B. 2<br> C. 4<br> D. 5
    10·1 answer
  • A wireframe is a sketch detailing where text and images will be placed on a website. T/F
    11·1 answer
  • What is the purpose of system calls, and how do system calls relate to the OS and to the concept of dual-mode (kernel-mode and u
    14·1 answer
  • - If we place records from different tables in adjacent____________, it would increase efficiency of a database.
    14·1 answer
  • What is better for the sd card use as portable storage or use as internal storage?
    15·1 answer
  • A group of computers that are interconnected in order to share information or documents is called a _____.
    7·1 answer
  • Riodic Table
    6·1 answer
  • _____ provide a description of the data characteristics and the set of relationships that link the data found within the databas
    8·1 answer
  • How does the dns solve the problem of translating domain names like example.com into ip addresses?
    10·1 answer
  • Cultural differences may make it difficult for team members to _____. Select 4 options.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!