CSCI 170 (GOLDWEBER)
Spring 08: 3/13/08
Homework 5
Due Date: Thursday March 27, 2008.
There is a possible 42 points for this homework assignment.
You will be using Python for the problems in this assignment. Create a function for each problem with the names indicated. Store the functions in one file called hwk5_lastName.py (where you replace lastName with your last name). You will hand in both a hardcopy of your file in addition to emailing a copy to me. Each function should be loadable and runnable in the Python interpreter. Ask for help early and often.
Problem 1. (5 points) Write a function called findSmallest, that takes as input a list, listOfNumbers, of numeric values. It should output the smallest value of the list. You should use findLargest as your model.
Problem 2. (7 pts.) Write a function called countEm that takes as input a number n and a list, listOfNumbers, of numeric values and outputs the number of occurrences of n in listOfNumbers.
Problem 3. (10 points) Write a function called perfect, that gets a positive integer value n from the user and determines whether that number is a perfect number. It should output "perfect" or "not perfect" depending on its status. A perfect number is an integer that is equal to the sum of its proper factors. For example, 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28.
Problem 4. (10 points) On its first voyage ever, the ocean liner Ship O' Doom strikes a whale and sinks (the whale survives; no animals were harmed during the construction of this homework assignment). Three survivors: Pat, Gene, and Dana are washed up onshore on an island which is deserted with the exception of a pile of coconuts and a monkey. Before sleeping for the night, the survivors decide that in the morning they will divide the coconuts between them so that each gets the same number, giving any extras to the monkey.
During the night, Pat gets up and divides the coconuts three ways, and finds there is one left over. Pat gives it to the monkey and then hides his/her third of the coconuts. Piling the remaining coconuts back into a single pile, Pat then goes back to sleep.
Later, Dana wakes up and (not suspecting that Pat has done this already) divides the remaining pile of coconuts into thirds, and finds there is one left over. Dana gives it to the monkey, hides his/her third of the coconuts, and piles the remaining coconuts together.
Of course Gene also wakes up during the night, divides the (remaining) pile into thirds, has one left over which is given to the monkey, hides his/her third, and piles the remaining coconuts together.
In the morning, the three awake. All profess they have slept soundly the whole night through and are now ready to divide the coconuts. They divide the pile into thirds, and find they have one left over. They give the remaining coconut to the monkey.
Write a function called shipOfDoom which takes as input a number of coconuts and determines if this number of coconuts could have been the number of coconuts in the original pile. (i.e. The output is either a "yes" or a "no".)
Hint: 79 is a number that should work.
Problem 5. (10 pts.) Write a function called findMedian that takes as input an odd numbered list, listOfNumbers, of numeric values. It should output the median value of the list. Hint: Do not attempt to solve this problem all in one function. Write a findMedian function so that it calls/uses your already written findSmallest function.
File translated from
TEX
by
TTH,
version 3.74.
On 14 Mar 2008, 11:00.