Question

In: Computer Science

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 elem of list
## if i%j != 0:
## primes.append(i)
#
#
## FIXES: --------------------------
## = invalid syntax, variable i unknown, variable primes unknown
## can't apply 'len' to an int
## division by zero -> iterate through elems not indices
## -> iterate from 2 not 0
## forgot to return
## primes is empty list for n > 2
## n = 3 goes through loop once -> range to n+1 not n
## infinite loop -> append j not i
## -> list is getting modified as iterating over it!
## -> switch loops around
## n = 4 adds 4 -> need way to stop going once found a divisible num
## -> use a flag
## --------------------------
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 elem of list
if i%j != 0:
primes.append(i)

print(primes_list(2) )   
print(primes_list(15) )


(a) Debug the program by using Python Programming Language.

Solutions

Expert Solution

PYTHON CODE

def primes_list(n):
        
        # initialize primes list
        primes = []
        
        # go through each elem of primes list
        for i in range(2, n+1):
                #flag is used
                flag = 0
                # go through each of 2...i-1
                for j in range(2,i):
                    # check if divisible by elem of list
                    if i%j == 0:
                        #if it is divisible then change flag as 1
                        flag = 1
                        break
                if flag == 0:#if flag as 0, then append element in primes list
                    primes.append(i)
                        
        return primes

print(primes_list(2) )
print(primes_list(15) )

PYTHON CODE SCREENSHOT

OUTPUT SCREENSHOT


Related Solutions

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...
In Python Find the errors, debug the program, and then execute to show the output. def...
In Python Find the errors, debug the program, and then execute to show the output. def main():     Calories1 = input( "How many calories are in the first food?")     Calories2 = input( "How many calories are in the first food?")     showCalories(calories1, calories2)    def showCalories():     print('The total calories you ate today', format(calories1 + calories2,'.2f'))
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2:...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if n==2: return 1 if n==3: return 2 if n==4: return annoying_fibonacci(4-1)+annoying_fibonacci(4-2) if n==5: return annoying_fibonacci(5-1)+annoying_fibonacci(5-2) if...
An FIR system produced an output y[n] as given below for an input x[n] = {1,1,1,1},...
An FIR system produced an output y[n] as given below for an input x[n] = {1,1,1,1}, y[n] = {6,11,15,18,14,10,6,3,1}. Find the FIR system. Use bk, d(n-k) equation if applicable. Thanks!
create two examples for each of the vulnerabilities in the category below and possible fixes ....
create two examples for each of the vulnerabilities in the category below and possible fixes . Missing Encryption of Sensitive Data Execution with Unnecessary Privileges Incorrect Permission Assignment for Critical Resource
# RQ3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>>...
# RQ3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>> largest_factor(15) # factors are 1, 3, 5 5 >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40 40 """ "*** YOUR CODE HERE ***" # RQ4 # Write functions c, t, and f such that calling the with_if_statement and # calling the with_if_function do different things. # In particular, write the functions (c,t,f) so that calling with_if_statement function returns...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2:...
def annoying_valley(n): if n == 0: print() elif n == 1: print("*") elif n == 2: print("./") print("*") print(".\\") elif n == 3: print("../") print("./") print("*") print(".\\") print("..\\") elif n == 4: print(".../") annoying_valley(3) print("...\\") elif n == 5: print("..../") annoying_valley(4) print("....\\") elif n == 6: print("...../") annoying_valley(5) print(".....\\") else: print("." * (n - 1) + "/") annoying_valley(n - 1) print("." * (n - 1) + "\\") def annoying_int_sequence(n): if n == 0: return [] elif n == 1: return...
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L)...
Question 14: Find the running time function, T(n), of the program below. def prob14(L): if len(L) <= 1: return 0 output = 0 for x in L: for y in L: output += x*y for x in L: output += x left = L[ 0 : len(L)//2 ] right = L[ len(L)//2 : len(L) ] return output + prob15(left) + prob15(right) ​ ANSWER: T(n) = ... ***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive...
Problem 10 Write down the running time function, T(n), of the program below. def prob10(L): s...
Problem 10 Write down the running time function, T(n), of the program below. def prob10(L): s = 0 for x in L: for y in L: if x+y % 10 == 0: print(x, y, x+y) s = s + x*y return s ​ ANSWER: T(n) = ... ***Big-O, Omega, Theta complexity of functions, Running time equations of iterative functions & recursive functions,  Substitution method & Master theorem Please answer within these topics.***
Write a program to implement problem statement below: provide the menu for input N and number...
Write a program to implement problem statement below: provide the menu for input N and number of experiment M to calculate average time on M runs. randomly generated list. State your estimate on the BigO number of your algorithm/program logic. (we discussed in the class) Measure the performance of your program by given different N with randomly generated list with multiple experiment of Ns against time to draw the BigO graph (using excel) we discussed during the lecture. Lab-08-BigBiggerBiggtest.png ***...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT