CS1010E: Programming Methodology
Week 04: Tuples, List and Iterables
Files Questions
• Week04.pdf 1-3 String Evaluation
• week04.py 4-6 Tuple Evaluation
7-8 List Evaluation
Coursemology 9-10 Burger Meal Order
• Tutorials > Week 05 Tutorial 11 Non-Negative
12 Interleave Tuples
Evaluations
For questions 1 to 8, please try to come up with the answer without using IDLE/Python first. Then
type in the expressions into IDLE to verify your answers. The objective is for you to understand
why and how they work.
Question 1: String(A)
Python compares strings character by character using each character’s ASCII value. Every char-
acter has a unique ASCII value as shown in the ASCII table (shown in lecture notes).
Expressions Output
print('abc' > 'aba')
print('abc' < 'Abc')
print('abc' > 'ab')
print('abc' > 'ba')
Question 2: String(B)
Both positive indexing and negative indexing can be used to access a character in a String. (Note:
actually the same can be applied on Tuple or List to access an element in a tuple/list.)
1
Expressions Output
s = 'banana'
print(s[2])
print(s[4])
print(s[-1])
print(s[-2])
print(s[-4])
Question 3: String(C)
String slicing returns a substring which may contain more than one characters (comparatively,
string indexing returns a single character).
In string slicing, parameter step determines the direction of slicing (i.e. left-to-right if step
is positive, or right-to-left if step is negative). The start position and stop position of slicing
can be expressed using either positive index or negative index . (Again, the same can be
applied on Tuple and List.)
Expressions Output
print('banana'[2:4])
print('banana'[4:])
print('banana'[-2:])
print('banana'[-2:-4:-1])
print('banana'[-4:-2:-1])
Question 4: Tuple(A)
Expressions Output
tup_a = (10, 11, 12, 13)
tup_b = ("CS", 1010)
tup_c = tup_a + tup_b
print(tup_c)
print(len(tup_c))
tup_d = (tup_a, tup_b)
print(tup_d)
print(len(tup_d))
2
Question 5: Tuple(B)
Expressions Output
tup_a = (10, 11, 12, 13)
tup_e = tup_a[1:3]
print(tup_e)
tup_f = tup_a[-3:-1]
print(tup_f)
tup_g = tup_a[2:0:-1]
print(tup_g)
Question 6: Tuple(C)
Expressions Output
tup_b = ("CS", 1010)
tup_h = tup_b + (2.5,)
print(tup_h)
i = tup_b[0] * 2
print(i)
Question 7: List(A)
Expressions Output
lst_a = ['CS', 1010]
lst_a.append('E')
print(lst_a)
tup_j = ('is', 'cool')
lst_a.append(tup_j)
print(lst_a)
(Note: You may want to change the second last line of the program to lst_a.extend(tup_j) ,
and run the modified program in IDLE to view the difference between append and extend ).
3
Question 8: List(B)
Expressions Output
lst_b = ['CS', 1010, 'E', ('is', 'cool')]
print(len(lst_b))
print(lst_b[3][0][1])
Burger Meal Order
In this exercise, you are hired by a fast food franchise Python Burger to implement their automatic
ordering system. One order can contain multiple burgers and each burger is a string (of ingredients).
For example, my order for my family can be:
>>> my_order
('BVPB', 'BCPCPB', 'BPCBPCB')
So an “order” is a collection (as tuple) of burgers.
Question 9: Do I Have Enough Money?
Write the function has_enough_money(order, money) to check if you have enough money to pay
for your order. The function returns True if money is greater than or equal to the total price in
order or False otherwise. The order is a tuple of burgers, e.g. ('BVPB', 'BCPCPB', 'BPCBPCB') .
The meaning of respective capital letters is shown in the following table.
Code Ingredient Price
'B' Bun $0.5
'C' Cheese $0.8
'P' Patty $1.5
'V' Vegetables $0.7
'O' Onions $0.4
'M' Mushrooms $0.9
Sample Run #1
>>> my_order = ('BVPB', 'BCPCPB', 'BPCBPCB')
>>> has_enough_money(my_order, 15)
True
4
Sample Run #2
>>> my_order = ('BVPB', 'BCPCPB', 'BPCBPCB')
>>> has_enough_money(my_order, 13)
False
Question 10: Can I Cancel That?
Write the function remove_burger_from_order(order, burger) that returns a new order with
all occurrences of the given burger removed. If there is no such burger in the order , the
function returns the original order unchanged.
Sample Run #1
>>> my_order = ('BVPB', 'BCPCPB', 'BPCBPCB')
>>> my_order = remove_burger_from_order(my_order, 'BVPB')
>>> my_order
('BCPCPB', 'BPCBPCB')
>>> my_order = remove_burger_from_order(my_order, 'BVVVVVPB')
>>> my_order
('BCPCPB', 'BPCBPCB')
Question 11: Non-Negative
Write a function non_negative(tup) that takes a tuple of integers as parameter and returns
True if all the integers are non-negative, or False otherwise.
For example, function call non_negative( (1, -2, 3, 8, 6) ) returns False and another
function call non_negative( (3, 1, 8, 6) ) returns True .
Note: A common mistake many students make is hasty conclusion: the moment one non-
negative integer is encountered, the function concludes that the whole tuple is non-negative. This
is a logical error. The other case is more of an efficiency issue: a negative integer is encountered,
but the function goes on to check the rest of the tuple, which is unnecessary.
Question 12: Interleave Tuples
Write a function interleave(t1, t2) that takes two tuples of integers, t1 and t2 , as param-
eters, and returns a tuple t3 in which the values of t1 and t2 are interleaved (first element of
t3 is from t1 ).
If t1 has more integers than t2 , append the remaining values of t1 at the back of t3 .
Similarly, if t2 has more integers than t1 , append the remaining values of t2 at the back of
t3 .
For example, suppose t1 = (1, 3, 5) and t2 = (2, 4, 6) , function call interleave(t1, t2)
returns (1, 2, 3, 4, 5, 6) .
As another example, suppose t1 = (1, 3) and t2 = (2, 4, 6, 8) , function call interleave(t1, t2)
returns (1, 2, 3, 4, 6, 8) .
5
As a third example, suppose t1 = (2, 4, 8, 0, 5) and t2 = (-1, 10) , function call
interleave(t1, t2) returns (2, -1, 4, 10, 8, 0, 5) .
Note: The same problem solving strategy can be applied on Assignment 2 Q9.