C4V Intro Python Assignment #4 – Tuples, Lists
Part A: Tuples
Complete all of the exercises in Chapter 9 – Tuples, including one additional question #4:
1. We’ve said nothing in this chapter about whether you can pass tuples as arguments to a
function. Construct a small Python example to test whether this is possible, and write up
your findings.
2. Is a pair a generalization of a tuple, or is a tuple a generalization of a pair? (this is a
written question, use pen-and-paper and explain why)
3. Is a pair a kind of tuple, or is a tuple a kind of pair? (this is a written question, use pen-
and-paper and explain why)
4. Write a function that accepts a string parameter (NO GREATER than length 5), and
eturns the string in a tuple format, where each value in the tuple is a single character
from the string. Consider how to initialize tuples and how this might work with a fixed-
length string.
Submit your responses to Part A including code if needed (formatted in a monospace font like
Courier New) as a Word-compatible file.
Additionally, complete the following questions and submit a .PY file with your word-compatible
file for Part A.
Part B: Coding with Lists
For Part B, you will write a basic program to work with numeric grades on a scale of XXXXXXXXXXTo
do this, you will need to implement the following functions:
1. LinearSearch(listOfGrades, gradeValue) returns 0 or 1
A linear search is a common operation in computer programming, usually done with a
list or a
ay data structure. The point of the linear search is to start from the beginning
of the list and scan each item in the list until the end to see if a value exists in the list. If
it does, then you can stop the search. This function can be implemented with a for-loop,
if statement, and a
eak statement. Return 1 if the gradeValue is found in listOfGrades.
Return 0 otherwise.
2. FastSort(listOfGrades) – can print in sorted order or return a sorted list
This fast sort function should sort the listOfGrades. The way you should implement this
sort is by creating a new list with one item for each possible grade: 0 to 100. This sort
should then have 101 items in it. Every grade you find in the listOfGrades, set the
element in the new list to be = 1. Then, use a for loop to iterate over the listOfGrades
and use an if statement to check if the element = 1, if it does, then print it out.
3. NumberOfOccurences(listOfGrades, gradeValue) – returns nothing
Similar to the FastSort function, you will need to use a new list of 101 items to get this
to work. Each time you find a value in listOfGrades, set your newList[position you are at
in listOfGrades] to be increased by 1. You should then be able to iterate over this new
list and print out the number of occu
ences of different grades.
4. GradesInRanges(listOfGrades) – returns nothing – prints the number of grades in each
ange
GradesInRanges should print out how many grades are in each letter grade. Assume the
following:
A 80-100
B 70-79
C 60-69
D 50-59
F 50 or below
You can model this with an if-elif statement. You could use a list to keep track of the
sums for each range or make 5 integer variables.
You should also have a main function that looks like this (you
can copy + paste it):
def main():
grades = [58, 77, 77, 65, 65, 65, 66, 81, 92, 47]
grades2 = [55, 72, 83, 89, 51, 62, 64]
LinearSearch(grades, 58) #returns 1
LinearSearch(grades, 100) #returns 0
FastSort(grades2) #print
eturns [51, 55, 62, 64, 72, 83, 89]
GradesInRange(grades2) #prints A – 2 B – 1, C – 2, D - 2
main()
How to Think Like a Compute
Scientist: Learning with Python 3
Documentation
Release 3rd Edition
Peter Wentworth, Jeffrey Elkner,
Allen B. Downey and Chris Meyers
August 12, 2012
CONTENTS
1 The way of the program 1
1.1 The Python programming language . . . . . . . . . . . . . XXXXXXXXXX1
1.2 What is a program? . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX3
1.3 What is debugging? . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX3
1.4 Syntax e
ors . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX3
1.5 Runtime e
ors . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX4
1.6 Semantic e
ors . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX4
1.7 Experimental debugging . . . . . . . . . . . . . . . . . . . XXXXXXXXXX4
1.8 Formal and natural languages . . . . . . . . . . . . . . . . XXXXXXXXXX5
1.9 The first program . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX6
1.10 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX7
1.11 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX7
1.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX9
2 Variables, expressions and statements 11
2.1 Values and data types . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX11
2.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX13
2.3 Variable names and keywords . . . . . . . . . . . . . . . . XXXXXXXXXX14
2.4 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . XXXXXXXXXX15
2.5 Evaluating expressions . . . . . . . . . . . . . . .