Write a function where the user inputs two lists, with the argument of list1, li
ID: 3771463 • Letter: W
Question
Write a function where the user inputs two lists, with the argument of list1, list2, range1 (r1), range2 (r2). This function takes as arguments list1, list2, r1, and r2 that removes items from list1 in the slice r1:r2 appends them onto list2 in reverse order and returns the resulting list. For instance, this program receives input and create a list of list1 = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9] for the first list. And also create a list of list2 = [ 100, 200 ]. And you Wish to add the numbers in the index range 4:7 of list1 to list2 in reverse order. So that result of this new list = [100, 200, 7, 6, 5]. Call this function inside the a loop, so that the user can continue the test this feature.
Explanation / Answer
def transform(list1,list2,r1,r2):
sliced = list1[r1:r2]
sliced.reverse()
return list2+sliced
#Enter numbers with spaces
s = raw_input()
list1 = map(int, s.split())
s = raw_input()
list2 = map(int, s.split())
#If above input taking is not working then do following
#s = input()
#list1 = s.split(" ")
#s = input()
#list2 = s.split(" ")
print transform(list1,list2,4,7)
'''
1 2 3 4 5 6 7 8 9 <--input
100 200 <--input
[100, 200, 7, 6, 5] <--output
'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.