Question 1 (15 points) Question 1 provides practice with files and string operat

Question 1 (15 points)
Question 1 provides practice with files and string operations. Specifically, you will need to use
– file operations, including opening a file, reading lines from a file, and closing a file (see slides for examples)
– the string find function and the string slicing operator : that is used when extracting substrings from a string, by
using this syntax [ < num1 > : < num2 > ]
What should my program do?
This program will read a series of assignment scores from a file with a specific format (see “Format of the file” section below), and after it is done processing the whole file, print the average score. No error checking is required (i.e., assume the user will enter a valid file name, assume that the file will have valid contents). An algorithm for your program is on the next page – please use this algorithm to implement your solution.
Format of the file
Assume the scores are stored in a file with the following format:
Id1 assignmentScore
Id1 assignmentScore
Id2 assignmentScore
Id3 assignmentScore
< etc. – the number of lines is not known beforehand>
Each line contains an ID and an assignment score; the ID is separated from the assignment score by a single blank. A sample file is below. We give you this file on Brightspace and you can use it to test your program, but make sure you grab the correct one, i.e., for Windows vs. Mac. The one for Mac is called test_Mac.text and the one for a Windows is called test_Windows.text. You need to provide the correct name in your program when opening the file.
Format details : Each line in the file contains a student ID (a string that starts with the character s followed by one or more numbers), followed by a blank, followed by an assignment score, followed by the newline character (“n”) .We can’t see this newline character but it’s there (if it wasn’t we wouldn’t have multiple lines). Note that as far as the ID, its length varies (some have just one number after the “s”, some have several) – this can also happen for the score if someone gets a really low one (e.g., 5). Also, it was up to the students as to how many assignments they completed, thus some students have just one line in the file, while others have several. But, here, we just want to know the overall average for all the assignment scores (i.e., we ignore who they were done by).
How do we compute an average score? We get the sum of all the scores and then divide that sum by the number of scores. Sample run:
Where do I start?
Step 1: double check you understand where the output 53.0 in the sample run is coming from. If we add up all the scores (10+50+25+90+56+54+ 22+50 +50+60+ 80+10+90+95) and divide by the number of scores (14), we get 53, exactly what our program output.
Step 2: figure out the algorithm for getting the average before writing code – if we don’t understand the problem, we can’t write a program for it. So as an exercise, think of what algorithm would solve this problem before looking at the proposed algorithm. When you are ready to continue, compare your answer to the algorithm on the next page.
Algorithm – use this to implement your program (answers with the split function will not be accepted):
Initialize a variable that will keep track of the number of scores read, call it num_read
Initialize a variable that will keep track of the total of the scores, call it res
Ask the user for a file name and open the file for reading [the name must match the name of the test file you downloaded,
including the .txt extension – sometimes that doesn’t show up, if you have it turned off in the file browser Now process the file (use a for loop to get each line in the file)
Extract the assignment score from the line (see hint 1 below)
Convert that score to a number
Add the score to the total of scores thus far (stored in the variable res) Increment num_read by 1 (to reflect the fact we just read another score)
Print the average of the scores (obtained by dividing the total score by the number of scores read) Close the file
Hint1: How do we extract the score from a line?
From the description on the previous page, we know that each line contains a student ID, followed by a blank, followed by a
score, followed by the newline character (“n”). We also know that each line is a string. Let’s look at some examples. Ex1: If a line corresponds to this string of length 7:
s15 10n we can visualize this as a series of characters as follows:
note that “n” is a single element of the string (rather than two elements).
Ex2: Here is another example this time assuming we have this line (i.e., string) of length 8:
s152 69n we can visualize this string as a series of characters as follows:
Ex3: Here is a final example assuming we have the line (of length 5): s9 5n which we can visualize like this
Note that we don’t know in advance how long a given ID is, and we don’t know in advance how long the score portion (represented as a string) will be either.
Back to our question: how to extract the score from the line? Answer:
– usethestringfindfunctiontofindthepositionoftheblankintheline(theoneseparatingtheidfromthescore) – usethestringfindfunctiontofindthepositionofthenewlinecharacter“n”
– extractthecharactersinbetweenthatblankpositionandthenewlinecharacterposition(seeWeek6-1slides)
Supplementary exercise: Trace this algorithm on paper with the sample test file. You do not have to pass this in.
Common pitfalls and some more hints:
– donotuse(1)alist(s)or(2)thesplit()function(answersusingthesewillnotbeaccepted)
– putthetest.txtfileinthesamefolderasthepython.pyfilethathasyourprogram
– checkyougrabbedthetest.filethatmatchesourcomputertype(e.g.,grabbedaMactestfilebutyouhaveaWindows
machine)
– doNOTputablankinfrontofthefilenamewhenenteringittotestyourprogram–pythontreatsthatblankaspartofthe
file name. A fancier solution (not required would be to use the strip() function to strip whitespace – not required, but feel
free to use it if you want)
– donotassumeeachidisthesamenumberofcharacters(i.e.,usevariablesintheslicefunctioncorrespondingtopositions
returned by the find function); likewise for the score
– do not hardcode the number of lines to 14 – it is 14 in this file, but the program should work with a different file
Implement and test your program. You must use the algorithm provided.Save your file as A3_1.py and upload it to
Brightspace.
Question 2 (20 points)
This is a shorter program, so we’ve left the algorithm design part up to you. Make sure you understand how to solve the problem before starting code – to do that, go through the two examples below and figure out a strategy for your program. Once you do, write it out in plain English (e.g., like we did for the algorithm we provided in question 1). Only then write your python code.
Suppose we have a program that has as its first line a list of strings. For instance, this first line could be:
This program takes the original list and creates a new list by concatenating consecutive elements in it as follows:
A few things to note:
– each element in the new list is the result of concatenating the two consecutive elements in the original list (with a dash in
between), and then moving forward just beyond that second element. Thus, the new list will have half as many elements as the original list
– you can “hard code” the list in the first line of the program – make sure it is of even length (see one example above;
another one is below
– we may test your program with other, longer lists but they will always be of even length
– your program output should match the one above (print the original and new list)
– you must use a for loop to process the original list
– you must not use the join or split built in operator (doing so will result in a 50% penalty)
Here is another example, assuming that we have a different list, i.e., this one:
Output:
Hints:
– as noted above, you can hard code the original list in your program (i.e., you do not have to ask the user for it), just make sure it has an even number of elements (see two examples above)
– we will go over how to add elements to a list in week 7-1 – will need to do that here
– you already know how to do string operations like string concatenation (i.e., how to concatenate two strings) – will need
that here
– remember to use a for loop
– range function will be useful – here is some info
o range can work with three arguments, see week 5-2, slide 14
§ soforinstancerange(0,10,2)producestheseriesofnumbers02468
o range requires integer(s) as its argument(s). If you divide an integer by 2 you will have to convert it back to an integer, here is a simple example:

Posted in Uncategorized

Place this order or similar order and get an amazing discount. USE Discount code “GET20” for 20% discount