Ask Question

Write a function called first_last that takes a single parameter, seq, a sequence. first_last should return a tuple of length 2, where the first item in the tuple is the first item in seq, and the second item in tuple is the last item in seq. If seq is empty, the function should return an empty tuple. If seq has only one element, the function should return a tuple containing just that element.

+1
Answers (1)
  1. 7 March, 22:04
    0
    The Python code with the function is given below. Testing and output gives the results of certain chosen parameters for the program

    Explanation:

    def first_last (seq):

    if (len (seq) = = 0):

    return ()

    elif (len (seq) = = 1):

    return (seq[0],)

    else:

    return (seq[0], seq[len (seq) - 1])

    #Testing

    print (first_last ([]))

    print (first_last ([1]))

    print (first_last ([1,2,3,4,5]))

    # Output

    ()

    (1,)

    (1, 5)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function called first_last that takes a single parameter, seq, a sequence. first_last should return a tuple of length 2, where the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers