Tuple Functions

import introcs

The purpose of this module is to allow students to work with tuples without having to understand method calls. It has the side effect of unifying tuple functionality with string functionality (i.e. adding find functions and handling index ranges in the same way).

count_tup

introcs.count_tup(tupl, value, start=None, end=None)

Counts the number of times value occurs in tupl[start:end].

Optional arguments start and end are interpreted as in slice notation.

Parameters
  • tupl (tuple) – The tuple to search

  • value (any) – The value to count

  • start (int) – The start of the search range

  • end (int) – The end of the search range

Returns

The number of times value occurs in tupl[start:end].

Return type

int

find_tup

introcs.find_tup(tupl, value, start=None, end=None)

Finds the lowest index of value within tupl in the range [start, end].

Optional arguments start and end are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slice tupl[start:end]. The function returns -1 if value is not found.

Note: The find_tup() function should be used only if you need to know the position of value. To check if value is in the tuple, use the in operator:

>>>
>>> 1 in (1,2,3)
True
Parameters
  • tupl (tuple) – The tuple to search

  • value (any) – The value to search for

  • start (int) – The start of the search range

  • end (int) – The end of the search range

Returns

The lowest index of value within tupl in the range [start, end].

Return type

int

rfind_tup

introcs.rfind_tup(tupl, value, start=None, end=None)

Finds the highest index of value within tupl in the range [start, end].

Optional arguments start and end are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slice tupl[start:end]. The function returns -1 if value is not found.

Parameters
  • tupl (tuple) – The tuple to search

  • value (any) – The value to search for

  • start (int) – The start of the search range

  • end (int) – The end of the search range

Returns

The highest index of value within tupl in the range [start, end].

Return type

int

index_tup

introcs.index_tup(tupl, value, start=None, end=None)

Finds the lowest index of value within tupl in the range [start, end].

Optional arguments start and end are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slice tupl[start:end].

This function is like find_tup(), except that it raises a ValueError when the value is not found.

Parameters
  • tupl (tuple) – The tuple to search

  • value (any) – The value to search for

  • start (int) – The start of the search range

  • end (int) – The end of the search range

Returns

The lowest index of value within tupl in the range [start, end].

Return type

int

rindex_tup

introcs.rindex_tup(tupl, value, start=None, end=None)

Finds the highest index of value within tupl in the range [start, end].

Optional arguments start and end are interpreted as in slice notation. However, the index returned is relative to the tuple and not the slice tupl[start:end].

This function is like rfind_tup(), except that it raises a ValueError when the value is not found.

Parameters
  • tupl (tuple) – The tuple to search

  • value (any) – The value to search for

  • start (int) – The start of the search range

  • end (int) – The end of the search range

Returns

The highest index of value within tupl in the range [start, end].

Return type

int

replace_tup

introcs.replace_tup(tupl, old, new, count=- 1)

Creates a copy of tupl with all occurrences of value old replaced by new.

Objects are replaced by value equality, not id equality (i.e. == not is). If the optional argument count is given, only the first count occurrences are replaced.

Parameters
  • tupl (tuple) – The tuple to copy

  • old (any) – The old value to replace

  • new (any) – The new value to replace with

  • count (int) – The number of occurrences to replace

Returns

A copy of tupl with all occurrences of value old replaced by new.

Return type

tuple

Contents: