QUIZ 6
COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_6.pyEnter two integers: 0 0
Here is the grid that has been generated:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
The largest isosceles triangle has a size of 0
$ python3 quiz_6.pyEnter two integers: 0 1
Here is the grid that has been generated:
1 1 0 1 1 1 1 1 1 0
0 1 0 0 1 0 1 0 0 1
1 0 1 1 1 0 1 1 1 0
0 0 1 0 1 1 0 1 0 0
0 0 0 1 0 0 1 1 0 1
1 0 1 0 1 1 0 1 1 0
1 0 0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 1 1 1
1 1 0 1 0 1 1 0 0 0
1 0 0 1 0 1 1 0 0 0
The largest isosceles triangle has a size of 2
$ python3 quiz_6.pyEnter two integers: 0 2
Here is the grid that has been generated:
1 1 0 1 1 1 1 1 1 1
1 0 1 0 1 0 0 1 1 1
1 1 0 1 0 1 0 1 1 1
1 0 1 1 1 1 1 0 1 1
1 1 1 0 1 0 0 1 1 1
1 1 0 1 1 1 0 1 1 1
0 0 1 0 0 0 1 1 0 0
1 1 1 0 1 1 1 1 0 1
1 1 0 1 1 1 1 1 0 1
1 1 1 0 1 0 0 0 0 1
The largest isosceles triangle has a size of 3
Date: Trimester 1, 2019.
2 COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_6.pyEnter two integers: 0 3
Here is the grid that has been generated:
1 1 0 1 1 1 1 1 1 1
1 1 1 0 1 1 1 0 0 1
1 0 1 1 1 1 1 1 1 0
0 0 1 0 1 1 1 1 0 1
1 1 1 1 0 0 1 1 0 1
1 0 1 1 1 1 0 1 1 1
1 1 1 1 0 1 1 0 0 1
1 0 0 1 1 1 1 1 1 1
1 1 0 1 0 1 1 1 1 0
1 0 1 1 1 1 1 0 0 1
The largest isosceles triangle has a size of 3
$ python3 quiz_6.pyEnter two integers: 0 4
Here is the grid that has been generated:
1 1 0 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1 1
1 1 1 0 0 1 1 1 0 1
1 1 1 1 1 1 1 1 1 0
1 0 0 1 0 1 1 1 1 1
0 1 1 1 1 1 1 1 0 0
1 1 1 0 1 1 1 0 1 1
1 1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 1 0 1 1
1 0 0 1 1 0 0 1 1 1
The largest isosceles triangle has a size of 4
$ python3 quiz_6.pyEnter two integers: 0 5
Here is the grid that has been generated:
1 1 0 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1 1
1 1 1 1 0 1 0 1 1 1
1 0 1 1 1 1 1 1 1 1
1 1 1 0 1 0 0 1 1 1
1 1 0 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 0 0
1 1 1 0 1 1 1 1 0 1
1 1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 0 1
The largest isosceles triangle has a size of 3
QUIZ 6 3
$ python3 quiz_6.pyEnter two integers: 0 6
Here is the grid that has been generated:
1 1 1 1 0 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 1
1 0 1 0 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 0 0 1 1 1
1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 0 0 1 1
1 0 1 1 1 1 0 1 1 1
The largest isosceles triangle has a size of 4
$ python3 quiz_6.pyEnter two integers: 0 100
Here is the grid that has been generated:
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
The largest isosceles triangle has a size of 5
#我的解法
# Randomly generates a grid with 0s and 1s, whose dimension is controlled by user input,
# as well as the density of 1s in the grid, and determines the size of the largest
# isosceles triangle, consisting of nothing but 1s and whose base can be either
# vertical or horizontal, pointing either left or right or up or down.
#
# Written by *** and Eric Martin for COMP9021
from random import seed, randint
import sys
def display_grid():
for i in range(len(grid)):
print(' ', ' '.join(str(int(grid[i][j] != 0)) for j in range(len(grid))))
def up(i,j):
# for i in range(10):
# print(*grid[i])
# print()
flag=True
count=0
for x in range(i,len(grid)):#x-1
if j-x+i>=0 and j+x-i+1<=len(grid[0]):
for y in range(j-x+i,j+x-i+1):
#print(x,y)
if grid[x][y]==0:
flag=False
break
else:
flag=False
if flag==True:
count+=1
else:
break
return count
def down(i,j):
flag=True
count=0
for x in range(i,-1,-1):#i-x
if j-i+x>=0 and j+i-x+1<=len(grid[0]):
for y in range(j-i+x,j+i-x+1):
if grid[x][y]==0:
flag=False
break
else:
flag=False
if flag==True:
count+=1
else:
break
return count
def left(i,j):
flag=True
count=0
for y in range(j,len(grid[0])):#y-j
if i-y+j>=0 and (i+y-j+1)<=len(grid):
for x in range(i-y+j,i+y-j+1):
#print(i,j,'ij')
if grid[x][y]==0:
flag=False
break
else:
flag=False
if flag==True:
count+=1
else:
break
return count
def right(i,j):
flag=True
count=0
for y in range(j,-1,-1):#j-y
if i-j+y>=0 and i+j-y+1<=len(grid):
for x in range(i-j+y,i+j-y+1):
if grid[x][y]==0:
flag=False
break
else:
flag=False
if flag==True:
count+=1
else:
break
return count
def size_of_largest_isosceles_triangle():
count=0
for i in range(0,len(grid)):
for j in range(0,len(grid[i])):
#print(grid)
if grid[i][j]!=0:
c=max(up(i,j),down(i,j),left(i,j),right(i,j))
if c>count:
#print(i,j,c)
#print(up(i,j),down(i,j),left(i,j),right(i,j))
count=c
return count
try:
arg_for_seed, density = (abs(int(x)) for x in input('Enter two integers: ').split())
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
seed(arg_for_seed)
grid = [[randint(0, density) for _ in range(10)] for _ in range(10)]
print('Here is the grid that has been generated:')
display_grid()
print('The largest isosceles triangle has a size of',
size_of_largest_isosceles_triangle()
)
网友评论