CS 110X Jan 29 2014 : LAB TWO
Tell me and I forget, teach me and I may remember, involve me and I learn.
Benjamin Franklin
1 Nothing Great Was Ever Achieved Without Enthusiasm
1.1 Lab Assignment
This second lab will put to the test your indexing skills.
Write a Python module with a function,
lab2(values), that determines whether a values list reads the
same from left to right as it does from right to left. In other words, is
the list a palindrome.
The lab2(values) function should return True if the list is a
palindrome, otherwise it should return False.
For example, you should be able to match the following output:
>>> lab2([1, 2, 3, 2, 1])
True
Here’s another example:
>>> lab2([2, 4, 4, 2])
True
And one more:
>>> lab2([1, 2, 3])
False
Think about the problem for a bit. You can see that you want to do something for every element in the list; perhaps to compare each element in the list to find a matching partner, or something.
First get started on the structure of your program.
1.2 Structure
Your Python module should be called "lab2.py" and you should store this file someplace where you can retrieve it later.
Run IDLE and open a New Window in which to program. The structure of your program should be as follows:
# Lab 2 # Author: <<Your Name>> def lab2(values): # process # output
For this lab, you will need to fill in the process and output sections that we have used in class. Note that for this lab, the function will not print any output, but rather return the result. Let’s start with reviewing the processing section for function lab2.
1.3 Processing Section
You have seen Indexing on Lists in lecture.
Can you see how this capability will be used to process each element in the list?
For example, if list values contained [1, 2, 3, 2, 1], then you know this is a palindrome because:
values[0] == values[4]
values[1] == values[3]
and values[2] is the middle, which naturally equals itself.
If values contained [1, 2, 2, 1], then this is also a palindrome because:
values[0] == values[3]
values[1] == values[2]
I hope you can see some pattern appearing. You should be able to come up with something like: "If a list values is a palindrome, then for each valid index position i, you know that values[i] == values[???] where you need some computation that involves i and something else.
values[i] == values[???]
Now, given a list, you can start by assuming it is a palindrome. You can do this, for example, by initializing a boolean variable, isPalindrome = True.
Then you can construct a for loop that compares elements in the list to be sure they are the same. Once the first one differs, you can be guaranteed that the input values is not a palindrome, and update the value of isPalindrome accordingly.
1.4 Output Section
Once you have confirmed whether the input values list is a palindrome, then you return either True or False.
As you might expect, you need to use a return statement here.
1.5 Review
Make sure your program has a well-defined # Process section and # Output section.
I expect you can complete this code with a for loop whose body of statements contains an if statement. And then you need a closing return statement that properly outputs the result of the function.
1.6 Extra Task #1: NO CREDIT GIVEN FOR THIS TASK
See if you can properly add the break statement to your code so the for loop stops immediately once it has determined that the values list is not a palindrome. This would be more efficient yet the result of the function will be identical.
1.7 Extra Task #2: NO CREDIT GIVEN FOR THIS TASK
If you still would like a challenge, then try writing a new function triplet(values) that determines whether a list can be identified as being contained of three repeating subsequences; if so, it returns True, otherwise it returns False.
For example, triplet([1,2,1,2,1,2]) would return True while triplet([1, 2, 1, 2])
1.8 Receiving Credit for this lab
Be sure that you use turnin to submit your lab so you can receive credit for the lab.
1.9 Version : 2014/02/05
(c) 2014, George Heineman