Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Lab # 1 Requirments Lab #1 Requirements. Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - 1. Write two primary helper functions - one iterative...

1 answer below »
Lab # 1 Requirments
Lab #1 Requirements.
Write the simplest program that will demonstrate iteration vs recursion using the
following guidelines -
1. Write two primary helper functions - one iterative (IsA
ayPrimeIter) and one
ecursive (IsA
ayPrimeRecur) - each of which
1. Take an a
ay and its size as input params and return a bool such
that
â–  'true' ==> a
ay and all elements are prime,
â–  'false' ==> at least one element in a
ay is not prime, so
a
ay is not prime.
2. Print out a message "Entering " as the first
statement of each function.
3. Perform the code to test whether every element of the a
ay is a
Prime number.
4. Print out a message "Leaving " 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 one other helper function (IsPrimeRecur) for the
ecursion which should also not contain any loops to make it a true
ecursive method.
6. Remember - replace with the actual name of
each function.
7. Including your 'main', there will be a total of 4 functions in you
program and only the primary helper functions can be invoked from
'main'. Note - for all functions except 'main', the 'Entering name>' and 'Leaving statements should be
printed.
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. You can expect the user will input the number of elements for the
a
ay on one line, not to exceed SORT_MAX_SIZE = 16 (validate
input).
2. Create an a
ay based on the size input provided by the user.
3. On the next input line you can expect the user will provide all the
a
ay elements separated by spaces. Validate that the integers
input by the user are between 1 and 9999, both inclusive.
Remember: 1 is not a prime number.
4. Make a call to the primary iterative function passing the a
ay and
its size.
5. If every member is a Prime, then the program should print out
'Prime A
ay using iteration', otherwise print out 'Not a Prime A
ay
using iteration'.
6. Then make a call to the primary recursive function passing the
a
ay and its size.
7. If every member is a Prime, then the program should print out
'Prime A
ay using recursion', otherwise print out 'Not a Prime
A
ay using recursion'.
8. If your functions are coded co
ectly, 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 use
interactivity as you see fit but programs will be graded for clarity of
interaction.
3. You can use language native a
ays - DO NOT USE VECTORS,
COLLECTIONS, SETS, BAGS or any other data structures from you
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 3 functions which are not 'main'.
7. Upload your code file and the screenshots in one zip file. Do not include
anything else.
Professore comments on my code:
Comments:
-15 (leniently) your code does not print the leaving statement at the right place in the recursive
function, your recursive prime check doesn't have output statements. No deduction - you
check for prime is not optimal.
No deduction for the missing pre/post documentation this time.

