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
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...
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 ***...
Try to make it as simple as you can and explain as much as it needed....
Try to make it as simple as you can and explain as much as it needed. What is Trusted Third Party (TTP)? What are the problems with TTP? (3 points) Ans: Using Caesar cipher algorithm and key value = 4, encrypt the plain text “Network Security”. Show your work.          (3 points) Ans: Let k be the encipherment key for a Caesar cipher. The decipherment key differs; it is 26 - k. One of the characteristics of a public key system...
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 seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n...
def seq3np1(n): """ Print the 3n+1 sequence from n, terminating when it reaches 1. args: n (int) starting value for 3n+1 sequence return: None """ while(n != 1): print(n) if(n % 2) == 0: # n is even n = n // 2 else: # n is odd n = n * 3 + 1 print(n) # the last print is 1 def main(): seq3np1(3) main() Using the provided code, alter the function as follows: First, delete the print statements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT