Starting from:

$29.99

PHYS2160 Lab 3: Functions, File Processing, and Arrays Solution


Exercise 1: Recursive Function to Evaluate a Finite Sum
AIM:
An approximation to the function x/(1 − x)2 for |x| < 1 is given by the finite sum:
n x
( 1 − x)2 = ∑ixi
i = 1
where n is a finite large number. Write a Python program that implements the recursive function fsum(x, n) to compute the above finite sum. Your program should also contain the code that displays this sum to 8 decimal places for x = 0.1, 0.2, 0.3, 0.4 and n = 2, 5, 10, 50, 100 by using this function.
ALGORITHM:
Notice that 𝑥 2 = ∑𝑥𝑖=1 𝑖𝑥𝑖 can be re-written as nx 1 ixn−1
1−𝑥
This can be implemented using a recursive function where we set the base case to be n = 1 for which we simply return x, otherwise we return nx + fsum(x, n-1).


Exercise 2: Manipulating the Data from a Text File
AIM:
[Number of first marriages registered in HK by sex and age group]
Male
16-19 289 232 220 175 155 31
20-24 4331 3076 3512 3737 3036 851

>= 50 386 493 1690 1068 1014 581
Female
16-19 1213 966 935 683 511 98
20-24 10066 6613 7972 8286 5805 1545

>= 50 140 93 156 227 320 319
Write a Python program that reads the data from this file, find the total number and dominant age group of first marriages registered in Hong Kong by sex and year, and finally print a table of the results with the following format on the screen:
Sex Year Total Number Dominant Age Group
----------------------------------------------
Male 1995 34080 25-29 Male 2000 26176 25-29

Female 1995 34232 25-29 Female 2000 26605 25-29

ALGORITHM:
Initialize two dictionaries to store the data for each of the years for the males and females.
Initialize two lists to store the years in which the data is collected and age groups for the population.
Read the file, iterating over the lines containing the male and female data separately.
In each line, separate the words delimited by a space/tab.
Each item in this new list corresponds to a column of the data
Read the age groups from the lines and store it in the initialized list [1st Column].
Go over the columns and add it to the corresponding year in the data dictionary.
Print the data in the desired format where the total number is simply the sum of data in a given year and the most dominant age group can be found by mapping the index of the mode of the data in a year to it’s age group





Exercise 3: Printing a Histogram to a Text File
AIM:
[Age Group] [Male] [Female]
0-4 137400 127400
5-9 154600 144800 10-14 154400 150700

80-84 79900 94000
>=85 79100 138700
Write a Python program to show the statistics of the data in this file by printing a histogram in the following format to a text file HKPop2020hist.txt:
(in nearest ten thousands)
0-4 | ##############&&&&&&&&&&&&&& (14/14)
5-9 | ###############&&&&&&&&&&&&&& (15/14) 10-14 | ###############&&&&&&&&&&&&&& (15/15)

>=85 | ########%%%%%%%%%%%%%% (8/14)
#-Male; %-Female ALGORITHM:






Exercise 4: Evaluating a Test with Arrays
AIM:
A test consisting of 20 multiple-choice questions with 5 possible choices (A, B, C, D, and E) is conducted for a group of 5 students. Write a Python program to evaluate the answers of these students using the following algorithm:
(a) Read the string of the correct answers to the questions from the user and store the answers into an array of characters.
(b) Read the string of the answers of a student from the user and store the answers into an array of characters.
(c) Construct a Boolean array to indicate whether the answer of the student to each question is correct.
(d) Use the array in (c) to count the number of correct answers and then print the results.
(e) Repeat steps (b) to (d) for each student.
You can assume that all the inputs are in the required format. Here are the sample input and output of this program:
Enter the correct answers to the MC questions:
EEDAECAEEEBCADDBCEEB
Enter the answers of Student-1:
CEACBBDBDBCBCEEADABB
Number of correct answers: 2
Answers to the following questions are correct:
2 20
Enter the answers of Student-2:
EEDAECAEEEBCADDBCEEB
Number of correct answers: 20 Answers to the following questions are correct: ALL

ALGORITHM:
Obtain the list of all solutions and store the result in an array of characters (C-string)
Iterate over the 5 students, asking for the answers of each and storing their response in a array of characters (C-String)
The Boolean array is implicitly created during the comparison of each character
For each student go over their answers, character by character comparing it with the solution list and adding the questions they got right into a list
Print out their results at the end of the iteration for the student.

More products