Skip to content

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 from tpl (i.e., tpl[i] == val is not an error for all valid values of i).

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

1
2
3
4
5
def find_first(tpl, val):
  for idx in range(len(tpl)): # enumerate from smallest to largest
    if tpl[idx] == val:
      return idx
  return -1

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 from tpl.

Restrictions

  • You are NOT allowed to convert to other data types.
  • You are NOT allowed to use the built-in max or min functions.

With proper decomposition, we can simply find the maximum and minimum separately.

Finding Maximum and Minimum

def find_max_min(tpl):
  # find maximum
  max_val = tpl[0]
  for elem in tpl:
    if elem > max_val:
      max_val = elem
  # find minimum
  min_val = tpl[0]
  for elem in tpl:
    if elem < min_val:
      min_val = elem
  return (max_val, min_val)

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.

Finding Maximum and Minimum

1
2
3
4
5
6
7
8
def find_max_min(tpl):
  min_val, max_val = tpl[0], tpl[0]
  for elem in tpl:
    if elem > max_val:
      max_val = elem
    if elem < min_val:
      min_val = elem
  return (max_val, min_val)