Write a function index(elem, seq) that takes as inputs an element elem and a sequence seq and returns the index of the first occurrence of elem in seq. The sequence seq can be either a list or a string. If seq is a string, elem will be a single-character string; if seq is a list, elem can be any value. If elem is not an element of seq, the function should return the length of the sequence. Dont forget that the index of the first element in a sequence is 0. Here are some examples: >>> index(5, [4, 10, 5, 3, 7, 5]) solution

Respuesta :

Answer:

def index(elem, seq):

   for i in range(len(seq)):

       if elem == seq[i]:

           return i

   

   return len(seq)

print(index(5, [4, 10, 8, 5, 3, 5]))

Explanation:

Create a function named index that takes elem and seq as parameters

Create a for loop that iterates through the seq. If elem is equal to the item in the seq, return the i, index of the item.

If the elem is not found in the seq, this means nothing will be returned in the loop, just return the length of the seq, len(seq)

Call the function with given parameters and print the result

Note: Since 5 is in the list in the example, the index of the 5 which is 3 will be returned

In this exercise we have to use the knowledge of computational language in Python, so we have that code is:

It can be found in the attached image.

What is an index in Python?

An index, in your example, refers to a position within an ordered list. Python strings can be thought of as lists of characters; each character is given an index from zero  to the length minus one.

So, to make it easier, the code in Python can be found below:

def index(elem, seq):

  for i in range(len(seq)):

      if elem == seq[i]:

          return i

  return len(seq)

print(index(5, [4, 10, 8, 5, 3, 5]))

See more about python at brainly.com/question/26104476

Ver imagen lhmarianateixeira