Question

In: Computer Science

Try to debug it! ######################################## ##def rev_list_buggy(L): ## """ ## input: L, a list ## Modifies...

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.

Solutions

Expert Solution

: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!


Related Solutions

Try to debug it! (fixes needed are explained below) ######################################## ##def primes_list_buggy(n): ## """ ## input:...
Try to debug it! (fixes needed are explained below) ######################################## ##def primes_list_buggy(n): ## """ ## input: n an integer > 1 ## returns: list of all the primes up to and including n ## """ ## # initialize primes list ## if i == 2: ## primes.append(2) ## # go through each elem of primes list ## for i in range(len(primes)): ## # go through each of 2...n ## for j in range(len(n)): ## # check if not divisible by...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
12. The reciprocal search problem is: input: a list L of n floating point numbers output:...
12. The reciprocal search problem is: input: a list L of n floating point numbers output: two elements a, b L such that a*b=1, or None if no such a, b exist Design an algorithm for this problem using the greedy pattern. Write pseudocode for your algorithm below. If you write multiple drafts, circle your final draft. Hint: I expect your algorithm to be relatively simple and to have time complexity O(n2). 13. Perform a chronological step count to derive...
def change_type(info: List[list]) -> None: """ Modify info to a float if and only if it...
def change_type(info: List[list]) -> None: """ Modify info to a float if and only if it represents a number that is not a whole number(a float), and convert info to an int if and only if it represents a whole number, keep everythingelse as a string. >>> i = [['apple', '888', 'School', '111.1']] >>> change_type(i) >>> i   [['apple', 888, 'School', '111.1']] Please help! Write as little code as possible! Please only use if statements, nested loop and lists to solve...
message = 'youcannotdecodemyciphertexttoday' def transposition_cipher_encode(plain_text, key): # the input, key should be a permutation of integers...
message = 'youcannotdecodemyciphertexttoday' def transposition_cipher_encode(plain_text, key): # the input, key should be a permutation of integers 0 to some number # your code here return need code in PYTHON
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's...
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's temperatures from temps_now are warmer than past temperatures in temps_then. The resulting list should contain "Warmer" at the indexes where this year's temperature is warmer, and "Not Warmer" at the indexes where the past year was warmer, or there is a tie. Precondition: len(temps_then) == len(temps_now) >>> warmer_year([10], [11]) ['Warmer'] >>> warmer_year([26, 27, 27, 28], [25, 28, 27, 30]) ['Not Warmer', 'Warmer', 'Not Warmer',...
The production function has two input, labor (L) and capital (K). The price for L and...
The production function has two input, labor (L) and capital (K). The price for L and K are respectively W and V. q = L + K a linear production function q = min{aK, bL} which is a Leontief production function 1.Calculate the marginal rate of substitution. 2.Calculate the elasticity of the marginal rate of substitution. 3.Drive the long run cost function that is a function of input prices and quantity produced.
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the ignore character as well as any blank lines. """ return ['a', 'z'] How would I code this in Python?
String inputStr; double number; try {       inputStr = JOptionPane.showInputDialog(null,             "Please input a number.");      ...
String inputStr; double number; try {       inputStr = JOptionPane.showInputDialog(null,             "Please input a number.");       if (inputStr.equals(""))             throw new IllegalArgumentException("Please enter a number");       number = Double.parseDouble(inputStr);       JOptionPane.showMessageDialog(null,             "The square of the number is " + number * number); } catch (NumberFormatException e) {       JOptionPane.showMessageDialog(null,             "Please enter a valid number to square"); } catch (IllegalArgumentException e) {       JOptionPane.showMessageDialog(null,             e.getMessage()); } finally {       System.exit(0); } 1) First,Why must the two catch blocks...
Write code to read a list of song names and durations from input. Input first receives...
Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit. #include <iostream> #include <string> #include <vector> using namespace std; class Song { public: void SetNameAndDuration(string songName, int songDuration) { name = songName; duration = songDuration; } void PrintSong() const { cout << name << " - " << duration << endl; } string GetName() const { return name;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT