Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a main() for the test portions of the program. Use the following forma

ID: 3709693 • Letter: I

Question

Implement a main() for the test portions of the program. Use the following format for implementing the function main(). if name "main" main( In Python P6.4 Write list functions that carry out the following tasks for a list of integers. For each function, provide a test program. a. Swap the first and last elements in the list b. Shift all elements by one to the right and move the last element into the first position. For example, 149 16 25 would be transformed into 25 149 16 c. Replace all even elements with d. Replace each element except the first and last by the larger of its two neighbors e. Remove the middle element if the list length is odd, or the middle two elements if the length is even. f. Move all even elements to the front, otherwise preserving the order of the elements

Explanation / Answer

Solution:

code:

import lists


def main():
    ''' test list methods implemented in lists.py '''
    a = [1, 0, 0, 3, 4, 6, 7]
    b = [1, 0, 0, 3, 4, 6, 7]
    c = [1, 4, 5, 3, 4, 6, 7]
    d = [1, 4, 5, 8, 4, 6, 7]
    e = [1, 4, 5, 3, 4, 6, 7]
    f = [1, 4, 5, 8, 10, 6, 7]
    g = [1, 4, 5, 8, 10, 6, 7]
    h = [1, 4, 5, 8, 11, 6, 7]
    i = [1, 4, 5, 3, 4, 6, 7]
    j = [1, 0, 0, 3, 4, 6, 7]
    print(lists.swap(a))
    print(lists.shift_to_right(b))
    print(lists.replace_with_zero(c))
    print(lists.replace_with_large(d))
    print(lists.remove_middle(e))
    print(lists.move_front(f))
    print(lists.second_largest(g))
    print(lists.is_sorted(h))
    print(lists.contains_adjacent_duplicate(i))
    print(lists.contains_duplicate(j))

main()
--------------------------------------------------------
//lists.py
import copy


def swap(list):
    # swap the last for the first element
    first_tem = list[0]
    last = len(list) - 1
    list[0] = list[last]
    list[last] = first_tem
    return list


def shift_to_right(list):
    # shift elements to the right
    # last element is the first now
    last_item = list.pop()
    list.insert(0, last_item)

    return list


def replace_with_zero(list):
    # replace even elements with zero
    for i in range(len(list)):
        if i % 2 == 0:
            list[i] = 0
    return list


def replace_with_large(list):
    # replace elements to with the greatest between its neighbors
    # only first and last keep heir location
    i = 0
    while i < len(list):
        if i == 0 or i == len(list) - 1:
            i += 1
            continue
        else:
            list[i] = max(list[i], list[i - 1], list[i + 1])
        i += 1

    return list


def remove_middle(list):
    # remove the middle element when the list is odd
    # remove two middle elements when the list is even
    length = len(list)
    if length % 2 == 0:
        half = int(length / 2)
        list.pop(half - 1) # pop the element in the middle
        list.pop(half - 1) # pop second element
    else:
        half = length // 2 + length % 2
        list.pop(half)
    return list


def move_front(list):
    # move to the front even elements
    odd_list = []
    even_list = []
    for i in range(len(list)):
        if list[i] % 2 == 0:
            even_list.append(list[i])
        else:
            odd_list.append(list[i])

    return even_list + odd_list


def second_largest(list):
    # return the second largest element
    list.remove(max(list)) # remove the first largest

    return max(list)


def is_sorted(list):
    # check if the list is sorted
    list_copy = copy.copy(list) # create a copy of the list
    list.sort()
    return list_copy == list

def contains_adjacent_duplicate(list):
    # check if two adjacents number are equal
    i = 0
    ad_duplicate = False
    while i < len(list) - 1:
        if list[i] == list[i + 1]:
            ad_duplicate = True
            break
        i = i + 1

    return ad_duplicate


def contains_duplicate(list):
    # check if any element is duplicated
    duplicates = False
    for i in range(len(list)):
        if list.count(list[i]) > 1:
            duplicates = True
            break

    return duplicates

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote