Posted: November 21st, 2014

Lists, While Loops, and Files

Lists, While Loops, and Files

Write Python programs to perform the following computations using lists. Each program should be divided into a main procedure that performs input and output, and

separate functions when appropriate to do the calculations. Input is from the keyboard, and output is to the screen.

1.    Basic list operations:  The main program should define the following lists (no data entry is required):
A = [4, 9, 16, 25]
B = [2.1, 4.2, 8.4, 16.8]
C = [horse, cow, pig, sheep]
Z = [0, 0, 0, 0, 0, 0, 0, 0, 0]
Perform the following list operations in the main program. Separate functions are not needed.
(a)    Append the number 36 to list A.
(b)    Insert the value 6.3 after 4.2 in list B.
(c)    Delete “pig” from list C, and replace it (in the same position) with “chicken”.
(d)    Define list D as the values of list A in reverse order.
(e)    Define list E as the first 3 values of lists A and B concatenated and in sorted order.
(f)    Define list F as the members of list C in upper case.
(g)    Change the values in list Z from all zeros to the integers from 1 to 9.
Output the final results for lists A, B, C, D, E, F, and Z.

2.    Data entry and statistics: The main program should input decimal (float) numbers from the keyboard until a sentinel value is observed. (Use a while loop.) Data

entry values should be appended to a Python list called X.
The main program should then sort the data in the list using the sort function for lists. After X is sorted, call two functions: trimMean(X) and median(X), which

return the trimmed mean and the median of the data in the list.
The trimmed mean is obtained by calculating the sum of the scores, removing the highest and lowest scores from the sum, and then dividing by the sample size minus 2.

For the median, when the sample size is an odd integer, the median is the middle score. If the sample size is even, the median is the average of the two middle scores.
Output the sample size, plus the return value for each function.
Compile and run your program with the following input samples:
(a)        23.4   17.8   12.5   41.1   78.9
(b)        7.20   -15.66   7.25   18.44   7.24   -16.87
(c)        1   10   100   1000   10000   100000   1000000
(d)        12   23   34   56   78   90
(e)        99   87   75   63   51

3.    Input numeric data from a file: The main program should allow the user to choose a data file. The program should then open the file for reading. The data file

has one numeric value per line. Input the values line by line, and convert each value to a floating point number. Store the numbers in a Python list called X.
The main program should sort the data in X, using the sort function for lists. After X is sorted, call the two functions that you wrote in Program 2: trimMean(X) and

median(X).
Output the sample size, plus the return value for each functions.
Compile and run your program with the following input data files:
(a)        AutoMPG.txt
(b)        Poverty.txt
(c)        CrimeRate.txt

4.    Random numbers with lists: The main program should input the number of times N to toss a coin. A main loop with N iterations should be performed. In each

iteration K, the function flipCoin() should be called. This function returns 1 (representing heads with probability 1/2) or 0 (representing tails with probability 1/2)

for one coin flip. Use the Python random library inside your flipCoin function.
For each iteration K, the K-th location in the X list should receive the return value (0 or 1) for the latest coin flip. The complete X list will contain the

sequential results of all coin flips (unsorted).
The main program should call a second function proportionP(X), which returns the proportion of heads (= 1) in X. A third function cumHeads(X, F) should also be called

to calculate the total number of heads achieved in F coin flips. Output the proportion and cumulative heads statistics.
Compile and run your program with the following input values for N and F:
(a)        N = 100            F = 50
(b)        N = 100            F = 80
(c)        N = 1000            F = 400
(d)        N = 1000            F = 600
(e)        N = 10000        F = 5000

PLACE THIS ORDER OR A SIMILAR ORDER WITH US TODAY AND GET AN AMAZING DISCOUNT 🙂

Expert paper writers are just a few clicks away

Place an order in 3 easy steps. Takes less than 5 mins.

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00
Live Chat+1-631-333-0101EmailWhatsApp