Answer:
polygon_sides = {"Triangle" : 3,"Square" : 5}
n_polygons = {}
for key in polygon_sides:
n_polygons[polygon_sides[key]] = key
print("Polygon sides: ", polygon_sides)
print("Names of polygons: ", n_polygons)
Explanation:
- Initialize the polygon_sides dictionary.
- Loop through polygon_sides dictionary and assign the associated names to n_polygons variable.
- Display the polygon sides and the names of polygons.
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.
Answer:
member wise Initialization.
Explanation:
Member wise initialization is used for the following cases:-
- When you have constant member in your class.
- When you have Reference member in your class.
- When you have a very large member in your class.
- When you have a member with no default constructor.
Member wise initialization is uses initialization and the direct initialization uses assignment.
Answer:
D - 14
Explanation:
Each time the program executes an iteration of the for-loop, it works out the current element by adding 3 to the previous element.
alpha[0] = 2
+3
alpha[1] = 5
+3
alpha[2] = 8
+3
alpha[3] = 11
+3
alpha[4] = 14