Question
(Python 3.5, short and simple codes will suffice) 1. The sum of the numbers 1 to n can be calculated recursively as follows: The sum
(Python 3.5, short and simple codes will suffice)
1. The sum of the numbers 1 to n can be calculated recursively as follows:
The sum from 1 to 1 is 1.
The sum from 1 to n is n more than the sum from 1 to n-1
Write a function named sum that accepts a variable containing an integer value as its parameter and returns the sum of the numbers from 1 to to the parameter (calculated recursively).
___________________________________________________________________________________
2. Given non-negative integers x and n, x to the nth power can be defined as:
x to the 0th power is 1
x to the nth power can be obtained by multiplying x to the (n-1)th power with x
Write a function named power that accepts two parameters containing integer values (x and n, in that order) and recursively calculates and returns the value of x to the nth power.
_______________________________________________________________________________________________________
3. The Fibonacci series (0, 1, 1, 2, 3, 5, 8, 13, 21) has as its first two values 0 and 1; each successive value is then calculated as the sum of the previous two values. The first element in the series is considered the 0th element. The nth element in the series, written as fib(n), is thus defined as:
n if n=0 or n=1
fib(n-1)+fib(n-2) otherwise
Write a function fib, that takes a single parameter, n, which contains an integer value, and recursively calculates and returns the nth element of the Fibonacci series.
______________________________________________________________________________________________________________________
4. Write a recursive function, len, that accepts a parameter that holds a string value, and returns the number of characters in the string. The length of the string is:
0 if the string is empy ("")
1 more than the length of the string beyond the first character
_________________________________________________________________________________________
5. The sum of the elements in a tuple can be recusively calculated as follows:
The sum of the elements in a tuple of size 0 is 0
Otherwise, the sum is the value of the first element added to the sum of the rest of the elements
Write a function named sum that accepts a tuple as an argument and returns the sum of the elements in the tuple.
______________________________________________________________________________________________________________
6. An 'array palindrome' is an array, which, when its elements are reversed, remains the same. Write a recursive function, isPalindrome, that accepts a tuple and returns whether the tuple is a palindrome. A tuple is a palindrome if:
the tuple is empty or contains one element
the first and last elements of the tuple are the same, and the rest of the tuple is a palindrome
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started