Question

Your task is to wite a function parse scenario(filenne) that parses a file with the structure described below, validates the

program.py bf.data bf0.dat 2 1,2,1 3 0,1,2 4 0,0,1 5 1,1,1 6 2,1,2 7 3,2,2 10 1,1

the second picture is the content of the file bf.dat

the language is python

Your task is to wite a function parse scenario(filenne) that parses a file with the structure described below, validates the contents and retums either a dicdionary containine all values required to specify a model scenarlo if the contents are valid, or None if any of the mntents are inwalid. The structure of the fle is as follows: . an inteper specifying the wicth and heih of the square landscape erid * M lines nacn containing M integrVAIes separated by con mas drf ning the initial url load at the landscapr a visual representation af the M hy ง grid lines, each containine M integer values separated by commas, defining the height of the lancscape aain, a visual representation of the M by M grid a for this scnarin n intrger specifying the igniticn threshald . a one or two character strine specilying the wind direction for Lhis scenario * an·ar mne lines, cach cnrtaining the . cnarn nates row number, cDhimn rnimber afa 쏘ll which is burning at thr start af the simulation For example, the file 'bf8.dat 2,2 1,2 speciñes a 2 by 2 landscape: · there is an initial tuel load of 2 in three of the four cells, with a ruel load o10 n hecell clo * thr height of all cells in the fist column J。is 1 w hile the height of all cells in the sernnd cnlumn thr ignition threshold is 1 . a wind is bloing from the Ncrh 1 is 2 in the landscape singes up trward the Fast- the top letcls buming at the start of the simulatioin You may assume that the input files are well-formed, but your function should ensure that: he di Lhe grid are a pesitive inleger; . the inition threshold is a positive integer not gree than eiht: the wind direci is vlid; and . the coordinates of the burning cells are (a located on the landscape, andb have non- zero initial fuel load. If all values are validd. your function should return a dictionary with keyvalue pairs as follows: .f grid:a list of lists fdiesiois M by i) . h grid: a list of lists lof dimensions byi] integer i-threshold: an Mdirectiorc a strine burn eeds: a list af tuples If any values are invalid. your function should return Hone For example: parsa scenario'bfe-dat' 'f grid' [2, 2], [, 2]1, hgrid11, 21, I1, 211, 'i-threshold' ; 1, direction'! 'N' 'burn-seeds'; [(6, 91) parsscario'b.dat ('f-grid': [[1: 1], [g, 2]: te, 1]], 2, 1, o, ithreshald': 1, M.direction' 'N burneds 1, 1
program.py bf.data bf0.dat 2 1,2,1 3 0,1,2 4 0,0,1 5 1,1,1 6 2,1,2 7 3,2,2 10 1,1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

1 def parse_scenario(filename): 2 3 try: r) open(filename , fp = nint (fp.readline().strip)) d[f grid] [ for i in range(n

Code:

def parse_scenario(filename):

try:

fp = open(filename, "r")

d = {}

n = int(fp.readline().strip())

d['f_grid'] = []

for i in range(n):

d['f_grid'].append(list(map(int, fp.readline().strip().split(","))))

d['h_grid'] = []

for i in range(n):

d['h_grid'].append(list(map(int, fp.readline().strip().split(","))))

d['i_threshold'] = int(fp.readline().strip())

d['w_direction'] = fp.readline().strip()

d['burn_seeds'] = []

for i in range(d['i_threshold']):

d['burn_seeds'].append(tuple(map(int, fp.readline().strip().split(","))))

return d

except:

return None

print(parse_scenario("bf0.dat"))

print(parse_scenario("bf.dat"))

Add a comment
Know the answer?
Add Answer to:
the second picture is the content of the file bf.dat the language is python Your task is to wite a function parse scenario(filenne) that parses a file with the structure described below, validates t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT