Given a variable t that is associated with a tuple whose elements are numbers, write some statements that use a while loop to count the number of times the first element of the tuple appears in the rest of the tuple, and associate that number with the variable repeats. Thus if the tuple contains (1,6,5,7,1,3,4,1), then repeats would be assigned the value 2 because after the first "1" there are two more "1"s.

Respuesta :

Answer:

The solution code is written in Python:

  1. tuple = (1,6,5,7,1,3,4,1)
  2. repeats = 0
  3. first = tuple[0]
  4. index = 1
  5. while index < len(tuple):
  6.    if(first == tuple[index]):
  7.        repeats += 1
  8.    index = index + 1
  9. print(repeats)

Explanation:

Firstly, we create a tuple that contains (1,6,5,7,1,3,4,1)  (Line 1).

Next, declare a variable, repeats and initialize it with zero (Line 3).

Next, we use index zero to get the first element value from tuple and assign it to variable first (Line 4).  

Then we are ready to use while loop to traverse through the tuple (Line 7). Please note the initial index is set at 1 instead of zero as we want to start from second element of the tuple to find if there is any number is equal to the first element (Line 8). If so, we add one to variable repeats (Line 9).

After completion of loop, we shall get the repeats with value of 2 (Line 13).