code the following in python: below is the .txt file 1. \"P2.6.3 Your program sh
ID: 3733413 • Letter: C
Question
code the following in python:
below is the .txt file
Explanation / Answer
'''
WRITTEN IN PYTHON 2.7
SORRY I DONT KNOW THE REFERENCE VALUES YOU HAVE MENTIONED THAT ARE IN TABLE 2.14
THATS WHY I HAVE USED REFERENCE VALUES MENTIONED IN INTERNET
AND I HAVE ONLY GOT 4 POPERTY'S REFERENCE AND WEIGHT EXPONENT
Planetary Properties used:
PROPERTY_NAME REFERENCE WEIGHT_i
1. RADIUS 1.0 0.57
2. DENSITY 1.0 1.07
3. ESCAPE VELOCITY 1.0 0.70
4. SURFACE_TEMP 288 5.58
NO.OF PROPERTIES (n) = 4
EVERYTHING WILL BE DONE LIKE YOU EXPLAINED
'''
import math
def calculate_esi(val,w_i,ref,n):
sub = (val-ref)/(val+ref) #IMPLEMENTING THE FORMULA GIVEN
sub = abs(sub)
sub = 1 - sub
sub = pow(sub,w_i/n)
return sub
f=open("file.txt","r") #open the file
x=f.readline() #first line contains attributes
x=f.readline() #second line contains attributes type
x=f.readline() ##third line ------------------- and we are skipping these three lines
x=f.readline() #EARTH TAKES AS REFERENCE
x=f.readline() #START OF OTHER PLANET OR OBJECT CALCULATION
w_i = [0.57 , 1.07 , 0.70 , 5.58] #weight components for properties
ref_prop = [1.0 , 1.0 ,1.0 , 288.0] #reference values according to above
rad_list = [] #list to store each planet esi according to radius
dens_list =[] #list to store each planet esi according to DENSITY
esc_vel_list = [] #list to store each planet esi according to ESCAPE_VELOCITY
surf_temp_list = [] ##list to store each planet esi according to SURFACE_TEMPRATURE
planet_esi = [] #list to store each planet esi according to PRODUCT OF EACH PALENTORY PROPERTY
n=4.0 #no of properties
while(x):
x=x.strip() #REMOVING WHITE SPACES AT EACH ENDS
ind=x.find(' ') #FINDING OBJECT NAME
obj= x[:ind]
values=[]
ind= x.find(' ',ind+1) #FINDING VALUES
while(ind!=-1):
ind1=ind+1
ind=x.find(' ',ind1)
if(ind1==ind): #IF IND1==IND THEN IT IS NOT A REQUIRED VALUE(i.e VALUE IS DEFINED AS IT WILL BE BETWEEN SPACES
continue #WE ARE FINDING TWO SPACES IF THE LOCATION OF INDEXS IS SIDE BY SIDE THEN THERE WILL BE NO VALUE
values.append(x[ind1:ind]) #IF THERE IS A VALUE ADD IT TO VALUES LIST
if(len(values)>0): #IF VALUES ARE TRAVERSED CORRECTLY THEN CALCULATE ESI
radius = float(values[1]) #CHANGING STRING VALUE TO FLOAT.
density = float(values[2]) #ACCORDING TO THE FILE PATTERN YOU HAVE GIVEN I AM ONLY CONSIDERING THE 4 PROPERITES ABOVE MENTIONED
escape_velocity = float(values[4])
surf_temp = float(values[6])
rad_esi = calculate_esi(radius,w_i[0],ref_prop[0],n) #CALLING calculate_esi() FUNCTION for each property
den_esi = calculate_esi(density,w_i[1],ref_prop[1],n)
esc_vel_esi = calculate_esi(escape_velocity,w_i[2],ref_prop[2],n)
sur_temp_esi = calculate_esi(surf_temp,w_i[3],ref_prop[3],n)
rad_list.append(rad_esi) #APPENDING EACH PROPERTY ESI TO RESPECTED LIST
dens_list.append(den_esi)
esc_vel_list.append(esc_vel_esi)
surf_temp_list.append(sur_temp_esi)
total_esi = rad_esi*den_esi*esc_vel_esi*sur_temp_esi #product of each property's ESI
planet_esi.append(total_esi)
x=f.readline()
f.close()
print "======================Most Similarity According to Radius======================"
print max(rad_list)
print "======================Most Similarity According to Density======================"
print max(dens_list)
print "======================Most Similarity According to Escape Velocity=============="
print max(esc_vel_list)
print "======================Most Similarity According to Surface Tempratur============"
print max(surf_temp_list)
print "======================Most Similarity According to Planet total ESI============="
print max(planet_esi)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.