Question

this assignment is about solving problems using recursion. For each question, write a recursive function that...

this assignment is about solving problems using recursion. For each question, write a recursive function that solves the problem asked in that question. Your solutions don't have to be efficient, but they do have to use recursion in a non-trivial way. Put all your code in a file called a9.py.

  1. Write a recursive function is_palindrome(s) that returns True or False depending on whether s is a palindrome. This is equivalent to returning s == s[::-1].
  2. Write a recursive function find_min(a) that returns the smallest value in the list a.
  3. Write a recursive functionreverso(a) that returns a new list that reverses the list  a. This is equvalent to returning a[::-1]
  4. Write a recursive functiontower(x) where x is a non-negative integer that returns 2**(2**(2**2(**...**2)...) where the number of 2s in the tower is x
    tower(1)
    2
    >>> tower(2)
    4
    >>> tower(3)
    16
    >>> tower(4)
    65536
    
  5. Write a recursive function is_prime(n) that returns True or False depending on whether the positive integer n is prime. (A positive integer n is prime if the only positive integers that divide n are n and 1.)

Homework Answers

Answer #1

def is_palindrome(s):
    if len(s) <= 1:
        return True
    else:
        if s[0] != s[-1]:
            return False
        else:
            return is_palindrome(s[1:-1])


def find_min(a):
    if len(a) == 1:
        return a[0]
    else:
        small = find_min(a[1:])
        if a[0] < small:
            small = a[0]
        return small


def reverse(a):
    if len(a) == 0:
        return []
    else:
        lst = reverse(a[1:])
        lst.append(a[0])
        return lst


def tower(x):
    if x == 0:
        return 1
    else:
        return 2 ** (tower(x - 1))


def is_prime(n, i=2):
    if n <= 1:
        return False
    elif n == i:
        return True
    else:
        return n % i != 0 and is_prime(n, i + 1)
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
In LISP, write a recursive function that takes an integer n and returns the summation of...
In LISP, write a recursive function that takes an integer n and returns the summation of n and all positive integers preceding it. ; e.g., passing 5 will return 15, which is 1+2+3+4+5. I have the worst skill when it comes to creating recursion in the LISP programming language. Any help at all would be appreciated.
In C++ Using recursion write a program that takes a positive integer number and returns whether...
In C++ Using recursion write a program that takes a positive integer number and returns whether there is 6. For example, if the input number is 7068, the function returns true
For C++: a) Write a function is_prime that takes a positive integer X and returns 1...
For C++: a) Write a function is_prime that takes a positive integer X and returns 1 if X is a prime number, or 1 if X is not a prime number. b) write a program that takes a positive integer N and prints all prime numbers from 2 to N by calling your function is_prime from part a.
C++ question: Write a recursive function that takes a non-negative integer as an argument and returns...
C++ question: Write a recursive function that takes a non-negative integer as an argument and returns an integer with the same digits of the argument in reverse order (i.e. in order from right to left). All trailing zeros in the argument number are omitted. For example, if the argument is 76038, it would return 83067 and if the argument is 45600 it returns 654.
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week...
*In Java Please RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: 1. Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that...
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The...
write a recursive racket function "sum-alternate" that takes a positive integer x as a parameter. The function should return the sum of all the integers x, x-2, x-4, x-6, etc. as long as the numbers are positive. For example, [sum-alternate 5] should evaluate to 5 + 3 + 1, and [sum-alternate 6] should evaluate to 6+4+2.
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list...
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because these are the only strings shorter than 3. Your solution must not be recursive. You will...
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 - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
Write the recursive version of the function decimal which takes in n, a number, and returns...
Write the recursive version of the function decimal which takes in n, a number, and returns a list representing the decimal representation of the number. def decimal(n): """Return a list representing the decimal representation of a number. >>> decimal(55055) [5, 5, 0, 5, 5] >>> decimal(-136) ['-', 1, 3, 6] """
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT