|
|

楼主 |
发表于 2017-9-5 17:59:33
|
显示全部楼层
- # Randomly fills a grid of size height and width whose values are input by the user,
- # with nonnegative integers randomly generated up to an upper bound N also input the user,
- # and computes, for each n <= N, the number of paths consisting of all integers from 1 up to n
- # that cannot be extended to n+1.
- # Outputs the number of such paths, when at least one exists.
- from random import seed, randint
- import sys
- from collections import defaultdict
- def display_grid():
- for i in range(len(grid)):
- print(' ', ' '.join(str(grid[i][j]) for j in range(len(grid[0]))))
- def get_paths():
- pass
- # Replace pass above with your code
- # Insert your code for other functions
-
- try:
- for_seed, max_length, height, width = [int(i) for i in
- input('Enter four nonnegative integers: ').split()
- ]
- if for_seed < 0 or max_length < 0 or height < 0 or width < 0:
- raise ValueError
- except ValueError:
- print('Incorrect input, giving up.')
- sys.exit()
- seed(for_seed)
- grid = [[randint(0, max_length) for _ in range(width)] for _ in range(height)]
- print('Here is the grid that has been generated:')
- display_grid()
- paths = get_paths()
- if paths:
- for length in sorted(paths):
- print(f'The number of paths from 1 to {length} is: {paths[length]}')
复制代码 |
|