Write an Oz program (having an Oz recursive function/procedure) which takes in the input a list, say L, and two positive integers, M and N. The function should return another list L’, which represents the sub-list of L containing all the elements from index M to index N in reverse order. For example, if the input list is [a 2 g 5 4 k] and M is 2 and N is 5, then the output list should be [4 5 g 2]. Try to cover as many margin cases as possible (e.g., M > N).
#Defining a function sub_reverse_list which take list
#as input and M,N two value
def sub_reverse_list(L,M,N):
#declaring a result list to hold the sub list result from M to N
result_lst = []
#checking the condition M index should be less than N and length
#of list should be greater than N
if(M<N and N < len(L)):
#looping from M to N in the list and appending
# the value in the result list
for i in range(M-1,N):
#appending the each element of the sub list
result_lst.append(L[i])
#reverse the list
result_lst.reverse()
#return the result list
return result_lst
print(["a",2 ,"g", 5, 4, "k"],"M=2 and N=5")
print("Output:")
print(sub_reverse_list(["a",2 ,"g", 5, 4, "k"],2,5))
output:
Get Answers For Free
Most questions answered within 1 hours.