Using Tuple
Learning Objectives
At the end of this sub-unit, students should
- know how to solve problems involving tuple.
Finding Values
In this sub-unit, we will discuss some problems involving tuples. The first problem we are going to discuss is a simple one. We will simply find the smallest positive index of a particular value.
Find Index
Problem Description
Given a tuple tpl
and a value val
, we want to find the smallest positive index idx
such that tpl[idx] == val
.
If there is no such index, we simply return -1
.
Task
Write the function find_first(tpl, val)
to find and return the smallest positive index idx
such that tpl[idx] == val
.
If no such value exist, return -1
.
Assumptions
tpl
is a tuple of anything, potentially empty (i.e.,len(tpl) >= 0
).val
is potentially anything but can always be compared via==
with elements fromtpl
(i.e.,tpl[i] == val
is not an error for all valid values ofi
).
Restrictions
- You are NOT allowed to convert to other data types.
As usual, we will describe the problem using high-level operations. The problem can be solved if we can do the following.
- Enumerate all the valid positive index of
tpl
from smallest to largest. - For each valid positive index, we compare with
val
.- If equal, then we return this index.
- Otherwise, we continue.
- If no match until the end, we return -1.
Since we enumerate all the valid positive index from smallest to largest, the first time the condition tpl[idx] == val
evaluates to True
, we are guaranteed the smallest positive index.
Translating it, we will get the following code.
Finding Values
Finding Maximum and Minimum
Now we are going to show how we can return multiple values at the same time. However, that is not exactly the correct way to think about it. We are actually returning only one value, but the value is a tuple.
Rice
"To return more than one values, we need to use a data type that can store multiple values. At that point, it becomes a question of whether eating rice is eating one meal or hundreds of meals each consisting of a single grain of rice."
Find Index
Problem Description
Given a tuple tpl
the largest value is the value val
such that val >= tpl[i]
for all valid i
.
Similarly, the smallest value is the value val
such that val <= tpl[i]
for all valid i
.
Task
Write the function find_max_min(tpl)
to find and return the largest and smallest value in a tuple.
The largest value is the first element of the return value and the smallest value is the second element of the return value.
Assumptions
tpl
is a tuple of anything, with at least one element (i.e.,len(tpl) > 0
).val
is potentially anything but can always be compared via<
and>
with elements fromtpl
.
Restrictions
- You are NOT allowed to convert to other data types.
- You are NOT allowed to use the built-in
max
ormin
functions.
With proper decomposition, we can simply find the maximum and minimum separately.
Finding Maximum and Minimum
With a little bit thinking, we can realize that the two for-loops begin with the same for elem in tpl
.
So we can combine them into one.