GrPA 4 Function Basics

GrPA 4 Function Basics


Week 4 GrPA


GrPA 4 Function Basics 👨‍💻

Instructions

Question ❓

Implement the below functions as per the docstrings.

Python Code 🐍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def swap_halves(items):
    ...

def swap_at_index(items,k):
    ...

def rotate_k(items,k=1):
    ...

def first_and_last_index(items,elem):
    ...

def reverse_first_and_last_halves(items):
    ...

Python Code Solution ✅

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def swap_halves(items):
    mid = len(items) // 2
    return items[mid:] + items[:mid]

def swap_at_index(items, k):
    return items[k+1:] + items[:k+1]

def rotate_k(items, k=1):
    n = len(items)
    k = k % n
    return items[-k:] + items[:-k]

def first_and_last_index(items, elem):
    return (items.index(elem), len(items) - 1 - items[::-1].index(elem))

def reverse_first_and_last_halves(items):
    n = len(items)
    mid = n // 2
    items[:mid] = items[:mid][::-1]
    items[mid:] = items[mid:][::-1]