Home » Blog » Consider the following formula and evaluate the y value for the range of t values found in a file with format ๐‘ฆ(๐‘ก)=๐‘ฃ0๐‘กโˆ’0.5๐‘”๐‘ก2

Consider the following formula and evaluate the y value for the range of t values found in a file with format ๐‘ฆ(๐‘ก)=๐‘ฃ0๐‘กโˆ’0.5๐‘”๐‘ก2

Problem:

Consider the following formula and evaluate the y value for the range of t values found in a file with format
๐‘ฆ(๐‘ก)=๐‘ฃ0๐‘กโˆ’0.5๐‘”๐‘ก2
File Format:
v0 3.0
t:
0.15592 0.28075 0.36807889 0.35 0.57681501876 0.21342619 0.0519085 0.042 0.27 0.50620017 0.528 0.2094294 0.1117 0.53012 0.3729850 0.39325246 0.21385894 0.3464815 0.57982969 0.10262264 0.29584013 0.17383923
More precisely, the first two lines are always present, while the next lines contain an arbitrary number of t values on each line, separated by one or more spaces.
i. Write a function that reads the input file and returns v0 and a list with the t values.
ii. Write a function that creates a file with two nicely formatted columns containing the t values to the left and the corresponding y values to the right. Let the t values appear in increasing order (note that the input file does not necessarily have the t values sorted).
iii. Make a test function that generates an input file, calls the function for reading the file, and checks that the returned data objects are correct.
iv. Write a function which handle the exception handling for the availability of file or not.

y=[]
tndy=[]
error=[]
def tvalue(tfile):
    t=(tfile.read()).split(' ')
    v0=3.0
    tfile.close()
    return v0,t

def calculate(tdata,value,ansfile):
    tdata.sort()
    ansfile.write("{: <20} {: <20}\n".format("t value","y value"))
    for i in range(len(tdata)):
        yt=value*float(tdata[i])-(0.5*9.81*pow(float(tdata[i]),2))
        y.append(yt)
        #print(y)
    print("{: <20} {: <20}".format("t value","y value"))
    for i in range(len(tdata)):
        row=tdata[i]+" \t\t\t "+str(y[i])+"\n"
        print("{: <20} {: <20}".format(tdata[i],y[i]))
        ansfile.write(tdata[i]+"\t\t"+str(y[i])+"\n")
    ansfile.close()
        
def fileavail():
    try:
        a=open("yt.txt","w")
        t=open("tvalue.txt","r")
    except IOError:
        print("Cannot Open File")
    return a,t

def test():
    yt=open("yt.txt","r")
    data=(yt.read()).split('\n')
    for x in range(len(data)-1):
        if x==0:
            continue
        else:
            tndy.append(data[x].split("\t\t"))
    
    for i in range(len(tndy)):
        yt=3.0*float(tndy[i][0])-(0.5*9.81*pow(float(tndy[i][0]),2))
        if yt == tndy[i][1]:
            continue
            print(yt,tndy[i][1])
            error.append(tndy[i])
    if len(error)>0:
        print("Wrong Data Are")
        print(error)
    else:
        print("No Wrong Data")

ansfile,tfile=fileavail()
v,tvalue=tvalue(tfile)
calculate(tvalue,v,ansfile)
test()

OutPut:

t value              y value             
0.042                0.11734758          
0.0519085            0.14250901491411377 
0.10262264           0.25621137239000613 
0.1117               0.27390085454999996 
0.15592              0.348514317408      
0.17383923           0.37328820796429985 
0.2094294            0.4131515960687142  
0.21342619           0.4168521972753215  
0.21385894           0.41724347530108474 
0.27                 0.4524255           
0.28075              0.45563514093749996 
0.29584013           0.45822800874716507 
0.3464815            0.4506020466237638  
0.35                 0.44913749999999986 
0.36807889           0.43969712026188446 
0.3729850            0.43658214084637514 
0.39325246           0.42121140576286686 
0.50620017           0.26175011761011824 
0.528                0.21656447999999995 
0.53012              0.2119215133679997  
0.57681501876        0.09847520570187851 
0.57982969           0.0904159575660417  
No Wrong Data

 

Leave a Reply

Your email address will not be published.