QUIZ 6
COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_6.py Enter two integers: 0 1 Here is the grid that has been generated:
0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000 0000000000
No chess knight has explored this board. $ python3 quiz_6.py Enter two integers: 0 -20 Here is the grid that has been generated:
0000000000 0000000000 0000000000 0000000000 0100100000 0000000000 0000000000 0000000000 0000000000 0000000000
At least 2 chess knights have explored this board. $ python3 quiz_6.py Enter two integers: 0 -6 Here is the grid that has been generated:
0010000000 0000001000 0000101000 0100000000 0001011000 0010000000 1000000011 0001000010 0000000010 0000000010
At least 7 chess knights have explored this board.
Date: Session 2, 2018.
2 COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_6.py Enter two integers: 0 -5 Here is the grid that has been generated:
0010000000 0000001000 0001100010 0000000001 0110100000 1000000011 0001000100 0000000100 0000000100 0110011000
At least 8 chess knights have explored this board. $ python3 quiz_6.py Enter two integers: 0 -4 Here is the grid that has been generated:
0010000000 0001000110 0100000001 1101000010 0000110010 0100001000 0000100110 0110000000 0010100001 0100000110
At least 6 chess knights have explored this board. $ python3 quiz_6.py Enter two integers: 0 3 Here is the grid that has been generated:
1101111111 1010100111 1101010111 1011111011 1110100111 1101110111 0010001100 1110111101 1101111101 1110100001
At least 3 chess knights have explored this board.
# Randomly fills an array of size 10×10 True and False, displayed as 1 and 0,
# and outputs the number chess knights needed to jump from 1s to 1s
# and visit all 1s (they can jump back to locations previously visited).
#
# Written by
from random import seed, randrange
import sys
dim = 10
def display_grid():
for i in range(dim):
print(‘ ‘, end = ”)
print(‘ ‘.join(grid[i][j] and ‘1’ or ‘0’ for j in range(dim)))
print()
def explore_board():
pass
# Replace pass above with your code
try:
for_seed, n = (int(i) for i in input(‘Enter two integers: ‘).split())
if not n:
raise ValueError
except ValueError:
print(‘Incorrect input, giving up.’)
sys.exit()
seed(for_seed)
if n > 0:
grid = [[randrange(n) > 0 for _ in range(dim)] for _ in range(dim)]
else:
grid = [[randrange(-n) == 0 for _ in range(dim)] for _ in range(dim)]
print(‘Here is the grid that has been generated:’)
display_grid()
nb_of_knights = explore_board()
if not nb_of_knights:
print(‘No chess knight has explored this board.’)
elif nb_of_knights == 1:
print(f’At least 1 chess knight has explored this board.’)
else:
print(f’At least {nb_of_knights} chess knights have explored this board’)