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
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...
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...
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...
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
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the...
In Python: Problem 4. Write a program [call it minmax.py] that accepts three integers in the range [0-100] as input from the user. Your program should then determine and print the smallest and largest integers in the values entered by the user using comparison operators *not* using predefined min or max Python functions. For instance: If the user input is: 36 5 99 your program should give as output: min = 5 max = 99 If the user input is:...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT