In: Computer Science
Try to debug it!
########################################
##def rev_list_buggy(L):
## """
## input: L, a list
## Modifies L such that its elements are in reverse order
## returns: nothing
## """
## for i in range(len(L)):
## j = len(L) - i
## L[i] = temp
## L[i] = L[j]
## L[j] = L[i]
#
## FIXES: --------------------------
## temp unknown
## list index out of range -> sub 1 to j
## get same list back -> iterate only over half
## --------------------------
def rev_list_buggy(L):
"""
## input: L, a list
## Modifies L such that its elements are in reverse order
## returns: nothing
## """
for i in range(len(L)):
j = len(L) - i
L[i] = temp
L[i] = L[j]
L[j] = L[i]
pdb.set_trace()
L = [1,2,3,4]
rev_list(L)
print(L)
(a) Debug the program by using the Python programming language.
:DEBUGGED CODE:
CODE:
def rev_list(L):
#running a loop only half the times the length of the list
for i in range(int(len(L)/2)):
#get j from the end of the list
j = len(L) - i - 1
#storing the ith
temp = L[i]
#stores the jth element at the ith position
L[i] = L[j]
#stores the ith element or the temp at the jth position
L[j] = temp
#so basically we are swapping elemnts from either end of the
#list once
#List
L = [1,2,3,4]
#calling the function
rev_list(L)
#printing the reversed list
print(L)
__________________________________________
CODE IMAGES AND OUTPUT:
_________________________________________
Feel free to ask any questions in the comments section
Thank You!