Registry Synced

Jan 2026 - Python - Week 6 - Collections 2 - Graded

636 words
3 min read

Collections 2 - Graded

Course: Jan 2026 - Python
Collections 2 - Graded
Submission deadline has passed for this assignment
Due Mar 25, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
3/3 Passed
Submitted on Mar 25, 2026 at 2:33 PM IST
Private Tests
3/3 Passed
Submitted on Mar 25, 2026 at 2:33 PM IST

**Change in eligibility criteria to write oppe2 exam: A5>=40/100 AND A6>=40/100 AND A7>=40/100 AND A8>=40/100. and becoming eligible to give the end term exam.


Swap Alternate Elements in a Tuple**
Write a function to swap every alternate of adjacent elements in the tuple.
Assume the length of the tuple is even.
Examples
python
>>> swap_alternate_elements((1, 2, 3, 4, 5, 6))
(2, 1, 4, 3, 6, 5)
>>> swap_alternate_elements(('a', 'b', 'c', 'd'))
('b', 'a', 'd', 'c')
Template Code(Click to Expand)

def swap_alternate_elements(t):
    '''Swap every alternate of adjacent elements in the tuple.

    Args:
        t (tuple): A tuple of even length.

    Returns:
        tuple: A new tuple with alternate elements swapped.

    Examples:
    >>> swap_alternate_elements((1, 2, 3, 4, 5, 6))
    (2, 1, 4, 3, 6, 5)
    >>> swap_alternate_elements(('a', 'b', 'c', 'd'))
    ('b', 'a', 'd', 'c')
    '''
    ...

NOTE: You can use the below tools for working out and debugging. Click to open them in new tab.

Public Tests ( 3/3 )

Case 1

Input:
text
is_equal(
    swap_alternate_elements((1, 2, 3, 4, 5, 6)),
    (2, 1, 4, 3, 6, 5)
)
Expected Output:
text
(2, 1, 4, 3, 6, 5)
Actual Output:
text
(2, 1, 4, 3, 6, 5)

Case 2

Input:
text
is_equal(
    swap_alternate_elements(('a', 'b', 'c', 'd')),
    ('b', 'a', 'd', 'c')
)
Expected Output:
text
('b', 'a', 'd', 'c')
Actual Output:
text
('b', 'a', 'd', 'c')

Case 3

Input:
text
is_equal(
    swap_alternate_elements(tuple("alpha1")),
    tuple("lahp1a")
)
Expected Output:
text
('l', 'a', 'h', 'p', '1', 'a')
Actual Output:
text
('l', 'a', 'h', 'p', '1', 'a')

Private Tests ( 3/3 )

Case 1

Input:
text
is_equal(
    swap_alternate_elements((5, 6, 7, 8, 9, 10)),
    (6, 5, 8, 7, 10, 9)
)
Expected Output:
text
(6, 5, 8, 7, 10, 9)
Actual Output:
text
(6, 5, 8, 7, 10, 9)

Case 2

Input:
text
is_equal(
    swap_alternate_elements(('apple', 'ball')),
    ('ball', 'apple')
)
Expected Output:
text
('ball', 'apple')
Actual Output:
text
('ball', 'apple')

Case 3

Input:
text
is_equal(
    swap_alternate_elements(tuple("llama3.3")),
    tuple("llma3a3.")
)
Expected Output:
text
('l', 'l', 'm', 'a', '3', 'a', '3', '.')
Actual Output:
text
('l', 'l', 'm', 'a', '3', 'a', '3', '.')

💻 IITM Official Solution

python

def swap_alternate_elements(t):
    '''Swap every alternate of adjacent elements in the tuple.

    Args:
        t (tuple): A tuple of even length.

    Returns:
        tuple: A new tuple with alternate elements swapped.

    Examples:
    >>> swap_alternate_elements((1, 2, 3, 4, 5, 6))
    (2, 1, 4, 3, 6, 5)
    >>> swap_alternate_elements(('a', 'b', 'c', 'd'))
    ('b', 'a', 'd', 'c')
    '''


    lst_t = list(t)
    for i in range(0, len(lst_t) - 1, 2):
        lst_t[i], lst_t[i+1] = lst_t[i+1], lst_t[i]
    return tuple(lst_t)


💻 My Submitted Code

python

def swap_alternate_elements(t: tuple) -> tuple:
    '''Swap every alternate of adjacent elements in the tuple.

    Args:
        t (tuple): A tuple of even length.

    Returns:
        tuple: A new tuple with alternate elements swapped.

    Examples:
    >>> swap_alternate_elements((1, 2, 3, 4, 5, 6))
    (2, 1, 4, 3, 6, 5)
    >>> swap_alternate_elements(('a', 'b', 'c', 'd'))
    ('b', 'a', 'd', 'c')
    '''
    ...

    lst = list(t)
    lst[0::2], lst[1::2] = lst[1::2], lst[0::2]

    return tuple(lst)

Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.