Question

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.

  1. 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, ....

  2. Write a recursive function to count the number of uppercase letters in a string. (Hint: s.upper() returns the all caps or uppercase version of a string.)

  3. Write a function called pow(a, b) that returns a raised to the power of b (i.e., a ** b) without making use of **.

  4. Write a function called count(list, string) that returns the number of times the string appears in the list.

  5. Write a function called remove(list, string) that returns a list with all occurrences of the string removed from it.

  6. (Harder) Write a function called replace(string, original, replacement) that returns the string with all occurrences of the string original changed to the string replacement. For example, replace("the cat in the hat", "the", "a") should return the string "a cat in a hat".

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

As Mentioned Above All The Programs Below Have Recursive Functions

Question 1:

def fibonacci(num):
   if num<0:
       print("Invalid Input")
  
   elif num==0:
       return 1
  
   elif num==1:
       return 1
   else:
       return fibonacci(num-1)+fibonacci(num-2)

print(fibonacci(9))

***********************************************************

Question 2:


def upcount(string):
count = 0
for i in string:
if(i.isupper()):
count=count+1
print(count)

string = 'CheggIndiaBonus'
upcount(string)

**************************************************************

Question 3:

def power(a,b):

   if (b == 0): return 1
   elif (int(b % 2) == 0):
       return (power(a, int(b / 2)) *
power(a, int(b / 2)))
   else:
       return (a * power(a, int(b / 2)) *
power(a, int(b / 2)))


a = 2; b = 3
print(power(a,b))


**************************************************************

Question 4:


def count(list, string):
count = 0
for str in list:
if (str == string):
count = count + 1
   return count

list = ['S', 'SUR', 'SURAJ', 'SUMIT', 'SUYESH', 'SUMAN']
string = 'S'
print('{} has occured {} times'.format(string, count(list, string)))

**************************************************************

Question 5:

def remove(list,string):
while (list.count(string)):
list.remove(string)
  
print(list)

list = ['s','r','s','t','s']
string = 's'
remove(list,string)

*****************************************************************

Question 6:

def replace(string, original, replacement):
str = string.replace(original , replacement)
print(str)
string = "Chegg is The Great Platform"
original = 'The'
replacement = '''a'''
replace(string,original,replacement)

  

Add a comment
Know the answer?
Add Answer to:
This is Python coding question, and topic is recursive. Can anyone help me figuring this out?...
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 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. 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...

  • I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a...

    I need help with this code. Im using C++ coding. Non Recursive (iterative) Fibonacci Write a program that uses a for loop to calculate a Fibonacci sequence (NOT RECURSIVE!!!) up to a given position, starting with position 0. This function defines a Fibonacci sequence: If the number is 0, the function returns a 0 If the number is 1, the function returns a 1 If the number is higher than 1, it returns the sum of the previous two numbers...

  • 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,...

  • Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed...

    Help me with this Python Question a. build_word_dictionary (filename) – This builds a word dictionary indexed by words from the file who’s filename is provided as an argument. It uses the words as keys and the count of occurrences as values. It returns the dictionary it constructed. It can use the ‘tokenize()’ function that is provided in the lecture slides. b. inverse_dict(dict) – This method takes a dictionary (generated by build_word_dictionary() and inverts it (as was done with students and...

  • Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number...

    Using java programming. Question 1. Write a recursive function fibo(n) that returns the nth Fibonacci number which is defined as follows: fibo(0) = 0 fibo(1) = 1 fibo(n) = fibo(n-1) + fibo(n-2) for n >= 2 Question 2. Write a recursive function that calculates the sum of quintics: 1power of5 + 2power of5 + 3power of5 + … + n5 Question 3. Write a program to find a route from one given position to another given position for the knight...

  • Need help coding this List. Lists are a lot like arrays, but you’ll be using get,...

    Need help coding this List. Lists are a lot like arrays, but you’ll be using get, set, add, size and the like instead of the array index operators [].​ package list.exercises; import java.util.List; public class ListExercises {    /**    * Counts the number of characters in total across all strings in the supplied list;    * in other words, the sum of the lengths of the all the strings.    * @param l a non-null list of strings   ...

  • Let’s work together to develop a call tree for the execution of the following recursive method....

    Let’s work together to develop a call tree for the execution of the following recursive method. (The method allows us to recursively generate the nth integer in the Fibonacci sequence, although you don’t need to be familiar with that sequence to understand this problem.) public static int fib(int n) { if (n == 0 || n == 1) { return 1; } else { int prev1 = fib(n - 2); int prev2 = fib(n - 1); return prev1 + prev2;...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

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