AnKiWoong 2020. 4. 10. 20:26
반응형

Quiz>

Your mission here is to create a function that gets a tuple and returns a tuple with 3 elements - the first, 

third and second element from the last for the given array.

 

Input:

A tuple, at least 3 elements long.

 

Output:

A tuple.

 

Example:

easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (1, 3, 7)
easy_unpack((1, 1, 1, 1)) == (1, 1, 1)
easy_unpack((6, 3, 7)) == (6, 7, 3)

 

def easy_unpack(elements: tuple) -> tuple:
    """
        returns a tuple with 3 elements - first, third and second to the last
    """
    # your code here
    return ()

if __name__ == '__main__':
    print('Examples:')
    print(easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)))
    
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (1, 3, 7)
    assert easy_unpack((1, 1, 1, 1)) == (1, 1, 1)
    assert easy_unpack((6, 3, 7)) == (6, 7, 3)
    print('Done! Go Check!')

 

Solve>

1. 튜플형식으로 elements를 받으면 인덱싱 번호를 통하여 리턴한다.

   리턴할 대상은 첫번째 - 0 / 세번째 - 2 / 마지막에서 두번째 - -2 를 리턴한다.

def easy_unpack(elements: tuple):
    return elements[0], elements[2], elements[-2]

 

Code>

def easy_unpack(elements: tuple):
    return elements[0], elements[2], elements[-2]

 

Example>

if __name__ == '__main__':
    print('Examples:')
    print(easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)))

 

Result>

Examples:
(1, 3, 7)

반응형