Search for question
Question

Already have the main function and use the main function and give me the python code without main function. Write solutions to the following problems involving string and string functions, loops, dictionaries, files, exception handling, and user-defined functions using the Python programming language on the Raspberry Pi using the NGINX web server. For these questions, you will ONLY be writing the required functions (prototyped exactly below). The main( ) program to test your solutions will be provided for you (also below). *** NOTE: Your program MUST run as a client/server application using the NGINX web server and your solution MUST be presented in class to your instructor 1 week from today as well as submitted online using this form. Additional details will be presented in class *** Solutions that are not presented in class using the student's unique Raspberry Pi 3B+/4B/400/5 will NOT earn any marks (even if receiving a 10/10 when submitted using this form) ! It is recommended that you copy the main program (see below) into a file that includes your my.seneca id, your course code and section, the current semester, and correct file extension (specified EXACTLY) as listed below. In this file, you will include your solution(s) for the required code and thoroughly test your solution before submission using this form. REQUIRED FILENAME: The Filename for this evaluation MUST BE in jahmed47_PRG550C.242.L3.py NOTE: If the filename used is not specified EXACTLY as stated above, this program will not accept your solution. SUBMISSIONS MUST NOT INCLUDE main( ) When submitting your code, you are NOT to include the main() function and you may NOT add ANY additional Python modules using the import directive other than the ones already provided with the main( ) below. You will only be submitting the code containing YOUR solutions. To check the version of Python installed on your system: python -V This semester (SUMMER 2024) we will be using Python version 3.11.9 SPECIFICATIONS: You must use the following html FORM (link: lab3form.html) Lab Exercises: Question #1: Write the code for a python user-defined function named 'analyze_text' that processes the data from the dropdown list in the form (above) as input from the client. The function (on the sever) captures the variable (a string) named 'file name' that contains the name of a file on disk. The function uses exception handling and FILE I/O to read the entire contents of the file into memory and then stores every UNIQUE word in the file into a dictionary with the word as its key in lowercase only. Words are separated from other words using the space character. NOTE: The word may only consist of alphabetic characters ONLY. Any extra characters (punctuation or special characters) must be removed before the word is stored in the dictionary. For example: found in file: "Tapestry" experience.. Cont'd You're converted and stored in dictionary as key: tapestry experience contd youre The value for each key will consist of a list (array) of 26 integers where each integer represents the frequency of each letter in the word. For example: key: tapestry value: [1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1,2,0,0,0,0,1,0] experience [0,0,1,0,4,0,0,0,1,0,0,0,0,1,0,1,0,1,0,0,0,0,0,1,0,0] The function must the iterate through the dictionary and display the name of the file on the first line, followed by a list of every word in the dictionary with the frequency of each letter in the word displayed immediately after each word in alphabetical order. The letter is displayed followed by the : followed by the frequency followed by a comma and finally a new line. For example, a file of quotel.txt containing the text: Lottery: A tax on people who are bad at math. Would display: quotel.txt lottery e:1,1:1,0:1,r:1,t:2,y:1 a a:1 tax a:1,t:1,x:1 on n:1,0:1 people e:2,1:1,0:1,p:2 who: h:1,0:1,w:1 are: a:1,e:1,r:1 bad a:1,b:1,d:1 at a:1,t:1 math a:1, h:1,m:1,t:1 The text files can be downloaded from the links below: poem.txt tapestry.txt shakespeare.txt OR... From the Raspberry Pi command line, the files can be downloaded by issuing the following commands: curl -0 https://matrix.senecapolytechnic.ca/~danny.abesdris/prg550.242/labs/1ab2alt/p oem.txt curl -O https://matrix.senecapolytechnic.ca/~danny.abesdris/prg550.242/labs/lab2alt/t apestry.txt curl -O https://matrix.senecapolytechnic.ca/~danny.abesdris/prg550.242/labs/lab2alt/s hakespeare.txt The function prototype MUST be: def analyze_text(file_name) Question %232: Write the code for a python user-defined function named 'rabbit numbers' that processes the data from the 2 radio buttons in the form (above) as input from the client. The function (on the sever) captures the variables (as strings and converts them to integers) named 'fn' and 'sn'. The function then displays a list of 30 numbers where the first 2 values are fn and sn, and the subsequent 28 values are computed by adding the sum of the 2 previous numbers. Each number is separated by a space. After the 30th value is displayed, the function displays the result of the division of the last number divided by the 2nd last number. For example, if fn=3 and sn=2, the function would display: 325 7 12 19 31 50 81 131 212 343 555 898 1453 2351 3804 6155 9959 16114 26073 42187 68260 110447 178707 289154 467861 757015 1224876 1981891 1.618033988746616 The function prototype MUST be: def rabbit_numbers (fn, sn) MAIN PROGRAM: Your solution may ONLY use the python modules listed below #23 program: L3main.py %23 author: danny abesdris # date: may 27, 2024 #23 purpose: python main( ) program for PRG550 SUMMER 2024 Lab %233 import string import cgi import cgitb %23 first several lines of the python server program MUST be as listed below: #!/home/pi/software/bin/python3 #23 "shebang" line above MUST be the first line of the script and # MUST include the absolute path to the Python interpretor #23 import modules for CGI handling import cgi, cgitb # create an instance of the FieldStorage class form = cgi. FieldStorage ( ) get data from fields getvalue( ) retrieves a single value file name fn sn = = form.getvalue('file_name') form.getvalue('fn') = form.getvalue('sn') print ("Content-type: text/html\n\n") # TBA def analyze_text(file_name) your code here... # end def def rabbit numbers (fn, sn) : your code here... # end def : def main() : f names = ' [ "poem.txt", "tapestry.txt", "shakespeare.txt" ] for name in f names analyze text (name) print ("= ===== rabbit numbers (0,1) print ( rabbit numbers (3,2) print rabbit numbers (2,6) #23 end main( ) : if " name main : main ( ) The OUTPUT should be EXACTLY as displayed below: poem.txt once c:1,e:1,n:1,0:1 there e:2,h:1,r:1,t:1 lived d:1,e:1,i:1,1:1,v:1 a a:1 village a:1,e:1,g:1,1:1,1:2,v:1 of f:1,0:1 creatures a:1,c:1,e:2,r:2,s:1,t:1,u:1 along a:1,g:1,1:1,n:1,0:1 the e:1,h:1,t:1 bottom b:1,m:1,0:2,t:2 great a:1,e:1,g:1,r:1,t:1 crystal a:1,c:1,1:1,r:1,s:1,t:1,y:1 river e:1,i:1,r:2,v:1 each a:1,c:1,e:1,h:1 creature a:1,c:1,e:2,r:2,t:1,u:1 in i:1,n:1 its i:1,s:1,t:1 own n:1,0:1,w:1 manner a: 1,e:1,m:1,n:2,r:1 clung c:1,g:1,1:1,n:1,u:1 tightly g:1,h:1,1:1,1:1,t:2,y:1 to o:1,t:1 twigs g:1,i:1,s:1,t:1,w:1 and a:1,d:1,n:1 rocks c:1,k:1,0:1,r:1,s:1 for f:1,0:1,r:1 clinging c:1,g:2,i:2,1:1,n:2 was a:1,s:1,w:1 their e:1,h:1,i:1,r:1,t:1 way a:1,w:1,y:1 life e:1,f:1,i:1,1:1 resisting e:1,g:1,i:2,n:1,r:1,s:2,t:1 current c:1,e:1,n:1,r:2,t:1,u:1 what a:1,h:1,t:1,w:1 had a:1,d:1,h:1 learned a:1,d:1,e:2,1:1,n:1,r:1 from f:1,m:1,0:1,r:1 birth b:1,h:1,i:1,r:1,t:1