In a particular board game, there is exactly one row and it comprises N spaces, numbered 0 through N - 1 from left to right. There 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.
The code I wrote works for the example posted on the problem (1 3 0 2), but when I add the extra number 4 randomly to the list the while loop never terminates. Looking at the sorted sequence, it seems to loop through a number of the same sequence repeatedly. I am not sure why it would work for one series but not the next one.
Bonus, I can't seem to figure out how to print the output as numbers separated by a space vs. as a list with brackets and commas. The problem asks that we print the output as numbers separated by spaces. Any help on that would be appreciated.
class MarblesBoard: """creates a marble board with number marbles in specific spots""" def __init__(self, marble_sequence): self.board = [x for x in marble_sequence] def __str__(self): return str(self.board) def __repr__(self): return "%r " % (self.board) def switch(self): """switch the marbles in position 0 and 1""" self.board[0], self.board[1] = self.board[1], self.board[0] return self.board def rotate(self): """Move the marble in position 0 to position N - 1, and move all other marbles one space to the left (one index lower)""" copy_board = self.board.copy() copy_board[len(self.board)-1] = self.board[0] for x in range(1, len(self.board)): copy_board[x - 1] = self.board[x] self.board = copy_board return self.board def is_sorted(self): return self.board == sorted(self.board): class Solver: """solves the marble sorting game when given a marble board as input""" def __init__(self, MarblesBoard): self.steps = 0 self.board = MarblesBoard.board return def solve(self): n = len(self.board) # print("n = ", n) print(self.board) while MarblesBoard.is_sorted(self) == False: if self.board[0] > self.board[1]: MarblesBoard.rotate(self) print(self.board) self.steps += 1 else: MarblesBoard.switch(self) print(self.board) self.steps += 1 print("total steps: ", self.steps) With regards to output, the code works fine for the example output here:
board2 = MarblesBoard((1,3,0,2)) solver = Solver(board2) solver.solve() [1, 3, 0, 2] [3, 1, 0, 2] [1, 0, 2, 3] [0, 2, 3, 1] [2, 0, 3, 1] [0, 3, 1, 2] [3, 0, 1, 2] [0, 1, 2, 3] total steps: 7 However, if I add a 4 into the starting board:
board2 = MarblesBoard((1,3,0,2,4)) solver = Solver(board2) solver.solve() [1, 3, 0, 2, 4] [3, 1, 0, 2, 4] [1, 0, 2, 4, 3] [0, 2, 4, 3, 1] [2, 0, 4, 3, 1] [0, 4, 3, 1, 2] [4, 0, 3, 1, 2] [0, 3, 1, 2, 4] [3, 0, 1, 2, 4] [0, 1, 2, 4, 3] [1, 0, 2, 4, 3] [0, 2, 4, 3, 1] [2, 0, 4, 3, 1] [0, 4, 3, 1, 2] [4, 0, 3, 1, 2] [0, 3, 1, 2, 4] [3, 0, 1, 2, 4] Note that 3 0 1 2 4 repeats as the second iteration and the last listed iteration. Due to the structure of the while loop, since the same sequence occurs, the same steps get implemented and the loop continues infinitely.
is_sortedmethod can simply bereturn self.board == sorted(self.board). Glad you didn't returnFalsewhen the condition isTrue.