Question

This is python coding recursive function question. Could anyone help me figuring out these not using...

This is python coding recursive function question.

Could anyone help me figuring out these not using built-in function?

Thanks.

  1. Write a recursive function

                factorial(n)
    

    that returns the value of n!=1⋅2⋅3⋅⋯⋅n.

2. Write a recursive function

reverse(text)
that returns the reverse of the string named text.

3. Write a recursive function

countUpper(s)
that returns the number of uppercase letters in the string s.

4. Write a recursive function

           equal(list1, list2)

that returns a Boolean value indicating whether the two lists are equal without testing whether list1 == list2. You may only compare the lengths of the lists and test whether individual list elements are equal.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. PLEASE MAKE SURE INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.

def factorial(n):
   if n == 1:
       return 1
   else:
       return n * factorial(n-1)

def reverse(text):
   if len(text) <= 1:
       return text
   else:
       return reverse(text[1:]) + text[0]

def countUpper(s):
   if s == '':
       return 0
   else:
       c = countUpper(s[1:])
       if s[0]>= 'A' and s[0] <= 'Z':
           c = c + 1
       return c

def equal(list1, list2):
   if list1 == [] and list2 == []:
       return True
   if len(list1) == len(list2) and len(list1) != 0:
       return list1[0] == list2[0] and equal(list1[1:], list2[1:])
   else:
       return False


#testing all the functions
n = 5
print('factorial of {} is {} '.format(n, factorial(n)))
text = 'hello'
print('reverse of {} is {}'.format(text,reverse(text)))
s = 'HeLlo'
print('no. of upper case letters in {} is {}'.format(s, countUpper(s)))
mylist1 = [1, 2, 3]
mylist2 = [1]
mylist3 = [1, 2, 3]
print('mylist1: ', mylist1)
print('mylist2: ', mylist2)
print('mylist3: ', mylist3)

if equal(mylist1, mylist2):
   print('mylist1 and mylist2 are equal')
else:
   print('mylist1 and mylist2 are not equal')

if equal(mylist1, mylist3):
   print('mylist1 and mylist3 are equal')
else:
   print('mylist1 and mylist3 are not equal')
  

output

Add a comment
Know the answer?
Add Answer to:
This is python coding recursive function question. Could anyone help me figuring out these not using...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is Python coding question, and topic is recursive. Can anyone help me figuring this out?...

    This is Python coding question, and topic is recursive. Can anyone help me figuring this out? (Only recursive function is allowed.) Thanks. Write a function called Fibonacci(n) that takes as a parameter an integer n and returns the nth Fibonacci number. The first two Fibonacci numbers are 1 and every subsequent one is the sum of the previous two: 1, 1, 2, 3, 5, 8, 13, .... Write a recursive function to count the number of uppercase letters in a...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • need help with python ( no loops used please!) Write a recursive function named sum_digits that...

    need help with python ( no loops used please!) Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one...

  • Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a...

    Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a function biasedCoinFlip(p) to return a biased coin flip that returns ''heads'' with probability p and tails otherwise. For example, when p=0.5, this function will behave like the last. On the other hand, p=0.25 will mean that there is only one chance in four of getting the result of "heads". 2. Write a function randomCard() to randomly return a card in a standard deck(suits: diamonds,...

  • Could someone help me out with the following python question? Preferably using the "return a <=...

    Could someone help me out with the following python question? Preferably using the "return a <= b < c" method instead of a standard if else statement. I would appreciate a step by step explanation if possible. Thank you! Write a class called Appointment with methods as described below: __init__() method Define an __init__() method with four parameters: self name, a string indicating the name of the appointment (for example, "Sales brunch"). start, a tuple consisting of two integers representing...

  • Using Python!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair...

    Using Python!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than m,...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • Using python Question 13 (20 points) Write a function named Linestats that finds the number of...

    Using python Question 13 (20 points) Write a function named Linestats that finds the number of consonant and vowel letters on each line of a file and writes those statistics to a corresponding line of a new file, separated by a space. Definitions: A vowel letter is one of a, e, i, o, u, along with the corresponding uppercase letters..A consonant letter is not a vowel letter. The function linestats takes two parameters: 1. inFile, a string, the name of...

  • Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is...

    Using Python, if you could help me with the code # Create a modified version of the search linear function defined # above to return all # occurrences of the search word in the text # An occurrence is the index of a word in the text that matches your given # search string. # e.g. if "hatter" occurs at positions 0, 6, 12 then return [ 0, 6, 12] def search_linear_occurrences(xs, target): """ Find and return a list of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT