Ask Question
11 March, 02:33

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.

+5
Answers (1)
  1. 11 March, 03:11
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers