Query= sectiona
>Fruits
Yes = (50%) No = (25%) Maybe = (25%)
Query= sectionb
>Vegetables
Yes = (25%) No = (50%) Maybe = (25%)
Query= sectionc
>Dairy
Yes = (12%) No = (18%) Maybe = (70%)
Query= sectiond
>Meat
Yes = (60%) No = (20%) Maybe = (20%)
The above is in a separate file named 'Example.txt' that has to be opened. Using Python, Write a script that opens the file and creates an output in the format given below so that only the query with the highest percent yes is given without just extracting strings, solve for it.
Query Food Percentyes
sectiond meat 60%
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Make sure that you maintain proper indentations while pasting the below code into your program, I’m also attaching the code in both text and image formats, so in case if the indentation is messed up, you can refer the image and maintain proper spacing in each and every line. Thanks
If you are satisfied with the solution, please rate the answer.
#code
import re
#method to open the queries file and display the
results
def openFile(filename):
#opening file
f=open(filename)
#loading lines into a list, after removing
newline characters
lines=[x.strip() for x
in f]
#variable to store the highest percentage
for yes
highestPercentYes=-1
#list variable to store the query, result
and yes percent for the highest percentage yes
queryHighest=[]
i=0
#looping until all lines are read, assuming
input file is valie
#and does not contain blank lines
while
i<len(lines):
#getting line
line=lines[i]
#removing 'Query='
and saving the query in a variable
query=re.sub('Query=','',line).strip()
#moving to next
line
i+=1
line=lines[i]
#extracting the
result after removing '>' symbol
result=re.sub('>','',line).strip()
#moving to next
line
i+=1
line = lines[i]
#finding the
percentage for yes (it'll be in the first pair of
parantheses)
#and converting into
integer
yes=int(line[line.find("(")+1:line.find(")")][:-1])
#checking if yes is
greater than current highestPercentage
if
yes>highestPercentYes:
#making this as the new highest percentage yes
highestPercentYes=yes
#storing the details on a list
queryHighest=[query,result,yes]
i+=1 #next
line
#displaying the query with highest percent yes
in a tab seperated format
print('Query\tResult\tPercentyes')
print('{:s}\t{:s}\t{:d}%'.format(queryHighest[0],queryHighest[1],queryHighest[2]))
#opening and displaying queries from a file named
'Example.txt'
openFile('Example.txt')
#code screenshot
![1 import re 2 #method to open the queries file and display the results 3 def openFile(filename): 4 #opening file open(filename) #loading lines into a list, after removing newline characters lines-[x.strip() for x in f] #variable to store the highest percentage for yes highestPercentYes--1 #list variable to store the query, result and yes percent for the highest percentage yes queryHighest 6 8 10 12 13 14 15 16 17 18 19 20 21 #looping until all lines are read, assuming input file is vale #and does not contain blank lines while i<len(lines): #getting line line-lines[i] #removing Query and saving the query in a variable query-re.sub( Query-,,line).strip() #moving to next line line-lines[i] #extracting the result after removing result-re.sub(>, ,line).strip() #moving to next line symbol 23 24 25 26 27 28 29 30 31 32 line -lines[i] #finding the percentage for yes (itll be in the first pair of parantheses) #and converting into integer yes-int (line[line.find()+1:line.find())][:-1]) #checking if yes is greater than current highestPercentage if yes>highestPercentYes: #making this as the new highest percentage yes highestPercentYes-yes #storing the details on a list queryHighest-[query,result,yes] 34 35 36 37 38 39 40 1+-1 #next line #displaying the query with highest percent yes in a tab seperated format print( Query tResultltPercentyes) print( { : s t{ : s t( :d}% .format (queryHighest [0] , queryHighest [1].queryHighest [2] )) 42 #opening and displaying queries from a file named Example.txt 43 openFile(Example.txt](http://img.homeworklib.com/questions/fb660ef0-26a0-11eb-9344-4b580a9724db.png?x-oss-process=image/resize,w_560)
#output
Query Result Percentyes
sectiond Meat 60%
Query= sectiona >Fruits Yes = (50%) No = (25%) Maybe = (25%) Query= sectionb >Vegetables Yes...
Hallo! I have a problem in my code, I need help please in java code. My task is : Write a program that can read a text from a file and do frequency analysis on the letters that occur . To store information (frequency) you must use an array. Tip! The place where you collect the frequency of eg the letter A is the equivalent of the ASCII code of "A" ie 65. The array should be 127 elements. For...
These are my answere to the following questions: are they right? 1. B 2. T 3. T 4. T 5. F 6. T 7. A 8. D 9. E 10. B 11. B 12. A 13. A 14. D 15. C 16. D 17. T 18. C 19. T 20. T 21. T 22. A 23. T 24. D 25. B 26. A 27. A 28. A 29. T 30. C 31. D 32. A 33. T 34. F 35....
CS 1102 Unit 5 – Programming Assignment In this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion". This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the...
10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated sludge operation that adversely affect effluent quality with origins in the engineering, hydraulic and microbiological components of the process. The real "heart" of the activated sludge system is the development and maintenance of a mixed microbial culture (activated sludge) that treats wastewater and which can be managed. One definition of a wastewater treatment plant operator is a "bug farmer", one who controls the aeration...