a,b, and c are already done, Write list functions that carry out the following t
ID: 3776233 • Letter: A
Question
a,b, and c are already done,
Write list functions that carry out the following tasks for a list of integers in Intro to Python. For each function, provide a test program. Make sure to write a main 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. 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. 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).
Explanation / Answer
// Be careful about indentation while copying this code to editor for execution
def changeMe(mylist):
for index in range(len(mylist)):
if (index == 0 or index == len(mylist)-1):
continue
else :
if(mylist[index-1]>mylist[index+1]):
mylist[index] = mylist[index-1]
else:
mylist[index] = mylist[index+1]
def changeMe_e(mylist):
x = len(mylist)
t = x/2-1
if x % 2 == 0 :
mylist.pop(t)
mylist.pop(t-1)
else :
mylist.pop(t+1)
def changeMe_f(mylist):
for index in range(len(mylist)):
if(mylist[index] % 2 != 0):
continue
for indexl in range(index,-1,-1):
if(mylist[indexl] % 2 != 0 ):
temp = mylist[indexl]
mylist[indexl] = mylist[index]
mylist[index] = temp
index = index - 1
def changeMe_g(mylist):
first = mylist[0]
second = mylist[0]
for index in range(1,len(mylist)):
if(mylist[index] > first):
second = first
first=mylist[index]
if(mylist[index]<first and mylist[index]>second):
second = mylist[index]
return second
def changeMe_h(mylist):
for index in range(1,len(mylist)-1):
if(mylist[index+1] < mylist[index]):
return False
return True
def changeMe_i(mylist):
for index in range(1,len(mylist)-1):
if(mylist[index+1] == mylist[index]):
return True
return False
def changeMe_j(mylist):
for index in range(len(mylist)):
for indexj in range(len(mylist)):
if((index != indexj) and (mylist[index] == mylist[indexj])):
return True
return False
print "solution to d"
list2 = [1, 2, 1, 4, 5 ]
changeMe(list2)
print list2
print "solution to e"
list2 = [1, 2, 1, 4]
print list2
changeMe_e(list2)
print list2
list2.append(3)
list2.append(5)
list2.append(6)
print list2
changeMe_e(list2)
print list2
print "solution to f"
list2 = [1, 2, 1, 4]
print list2
changeMe_f(list2)
print list2
list2.append(3)
list2.append(5)
list2.append(6)
print list2
changeMe_f(list2)
print list2
print "solution to g"
list2 = [1, 2, 1, 4, 43, 90, 66, 78]
print changeMe_g(list2)
print "solution to h"
list2 = [1, 2, 3, 4, 3, 4]
print list2
print changeMe_h(list2)
print "solution to i"
list2 = [1, 2, 3, 4, 3, 4, 4]
print list2
print changeMe_i(list2)
print list2
print "solution to j"
list2 = [1, 2, 3, 4, 1]
print list2
print changeMe_j(list2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.