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 string. (Hint: s.upper() returns the all caps or uppercase version of a string.)
Write a function called pow(a, b) that returns a raised to the power of b (i.e., a ** b) without making use of **.
Write a function called count(list, string) that returns the number of times the string appears in the list.
Write a function called remove(list, string) that returns a list with all occurrences of the string removed from it.
(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".
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)
This is Python coding question, and topic is recursive. Can anyone help me figuring this out?...
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...
This is Python Coding question. Can anyone help me figuring these out? (Please don't use built-in function, and leave comments for my understanding) Thanks!! 1. Write a function called span(n) that takes as a parameter a 2D list of numbers called n and returns the difference between the largest and smallest number in n. For example, it should return the value 6 for the list [[2, 1, 3],[3, 7, 4]] since the largest number is 7 and the smallest is...
Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...
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 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 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 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...
In C : Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced. You can assume the original words are unique. For coding simplicity, follow each output word by a space, even the last one. Hint:...
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 ...
Need help figuring this out Write a program that reads in a sequence of numbers (doubles) from standard input until 0 is read, and stores them in an array, This part is done using iteration (loop). You may assume that the maximum size of the array will not be more than 100. Your program computes the maximum number stored in the array, the count of negative numbers, and computes the sum of positive numbers, using recursion. You need to have...