Question: Please do in Python Write list functions for items a to j that carry out the following tasks for a list of integers. a. Swap
Please do in Python
Write list functions for items a to j that carry out the following tasks for a list of integers.
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,
1 4 9 16 25
would be transformed into
25 1 4 9 16
.
c. Replace all even elements with 0 (zeroes)
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 element to the front, otherwise preserving the order of the elements.
g. Return the second largest element in the list.
h. Return true if the list is currently sorted in increasing order.
i. Return true if the list contains two adjacent duplicate elements.
j. Return true if the list contains duplicate elements (which need not be adjacent).
The main() function to test each of the functions is included here for you. You must use the same function names
that is provide for you below. Using other function names will result in a zero for this program.
# Program to test functions a to j.
#
# Define constant variables.
ONE_TEN = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def main() :
print("The original data for all functions is: ", ONE_TEN)
#a. Demonstrate swapping the first and last element.
data = list(ONE_TEN)
swapFirstLast(data)
print("After swapping first and last: ", data)
#b. Demonstrate shifting to the right.
data = list(ONE_TEN)
shiftRight(data)
print("After shifting right: ", data)
#c. Demonstrate replacing even elements with zero.
data = list(ONE_TEN)
replaceEven(data)
print("After replacing even elements: ", data)
#d. Demonstrate replacing values with the larger of their neighbors.
data = list(ONE_TEN)
replaceNeighbors(data)
print("After replacing with neighbors: ", data)
#e. Demonstrate removing the middle element.
data = list(ONE_TEN)
removeMiddle(data)
print("After removing the middle element(s): ", data)
#f. Demonstrate moving even elements to the front of the list.
data = list(ONE_TEN)
evenToFront(data)
print("After moving even elements: ", data)
#g. Demonstrate finding the second largest value.
print("The second largest value is: ", secondLargest(ONE_TEN))
#h. Demonstrate testing if the list is in increasing order.
print("The list is in increasing order: ", isIncreasing(ONE_TEN))
#i. Demonstrate testing if the list contains adjacent duplicates.
print("The list has adjacent duplicates: ", hasAdjacentDuplicate(ONE_TEN))
#j. Demonstrate testing if the list contains duplicates.
print("The list has duplicates: ", hasDuplicate(ONE_TEN))
main()
The output should look like this:
Run 1
>>>
====== RESTART: E:/IVC/CS10 Python/Homework/HW4 Files/HW4_sp2018_PS1.py ======
The original data for all functions is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
After swapping first and last:
[10, 2, 3, 4, 5, 6, 7, 8, 9, 1]
After shifting right:
[10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After replacing even elements:
[1, 0, 3, 0, 5, 0, 7, 0, 9, 0]
After replacing with neighbors:
[1, 3, 4, 5, 6, 7, 8, 9, 10, 10]
After removing the middle element(s):
[1, 2, 3, 4, 7, 8, 9, 10]
After moving even elements:
[2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
The second largest value is:
9
The list is in increasing order:
True
The list has adjacent duplicates:
False
The list has duplicates:
False
>>>
Run 2
======= RESTART: E:/IVC/CS10 Python/Homework/HW4 Files/HW4Fa2018_Q1.py =======
The original data for all functions is:
[12, 20, 10, 14, 54, 16, 75, 38, 79, 103]
After swapping first and last:
[103, 20, 10, 14, 54, 16, 75, 38, 79, 12]
After shifting right:
[103, 12, 20, 10, 14, 54, 16, 75, 38, 79]
After replacing even elements:
[0, 0, 0, 0, 0, 0, 75, 0, 79, 103]
After replacing with neighbors:
[12, 12, 14, 54, 54, 75, 75, 79, 103, 103]
After removing the middle element(s):
[12, 20, 10, 14, 75, 38, 79, 103]
After moving even elements:
[12, 20, 10, 14, 54, 16, 38, 75, 79, 103]
The second largest value is:
79
The list is in increasing order:
False
The list has adjacent duplicates:
False
The list has duplicates:
False
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
