Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1 In this question we will revisit the character counting example shown in class. In that example we read through a text file, counting

Question 1 In this question we will revisit the character counting example shown in class. In that example we read through a text file, counting how many times each character showed up and then we displayed some information about how many times each letter of the alphabet occurred in the file. Here you are going to modify this code to first prompt the user for which file we want to analyze. From here we are going to do things a little different. 1. We are interested in counting the instances of ALL characters, including upper and lowercase letters, numbers and punctuation (except for new lines). 2. We also want to count the occurence of each distinct word in the file. Again, we want to be case sensitive (so this and This are different). 3. Lastly we also want to count the occurence of each distinct line in the file Each of these sets of data should be stored in their own dictionary. You are also only allowed to iterate through the file once. Once you have processed the file, you will present to the screen the top occurring character, word and line from the file as well as the least occuring character, word and line. It is likely that there are more than one top/least occurring element, however you only need to report one.You will also create a results file that contains all of the data you have collected. First you will output the character occurrences one per line (char: count). You will then enter a blank line into the file and do the same thing with the word occurrences, followed by a blank line, followed by the line occurrences. The order that this data is written to file is not important. A sample input and output file is provided so you can see the exact format expected. The filename of this output file will be built from the input filename. You can assume the user will choose a file with a .txt extension. Your output file will replace the .txt in the input file name with -number-analysis.txt. For example, if the input file specified by the user was "test1.txt", then the output file will be named "test1-number-analysis.txt". For this problem, you are being provided with a partial solution you are to start from and modify. This is close to the example we did in class. This file is called question1_starting_point.py . You can find it on cuLearn with your assignment. Save your solution in a file called question1.py . Put this file in your submission zip file. Note: It is assumed that the file to be loaded will be in the same working directory (folder) as your python code. The new file that you create must also be in this same directory. You should not be specifying any paths for file names in this problem. Example output (user input highlighted ). The contents of caged_bird-number-analysis.txt is provided with this assignment.

File would you like to analyze: caged_bird.txt Highest occurring character, word and line:

: 155

the: 15

The caged bird sings: 2

Lowest occurring character, word and line:

z: 1

sky: 1

his shadow shouts on a nightmare scream: 1

Question 2 In this problem, you will implement a function that performs matrix-vector multiplication. If you are unfamiliar with this operation, or need a reminder, please see http://mathinsight.org/matrix_vector_multiplication In this question we are going to build off of the code you wrote in tutorial 3. In this tutorial you wrote code to load a matrix from a csv file, check that it is a matrix (by checking if it contains only numbers and checking if it is rectangular), among other things. We are going to add some functionality to this code that will allow us to load a vector from a file, check if it contains only numbers, check if we can perform a multiplication between a loaded matrix and vector and then actually perform that multiplication. When performing matrix-vector multiplication, the vector in question is what is called a column vector, so this is how it will be stored in a file. The vector files will be csv files that contain one entry per line and the number of lines in the file determine how big the vector is going to be. In other words, a column vector is simply a matrix with one column. You are being supplied with a matrix.py module. You are going to import this code into a new python code file you are going to write to solve this problem (called question3.py ). You are not allowed to change any of the functions in the supplied module and must use them as is . After importing the supplied module, your program will do the following things: 1. Prompt the user for the filename of a matrix csv file. You will load this matrix from file (using the loadMatrix function in the matrix module) and check that it is a matrix (using the isMatrix function in the matrix module). If it is a valid matrix, print it to the screen (using the printMatrix function in the matrix module). If it is not, prompt the user for another matrix csv file. Continue to do this until the user provides a file that contains a valid matrix. 2. Prompt the user for the filename of a vector csv file. As we have already mentioned, a column vector is simply matrix with one column. So we can use our loadMatrix and isMatrix functions to load and ensure that the data is numerical and rectangular. You will then need to write a new function called isColumnVector that takes your loaded vector and check to make sure each row only has one element in it (ie. it is a column vector). This function will return a boolean: True if the loaded vector is actually a column vector and False otherwise. If the file selected by the user contained a valid column vector, print it to the screen (using the printMatrix function). If it is not, prompt the user for another vector csv file. Continue to do this until the user provides a file that contains a valid column vector. 3. We now want to check if we can perform a multiplication between the loaded matrix and vector. To do this you will write a function called canMultiply that takes two parameters, your matrix and vector. To be able to perform multiplicationbetween a vector and a matrix, the vector has to have the same number of entries (rows) and the matrix has columns. Your function will check for this and return True if this is the case and False otherwise. 4. If you cannot multiply your matrix and vector together (based on the function you wrote in the previous step) you will let the user know and end your program. If you can multiply them together you will do so. To do this you will write a function called multiply that takes two parameters, your matrix and column vector. This function will multiply them together, storing the result in the same matrix representation as your matrix and vector. Your function will return this list of lists representation of the product. You will then print the result to the screen using your printMatrix function. For example, if your matrix (A) and vector (x) are given by A = [ [5 2 3], [2,4,6] ] x = [ [1], [2], [4] ] Then canMultiply(A,x) will return True and multiply(A,x) will return the column vector [ [21], [34] ] With the assignment, you are being provided with two matrices and two vectors. matrix1.csv and vector1.csv can be multiplied together, as can matrix2.csv and vector2.csv. HINT: The matrices store the values as strings (as this is how the module was implemented for tutorial 3). This means that you will need to convert the entries of the matrix and vector to integers to perform the multiplication/sum and store the result of this back as a string so the resulting vector can be printed using the printMatrix function. You do not need to convert the entire matrix/vector from strings to integers, only do this on the elements while you are performing the math operations.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

1 2 3 Data Base Techniques

Authors: Dick Andersen

1st Edition

0880223464, 978-0880223461

More Books

Students also viewed these Databases questions

Question

5-4 What are the current computer software platforms and trends?

Answered: 1 week ago

Question

3. What are potential solutions?

Answered: 1 week ago