Develope a python program to analyze the data
Assignment Specifications The lines of interest in the file GDP. txt are the 9h line which has the annual change in GDP and the 44th line which has the value of GDP for each year. The data starts in column 76 and each of the 47 data items spans 12 columns (there are 47 years inclusively from 1969 through 2015). These numbers are important because you can use string slicing to extract them. For example, the first data item in a line can be extracted using the slice line [76:76+12] Your task is to find the minimum and maximum change in GDP for the years 1969 through 2015 and to find the GDP value for those two years. See the sample output below. Your program will prompt for an input file and then display the output. You must use specific functions as described below and you are not allowed to use collections such as lists tuples and dictionaries. Mirmir tests: I can provide any file with the same format as the provided GDP.txt-the number of rows will be the same (so you can select lines 9 and 44), but the number of columns may be different (i.e. different years) Assignment Notes Divide-and-conquer is an important problem solving technique. In fact, inside of every challenging problem is a simpler problem trying to get out. Your challenge is to find those simpler problems and divide the problem into smaller pieces that you can conquer. Functions can assist in this approach. We provide skeleton code on Mirmir that has all the function skeletons for you to fill in Hint: build the following functions one at a time and test each one before moving on to the next. 1. The open file function takes no arguments and returns a file pointer. It repeatedly prompts for file names until a file successfully opens. Use the try-except construct checking for the FileNotFoundError exception. You do not need to check that the filename is correct-simply check that the file opened. The simplest version of this function has no error checking so the body of the function is simply fp open (“GDP txt”) return fp Hint: Start with that as your function body and add the try-except error checking later
Expert Answer
Hi there,
I implemented functions 1 – 4. The code below MAIN comment was actually the code I used for testing.
EDIT 1:
def open_file():
ENOENT = True
while ENOENT:
try:
filename = raw_input(“Enter a file name: “)
fp = open(“GDP.txt”) if filename == “” else open(filename)
return fp
except IOError:
print “Error. please try again”
def find_min_percent(line):
start, end, indx = 76, 76+12, 0
# Presets
min_val = 1000000000000000
min_indx = -1
for i in range(0, 48):
# print line[start:end]
try:
val = float(line[start:end].strip())
except ValueError:
continue
if (min_val > val):
min_val = val
min_indx = indx
else:
pass
indx += 1
start += 12
end += 12
return min_val, min_indx
def find_max_percent(line):
start, end = 76, 76+12
# Presets
max_val = 0
max_indx = -1
for i in range(0, 47):
# print line[start:end].strip()
try:
val = float(line[start:end].strip())
except ValueError:
continue
if (max_val < val):
max_val = val
max_indx = i
else:
pass
start += 12
end += 12
return max_val, max_indx
def find_gdp(line, index):
start, end = 76, 76+12
for i in range(0, 49):
if (i == index):
return float(line[start:end].strip())
else:
pass
start += 12
end += 12
# MAIN
# ====
my_file = open_file()
line_no = 0
max_percent = max_index = max_gdp = 0
min_percent = min_indx = min_gdp = 0
for line in my_file.readlines():
if line_no == 8:
max_percent, max_index = find_max_percent(line)
min_percent, min_index = find_min_percent(line)
elif line_no == 43:
min_gdp = find_gdp(line, min_index)
max_gdp = find_gdp(line, max_index)
else:
pass
line_no += 1
print “Max gdp change occured at GDP: %f” % max_gdp
print “Percentage is: %f, occured at: %d” % (max_percent, max_index+1969)
print “Min gdp change occured at GDP: %f” % min_gdp
print “Percentage is: %f, occured at: %d” % (min_percent, min_index+1969)