Approved-online-essay-writers

Lists, While Loops, and Files

CS 120 Project #3 – Lists, While Loops, and Files
Due Fri Oct 24 (revised)
Write Plfthon 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, 15, 251
B = [2.1, 4.2, 8.4, 16.8]
C = [horse , cow, pig, sheep]
Z = [0. o, o, o, 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 afier 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.
(1) 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 th
sample size is an odd integer, the median is the middle score. If the sample s1ze 18 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 45.55 7.25 18.44 7.24 -15.87
(a) 1 10 100 1000 10000 100000 1000000

We Write Essays for Students

Tell us about your assignment and we will find the best writer for your paper

Get Help Now!

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
flipCo in ( ) 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 (= l) in X. A third function cumHeads (x, 1“) 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

CS 120 Project 3 – Data entry with lists (compare Chapter 5 in textb ook)

1. Definite iteration

X = [J

N = eval(input(“Sample size:

for count in range(O,N): – ‘ a 7
value = eval(input(“Next value:

X append(value)
print(x)
2a. Indefinite iteration – prompt to continue
X = f]
moredata = “Y”
while moredata == “Y” or moredata == “y”:

value = eval(input(“Next value:

X.append(value)

moredata = input(“More data (Y or N);”)

N = len(X)
print(X)
2b. Indefinite iteration – with sentinel
X = [J
sentinel = -999
value = 0
while value != sentinel:
value = eval(input(“Next value:
if value I: sentinel:
X.append(value)
N = len(X)
printtX)
2c. Indefinite iteration – press Enter to quit 7
X = i] – ‘3
emptyStr = “” ‘ I . 7
txtvalue = “go” ‘ 1.’
while txtvalue I: emptyStr:
txtvalue = input(“NeXt value (press Enter to quit): “} ‘ “V” i;

if txtvalue 1: emptyStr: . I’ . f

X.append(eval(txtvalue)) ‘ a 5- .,,f
N = len(X) . , ¢.~.7§_’j
print(X) . . ‘7‘ “.-“;’,fflfi

I. .. I;IHJ.,QRE§§

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

Click the button below to order this paper.

The post Lists, While Loops, and Files appeared first on Speed Essays.

Welcome to originalessaywriters.com, our friendly and experienced essay writers are available 24/7 to complete all your assignments. We offer high-quality academic essays written from scratch to guarantee top grades to all students. All our papers are 100% plagiarism-free and come with a plagiarism report, upon request

Tell Us “Write My Essay for Me” and Relax! You will get an original essay well before your submission deadline.

PLACE YOUR ORDER