Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write in PYTHON programming. You must use recursion for every exercise. Write a

ID: 651781 • Letter: W

Question

Write in PYTHON programming.

You must use recursion for every exercise.

Write a recursive function called reverse that takes a list as an argument and reverses it.

Write a recursive function called search that takes a list that is already in order and a single integer and returns the index at which that integer appears in the list. If the integer does not appear in the list, return -1.

Write a function called stars that takes a single argument, height, and prints a triangle of stars of that height.

Write a function called gcd that takes two arguments and finds the greatest common divisor between the two.

Explanation / Answer

=================================================
1.Write a recursive function called reverse that takes a list as an argument and reverses it.

Ans:

#!/usr/bin/python

# Function definition is here
def rev(l):
   if not l: return []
   return [l[-1]] + rev(l[:-1])

mylist = [10,20,30];

print "Before Reverse :"
print mylist
# Calling rev function
mylist = rev(mylist);

print "After Reverse:"
print mylist
=================================================
2.Write a recursive function called search that takes a list that is already in order and a single integer

Ans:

#!/usr/bin/python

#Recurstive function for Search
def Search(arr,size,searchElement):
   if size == 0:
       return -1
   else:
       if arr[size-1] == searchElement:
           return size-1
       return Search(arr,size-1,searchElement)
  
  
a=[1,2,3,4,5]
searchElement=3
b=Search(a,3,searchElement)
if b==-1:
print "Index not found"
else:
print "Index of Search Element is :"
print b

=================================================
3.Write a function called stars that takes a single argument, height, and prints a triangle of stars of that height.

#Traingle function for Search
def printTriangle(height):
   if height == 0:
       return
   else:
       s=""
       count=0
       while(count < height):
           s+="*"
           count = count +1
  
       print s
       return printTriangle(height-1)
  
printTriangle(3)
  
==================================================
4. Write a function called gcd that takes two arguments and finds the greatest common divisor between the two.
Ans:
#gcd function

def gcd(a, b):
   if (0 == a % b):
       return b
   return gcd(b, a%b)
  
gcd=gcd(3,4)
print gcd

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote