Description: Description: Description: Description: Description: Description: Description: Description: Breaking News! Click here for details.

 CSC 131 - Introduction to Computer Science

Homework 6 – Due Thursday, Oct. 19, 11:59pm

Submitted on Blackboard (learn.uncw.edu)

Programs based on material from Chapter 4.

 

Program 1: Read from user input and add evens to list

Write a Python program, IgnoreOdd, that asks the user to enter integers.   The program should stop asking for input when the user simply presses Enter.    As each number is read in, add it to a list, if and only if, the number is even.

 

When all the input is read, print out the following on separate lines (with labels):

·         The list.

·         The length of the list.

·         How many numbers in the list are also divisible by 4.

 

Program 2: Read names from a file

Write a Python program, ReadNames.py, which asks the user for the name of a text file that (we will assume) contains one first name per line.   As each name is read in, add it to a list.

 

Now, after the list has been completely read in, compute and output the following:

·         Each name printed one per line!  Do not print out the whole list on a line.   Iterate and print out each element.  This will take a loop.

·         Print out an empty line.

·         Print out each name that begins with the letter ‘C’ or ‘c’ (one per line).  This will take a loop.

·         Print out an empty line.

·         Print out each name that is shorter than 10 total characters.  This will take a loop.

 

Now build a new list from the elements of the original list.  This new list should contain all the names that have the letter ‘e’ somewhere in the name.   This will take a loop.

·         Print out the new list (as a list, not one per line).

 

 

Program 3: Statistics

Write a Python program, Statistics.py, which asks the user for the name of a text file that (we will assume) contains one floating point number per line.  As each number is read in, add it to a list.

 

Now that you have the list, compute and print the following statistics.  Please include appropriate labels in the output.

·         Number of elements in the list.

·         Sum of the elements in the list.

·         Average of the elements in the list.

·         Minimum of the elements in the list.

·         Maximum of the elements in the list.

·         Number of elements which are also whole numbers (for instance, 3.0 or 100.0).

 

·         Here’s the tough one.  Do not use the statistics library to do this one.  Compute the median of the list.  Do not use the statistics library to do this one. The median is the number that comes in the middle if the list is sorted.   So if the original list is [5, 2, 4, 9, 8], the median is 5.   That’s because if you sort the list, [2, 4, 5, 8, 9], 5 comes in the middle.   

 

What does “middle” mean?   Take the length and divide by 2.  

 

But, you ask, what about if the list has an even length like [1, 3, 6, 12]?   Which number is in the middle?  In this case, you average the two numbers in the “middle” – in this case 3 and 6 (so the median is 4.5).   Which two elements do you take?  The elements at indices length/2 and (length/2 – 1).