Question

Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...

Please do it in Python

Write the simplest program that will demonstrate iteration vs recursion using the following guidelines -

  1. Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which
    1. Take the array and its size as input params and return a bool.
    2. Print out a message "Entering <function_name>" as the first statement of each function.
    3. Perform the code to test whether every element of the array is a Prime number.
    4. Print out a message "Leaving <function_name>" as the last statement before returning from the function.
    5. Remember - there will be nested loops for the iterative function and there can be no loops at all in the recursive function. You will need to define other helper functions to make the recursive method work.
    6. Remember - replace <function_name> with the actual name of each function.
    7. You can create secondary helper functions if you need, but only the primary helper functions can be invoked from 'main'.
    8. Hint - try to complete your iterative code first and then convert it piece by piece to the recursive code.
  2. In your main:
    1. Ask the user for the number of elements, not to exceed SORT_MAX_SIZE = 16 (validate input).
    2. Create an array based on the size input provided by the user.
    3. Get the input in a loop, validating that the integers input by the user are between 1 and 99, both inclusive
    4. Make a call to the primary iterative function passing the array and its size.
    5. If every member is a Prime, then the program should print out 'Prime Array using iteration', otherwise print out 'Not a Prime Array using iteration'.
    6. Then make a call to the primary recursive function passing the array and its size.
    7. If every member is a Prime, then the program should print out 'Prime Array using recursion', otherwise print out 'Not a Prime Array using recursion'.
    8. If your functions are coded correctly, both should come up with the same answer, except you should have lots more output statements using recursion.
    9. There is no sample output - you are allowed to provide user interactivity as you see fit but programs will be graded for clarity of interaction.
  3. You can use language native arrays - DO NOT USE VECTORS, COLLECTIONS, SETS, BAGS or any other data structures from your programming language.
  4. There will be only one code file in your submission.
  5. Remember to take multiple screenshots so that they are clearly readable without needing to zoom in.
  6. For documentation, include your name block as well pre/post and pseudocode for the functions only.
  7. Upload your code file and the screenshots in one zip file. Do not include anything else.

Grading:

  • 25 pts - EXE works from your code as explained above without needing any code change
  • 15 pts - for the main
  • 25 pts - the IsArrayPrimeIter function
  • 35 pts - the IsArrayPrimeRecur function

I understand most of this information is more suitable to C++ but our instructor wants us to modify it to do it in Python. As long as you fufill the parameters the best you can in Python and works that all I want. Thank you

Homework Answers

Answer #1

Program:

# function is used to check whether a given number or not using iteration

def isPrime(n): 
        if n <= 1: 
                return False
        for i in range(2, n): 
                if n % i == 0: 
                        return False; 
        return True
        
# function is used to check whether a given list has all prime numbers or not using iteration

def check_list_prime_iteration(arr,le):
    count=0
    for i in arr:
        if(isPrime(i)):
            count=count+1
    if(count==le):
        return True
    else:
        return False

# function is used to check whether a given list has all prime numbers or not using recusrion

def check_list_prime_recursion(arr,le):
    count=0
    for i in arr:
        if(isPrime_Rec(i)):
            count=count+1
    if(count==le):
        return True
    else:
        return False

# function is used to check whether a given number or not using recusrion

def isPrime_Rec(n, i = 2): 
    if (n <= 2): 
        return True if(n == 2) else False
    if (n % i == 0): 
        return False
    if (i * i > n): 
        return True 
    return isPrime_Rec(n, i + 1) 

# List to store values
l=[]
i=1
# input taking
n=int(input("Enter size of an Array"))
while(i<=n):
    input_value=int(input("Enter number"))
    if(input_value>=1 and input_value<=99):
        l.append(input_value)
    else:
        print("please enter valid input between 1 and 99")
        i=i-1
    i=i+1
# Final list after validation
print("Array List",l)

if check_list_prime_iteration(l,len(l)):
    print("Prime Array using iteration")
else:
    print("Not a Prime Array using iteration")
    
if check_list_prime_recursion(l,len(l)):
    print("Prime Array using recursion")
else:
    print("Not a Prime Array using recursion")

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Submission Question: Recursion with Trees – Depth First Search & Breadth First Search Write a Python...
Submission Question: Recursion with Trees – Depth First Search & Breadth First Search Write a Python program using the Python IDE based on recursion with trees that is both depth first and breadth first searches. The Python program to be developed will demonstrate the use of both depth-first (DFS) and breadth-first (BFS) searches. A tree node structure will be developed that will be used both for DFS and BFS searches. The program will apply recursion both for the DFS and...
5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code...
5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code from your pseudocode. Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. For example given 3 the algorithm will print 14 (because 1 + 4 + 9 =14) You can use multiplication denoted as * in your solution and you do not have to define it (For example. 3*3=9) For...
By using Python code answer the following parts: A) Write a program to ask the user...
By using Python code answer the following parts: A) Write a program to ask the user to input their name and then output the type and length of the input. B) Write a program to output the last 11 letters of 'Introduction to Indiana'. C) Write a program to output the 3rd to the 11th letters of 'Introduction to Indiana'. D) Write a program to ask the user to input a float number and output the rounded value of it...
ALL IN PYTHON PLEASE Problem 4 Write a program to compute the area of a circle...
ALL IN PYTHON PLEASE Problem 4 Write a program to compute the area of a circle some 'n' times. You must accept n and r from the user. Area is calculated using (22/7.0)*r*r - where r is the radius. Implement using value-returning functions. Hint: create a function to calculate area taking r as a parameter. In the main() function, ask for n and create a loop where you input r and invoke the area function n times. Rubric: Correct use...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are...
Please follow ALL the instructions and solve it by C++. Please and thank you! There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print...
Write a python code to print (any given file) file as a csv format by using...
Write a python code to print (any given file) file as a csv format by using MRJob only. code must be able to run stand-alone MRJob application. For submission to your work: Your final hand should be a single file name BDM.py that takes exactly one arguments for the input path. output will be handled through redirection. Sample run: python BDM.py sample.csv > output.csv
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT