Imagine that the != operator for numbers has suddenly been removed from the Python programming language. We need to find a way to replace it with the remaining operators in the language. Remember that before it went missing, the != operator took two numbers, and returned True if they were not equal to one another, and False otherwise.
Create a new file not_equal.py. Create a new function not_equal. We still have the not operator (which can be applied to booleans) and the == operator, which can be applied to numbers.
Now let's try and test our new function. Create 4 variables a, b, c, and d, such that a and b have the same value and c and d are different from one another.
Now write a call to your not_equal function, passing it a and b and assign its return value to a variable not_equal_test1. Then print out the value of a and b and the value that was returned by your function not_equal. This tests whether your function does the right thing when the two values are actually equal.
Draw a stack diagram that shows all the variable and their assignments when you run your program. Draw the stack for the moment when not_equal returns.
Now test that your function works when the parameters are not equal (by passing it c and d). Assign the return value to a variable not_equal_test2 and print out the values.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps6 not_equal.py
Create a new file simple_methods.py. Write the following methods and test them to make sure they work.
Write a method to print a countdown from 100 to 1.
Write a method named is_divisible that takes two integers, n and m and that returns True if n is divisible by m and False otherwise.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps6 simple_methods.py
Look at the function below and test it out. Save the code in a file sum_all.py.
def sum_all(t): total = 0 for x in t: total += x return total
The function takes a list of numbers as a parameter and returns the sum of all the numbers.
Now modify the function so that instead of returning the sum of all the elements, it returns the cumulative sum; that is a new list where the ith element is the sum of the first i + 1 elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps6 sum_all.py
Here you will write a program that will allow the user to enter a sequence of temperatures and then will print out statistics. Here is an example of how the program will work:
How many temperatures? 4 Enter temperatures now: ? 70 ? 65 ? 78 ? 82 Minimum temperature: 65 Maximum temprature: 82 Average temprature: 73.75
Think about how to break the program into parts. What are the different operations that we do here?
Create a file temperature_stats.py. Enter a comment for each of the major parts of the program.
We will create a function for each of the major parts of the program. Think about what parameters those functions will need as input and what they would return. Usually, if a function is performing some computation, it will return back a result when it is done. Now write the code to call the appropriate function below each comment in your program.
Add a function definition (def) for each of the functions that you called.
Run your program to check that you do not have any syntax errors, and fix them if any.
Something like this:
return [ 70, 65, 78, 82]
Run the program to spot errors.
Create functions to compute each of the different statistics. Each function will receive the list of temperatures and compute the minimum, maximum and average tempartures accordingly. For example, to compute the minimum temperature, you can use the following function:
def minimum(lst): res = lst[0] for item in lst[1:]: # if the value of item is less than the value of res, # update res with the new low value if item < res: res = item return res
Create a function that would print out the statistics. You might have also chosen to print the stats in the main body of your program. If you did so, still create a function to print the statistics and move your code there. This is called refactoring (changing the structure of your code without changing the behavior of your program).
Run the program and make sure it works.
Now that we know that our functions for computing and printing statistics work. We can go back and change our input function to gather the temperatures from the user.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps6 temperature_stats.py
This exercise will allow you to practice with the methods for strings and lists. An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, RAM is an acronym for “random access memory.” Write a program that allows the user to type in a phrase and outputs the acronym for that phrase. Your program should work as follows:
Enter a phrase: random Access memory
Your acronym is: RAM
Note: The acronym should be all uppercase, even if the words in the phrase are not capitalized
Save your program as acronym.py. When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps6 acronym.py
If you don't remember what the problem was about, go back and look at problem set 2. Now create a modular program. Follow the approach we took in exercise 4. Here is how your program should work:
Enter your name: Harry Potter Enter your date of birth (MMM-DD-YYYY): JUL-31-1980 Harry Potter, your birthday fell on a Thursday.
Note: If the month is January or February, then the preceding year is used for computation. This is because there was a period in history when March 1st, not January 1st, was the beginning of the year.
Note: If the computed value of R is a negative number, add 7 to get a nonnegative number between 0 and 6.
Note: To convert the string '90' to the number 90, int('90').
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps6 zellers.py