"""
CIS 22C Summer Quarte
Professor: Manish Goel
Student: Tannaz Anvari
**********
Started date: 06/29/2021
Updated dates: 06/30/2021, 07/01/2021, 07/02/2021
Final submission date: 07/03/2021
"""
# Code With Explanation:
#create the IsA
ayPrimeIter function accepting an a
ay and size of the a
ay
def IsA
ayPrimeIter(a
ay, size):
#print entering message
print("Entering IsA
ayPrimeIter")
#initialize variable isA
Prime to True
isA
Prime = True
#loop in every index of a
ay
for index in range(size):
#get the number of the index
XXXXXXXXXXnumber = a
ay[index]
#if the number is less than 2
XXXXXXXXXXif number < 2:
#update isA
Prime to False
XXXXXXXXXXisA
Prime = False
#
eak the loop

eak
#create inner loop ranging from 2 to numbe
XXXXXXXXXXfor num in range(2, number):
#check remainder of number divided by num, if equal to 0
XXXXXXXXXXif number % num == 0:
#udate isA
Prime to False
XXXXXXXXXXisA
Prime = False
#
eak the inner loop

eak
#if isA
Prime is False,
eak the outer loop
XXXXXXXXXXif not isA
Prime:

eak
#print leaving message
print("Leaving IsA
ayPrimeIter")
#return isA
Prime
return isA
Prime
#create function IsA
ayPrimeRecur accepting the a
ay and size of the a
ay
def IsA
ayPrimeRecur(a
ay, size):
#print entering message
print("Entering IsA
ayPrimeRecur")
#initialize variable isA
Prime to True
isA
Prime = True
#get the 1st value on the a
ay assign it to variable num and remove it using pop(0)
num = a
ay.pop(0)
#update size, decrement 1 from the size
size -= 1
#assign num-1 to variable possible_facto
possible_factor = num - 1
#check if num is prime using function IsPrimeRecur, assign return value to isA
Prime
isA
Prime = IsPrimeRecur(num, possible_factor)
#if size is not yet 0 and isA
Prime is still True
if size != 0 and isA
Prime:
#print leaving message
XXXXXXXXXXprint("Leaving IsA
ayPrimeRecur")
#call IsA
ayPrimeRecur again passing the updated value of a
ay and size
XXXXXXXXXXreturn IsA
ayPrimeRecur(a
ay, size)
#else
else:
#print leaving message
XXXXXXXXXXprint("Leaving IsA
ayPrimeRecur")
#return isA
Prime
XXXXXXXXXXreturn isA
Prime
#create the helper function IsPrimeRecur accepting number and facto
def IsPrimeRecur(number, factor):
#if number is less than 2
if number < 2:
#return false
XXXXXXXXXXreturn False
#if factor is equal to 1 already
if factor == 1:
#return true
XXXXXXXXXXreturn True
#if the remainder of number divide by factor is zero
if number % factor == 0:
#return false
XXXXXXXXXXreturn False
#else
else:
#call IsPrimeRecur passing the number and updated facto
XXXXXXXXXXreturn IsPrimeRecur(number, factor - 1)
#create main function
def main():
#initialize SORT_MAX_SIZE to 16
SORT_MAX_SIZE = 16
#get size from use
size = int(input("Enter size of a
ay: "))
#if size is greater than SORT_MAX_SIZE or size is less than 1
if size > SORT_MAX_SIZE or size < 1:
#print invalid message
XXXXXXXXXXprint("Size Invalid. The size of a
ay should be up to 16 numbers!")
#end program
XXXXXXXXXXexit()
#initialize empty a
ay
a
ay = []
#while the elements in a
ay is less than size
while len(a
ay) < size:
#get number from user, assign to num variable
XXXXXXXXXXnum = int(input('Enter number: '))
#if num is greater than 0 and less than or equal to 9999
XXXXXXXXXXif num <= 0 or num > 9999:
#print invalid value message
XXXXXXXXXXprint('Value invalid. Please enter value from 1 to 9999')
#if not
XXXXXXXXXXelse:
#add num to a
ay
XXXXXXXXXXa
ay.append(num)
# Printing A
ay list
print("A
ay List: ", a
ay)
print()
#if the return value of IsA
ayPrimeIter() is True, print prime a
ay
if IsA
ayPrimeIter(a
ay, size):
XXXXXXXXXXprint('Prime A
ay using iteration\n')
#otherwise, print that it is not prime
else:
XXXXXXXXXXprint('Not a Prime A
ay using iteration\n')
#if the return value of IsA
ayPrimeRecur() is True, print prime a
ay
if IsA
ayPrimeRecur(a
ay, size):
XXXXXXXXXXprint('Prime A
ay using recursion')
#otherwise, print that it is not prime
else:
Answered 3 days After Jul 22, 2021

Solution

Arun Shankar answered on Jul 24 2021
144 Votes
class LinkedNode:
# Constructo
def __init__(self, data):
self.data = data
self.next = None
# Getters
def get_data(self):
return self.data
def get_next(self):
return self.next
# Setters
def set_data(self, data):
self.data = data
def set_next(self, next):
self.next = next
def __str__(self):
return str(self.data)
class SinglyLinkedList:
# Constructo
def __init__(self):
self.count = 0
self.start = self.end = None
# Getters
def get_count(self):
return self.count
def get_start(self):
return self.start
def get_end(self):
return self.end
# Setters
def set_count(self, count):
self.count =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here