# STAY HOME # STAY SAFE
# initialize a global dictionary
di={1:1,2:1}
pos=3
limit=0
flag=1
# recursive function fibtionary
def fibtionary(num):
# use the global variables
global pos,limit,flag
global di
# set the value of limit as num
if(flag==1):
limit=num
flag=0
# if num is 1 then return {1:1}
if(num==1):
return {1:1}
# if num is 2 then return di
if(num==2):
return di
else:
# else build the dictionary and recursively call the function until
the pos reaches the limit
di[pos]=di[pos-1]+di[pos-2]
pos=pos+1
# if the pos becomes greater than limit then return di
if(pos>limit):
return di
# else call the function recursively
else:
return fibtionary(pos)
num1 = 5
print(fibtionary(num1))
![In [15]: #STAY HOME #STAY SAFE # initial in a global dictionary di (1:1, 2:1) pos=3 limite flap-1 # recursive function fibtio](http://img.homeworklib.com/questions/75f682f0-a250-11eb-83a6-ab1feef95a0b.png?x-oss-process=image/resize,w_560)
![In 16]: STAY HONE STAY SAFE initialize a global dictionary dis 1:1, 2:1} posm3 linite fla=1 # recursive function fibtionary d](http://img.homeworklib.com/questions/766a2630-a250-11eb-8ad0-8df52d2b9a3a.png?x-oss-process=image/resize,w_560)
USE PYTHON / RECURSION METHOD Fibonacci Dictionary Function Name: fibtionary Parameters: num (int) Returns: dictionary (key:...
Python
String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description: You're texting your friend when you notice that they replace many letters with numbers. Out of curiosity, you want to find the product of the numbers. Write a function that takes in a string sentence and an int num, and find the product of only the first num numbers in the sentence. If num is 0, return 0. If num > O but there...
Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...
1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...
Fibonacci num Fn are defined as follow. F0 is 1, F1 is 1, and Fi+2 = Fi + Fi+1, where i = 0, 1, 2, . . . . In other words, each number is the sum of the previous two numbers. Write a recursive function definition in C++ that has one parameter n of type int and that returns the n-th Fibonacci number. You can call this function inside the main function to print the Fibonacci numbers. Sample Input...
in python Write a function pairlist(dictionary) that returns a list consisting of tuples of each key and value pair from that dictionary pairlist({"one": 1, "two": 2, "three": 3}) # returns [("one", 1), ("two", 2), ("three", 3)]
In C++
#include<iostream>
using namespace std;
//Implement the function below.
bool is_fibonacci_array(int*,int);
//Param 1: pointer to the beginning of an array of integers
//Param 2: size of that array
//Returns: true, is every element in the input array is a Fibonacci number
//(the order of the numbers in the array need not be ordered
//as per the Fibonacci sequence)
//false, otherwise
//A Fibonacci sequence is generated as follows.
//The first two numbers in the sequence are 0 and 1.
//The...
Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float Description: Given a list of integers and a number, you want to add the numbers in the list to a total value by iterating through the given list. However, when the index you are at is divisible by the integer passed in, you must instead divide the current total by the element at that index. You must be careful when dividing the total (you...
###############recursion (Python) ##def facto( num ): ## if num == 0: ## return 1 ## else: ## return num * facto( num - 1 ) ## ##Task 2. N! = 1*2*3*...*(N-1)*N as we know. There is another "double factorial": ##N!! : ##1)if N is odd then N!! = 1*3*5*7*9*...*N ##2)if N is even then N!! = 2*4*6*8*...*N ## ####Task 2. Create a function facto2 which calculates facto2(N) = N!! ##Show that your function works. ######use input(...)
Language: Python Function name : findwaldo Parameters : string Returns: int Description: Write a recursive function that takes in a string containing some combination of letters, numbers, and spaces, and return the starting index of the first instance of “waldo” contained in that string. If the string does not contain “waldo”, return -1. This function IS case sensitive, so “waldo” is not the same as “WALDO”. Code using string functions such as .index() and .find() that oversimplify the problem will...
Python Function Name: networks Parameters: int, list of tuples of int Returns: list of sets of int Description: In your class, many students are friends. Let’s assume that two students sharing a friend must be friends themselves; in other words, if students 0 and 1 are friends and students 1 and 2 are friends, then students 0 and 2 must be friends. Using this rule, we can partition the students into circles of friends. To do this, implement a function...