Python Programming: An Introduction to Computer Science, 3rd Ed.
3rd Edition
ISBN: 9781590282755
Author: John Zelle
Publisher: Franklin, Beedle & Associates
expand_more
expand_more
format_list_bulleted
Question
Chapter 13, Problem 1PE
Program Plan Intro
Fibonacci
- Define a function named “fib()”.
- Print a statement “Computing fib({})”.
- If condition “n<3” is true.
- Return a value “1”.
- If the condition is false
- Print a statement "Leaving fib({0}), returning {1}"
- Return the values
- Call the function “fib()”.
- Print the result.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
No plagarism!
Correct and detailed answer ( code with comments and output screenshot if necessary)
Write a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, the pattern generated is as follows: **** *** ** * * ** *** **** Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the preceding pattern.
create a function named powerHalf(x, n), where this function is recursive like power(x, n), but it also uses the idea that xn = (x n/2)2 when n is even.
Use the global variable countcalls to verify that this version of the power function is more efficient than power(x,n), where n is 0 or a positive integer with the base case that:
xn = x * xn-1
use python please
Chapter 13 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
Ch. 13 - Prob. 1TFCh. 13 - Prob. 2TFCh. 13 - Prob. 3TFCh. 13 - Prob. 4TFCh. 13 - Prob. 5TFCh. 13 - Prob. 6TFCh. 13 - Prob. 7TFCh. 13 - Prob. 8TFCh. 13 - Prob. 9TFCh. 13 - Prob. 10TF
Ch. 13 - Prob. 1MCCh. 13 - Prob. 2MCCh. 13 - Prob. 3MCCh. 13 - Prob. 4MCCh. 13 - Prob. 5MCCh. 13 - Prob. 6MCCh. 13 - Prob. 7MCCh. 13 - Prob. 8MCCh. 13 - Prob. 9MCCh. 13 - Prob. 10MCCh. 13 - Prob. 1DCh. 13 - Prob. 2DCh. 13 - Prob. 3DCh. 13 - Prob. 4DCh. 13 - Prob. 5DCh. 13 - Prob. 1PECh. 13 - Prob. 2PECh. 13 - Prob. 3PECh. 13 - Prob. 4PECh. 13 - Prob. 5PECh. 13 - Prob. 6PECh. 13 - Prob. 7PE
Knowledge Booster
Similar questions
- Computer scientists and mathematicians often use numbering systems other than base 10. Write a program that allows a user to enter a number and a base and then prints out the digits of the number in the new base. Use a recursive function baseConversion (num, base) to print the digits. Hint: Consider base 10. To get the rightmost digit of a base 10 number, simply look at the remainder after dividing by 10. For example, 153 % 10 is 3. To get the remaining digits, you repeat the process on 15, which is just 153 // 10. This same process works for any base. The only problem is that we get the digits in reverse order (right to left). The base case for the recursion occurs when num is less than base and the output is simply num. In the general case, the function (recursively) prints the digits of num // base and then prints num % base. You should put a space between successive outputs, since bases greater than 10 will print out with multi-character "digits." For example, baseConversion(1234,…arrow_forwardPart I Implement the Fibonnaci SequenceOne of this week’s quiz questions referred to the Fibonnaci sequence. This sequence of numbers is definedsuch that the nth number of the sequence is simply the sum of the two previous numbers in the sequence. Informal terms, Fn = Fn1 + Fn2, where Fnis the nth Fibonnaci number. Write a function in recursion.py, calledfibonnaci, which will accept one integer parameter (lets call it n) and returns the nth element of the Fibonnacisequence.Part II Implement Euclid’s GCD AlgorithmThe greatest common divisor, or GCD, of two integers is the largest number that divides both of them withno remainder. Euclid’s algorithm is one method to find the GCD of two numbers. Mathematically, we knowthat if r is the remainder when a is divided by b, then gcd(a, b) = gcd(b, r). Write a recursive function calledgcd that takes parameters a and b and returns their greatest common divisor. Think about what the basecase is for this algorithm.Part III String…arrow_forwardFor function decToBinary, write the missing parts of the recursion case. This function should return a string that stores the binary equivalent for int variable num. Example: The binary equivalent of 13 may be found by repeatedly dividing 13 by 2. So, 13 in base 2 is represented by the string "1101". Examples: decToBinary(13) -> "1101" public String decToBinary (int num) { if (num < 2) return Integer.toString(num); else return <<Missing recursive call>> + <<Missing calculation>>;}arrow_forward
- Write a recursive function to print all the permutations of a string. For example, for the string abc, the printout is:abcacbbacbcacabcba(Hint: Define the following two functions. The second function is a helper function.def displayPermuation(s):def displayPermuationHelper(s1, s2): The first function simply invokes displayPermuation(" ", s). The secondfunction uses a loop to move a character from s2 to s1 and recursively invokes t with a new s1 and s2. The base case is that s2 is empty and prints s1 to the console.)Write a test program that prompts the user to enter a string and displays all its permutations.arrow_forwardUse Dr Racket please.arrow_forwardOnly typing answers please.arrow_forward
- Using recursion, write a Python function def countOdds(A) which which takes an array of integers A as input and returns the number of elements in the array that are odd. For example, if A is [1,2,5,3,6,5,3,5,5,4] the the function should return 7. If A is empty then it should return 3. Hint: To check if n is odd it suffices to check if dividing it by 2 gives remainder 1 (in Python, this remainder is calculated by n%2).arrow_forwardWrite a recursive function for int powerOfTwo (int k). The function determines the value of 2k. (Note: k is a positive integer). Example, when k=0 the function returns 1 and when k-3 the function returns 8. To show that your code is correct, give the recursive trace for powerOfTwo (2) that returns 4.arrow_forwardWrite a recursive function that returns true if the digits of a positive integer are in increasing order; otherwise, the function returns false. Also, write a program to test your function.arrow_forward
- The 4th problem mimics the situation where eagles flying in the sky can be spotted and counted.FindEagles: a recursive function that examines and counts the number of objects (eagles) in aphotograph. The data is in a two-dimensional grid of cells, each of which may be empty (value 0) orfilled (value 1 to 9). Maximum grid size is 50 x 50. The filled cells that are connected form an object(eagle). Two cells are connected if they are vertically, horizontally, or diagonally adjacent. Thefollowing figure shows 3 x 4 grids with 3 eagles. 0 0 1 21 0 0 01 0 3 1 FindEagle function takes as parameters the 2-D array and the x-y coordinates of a cell that is a part ofan eagle (non-zero value) and erases (change to 0) the image of an eagle. The function FindEagleshould return an integer value that counts how many cells has been counted as part of an eagle and havebeen erased. The following sample data has two pictures, the first one is 3 x 4, and the second one is 5 x 5 grids. Notethat your program…arrow_forwardIn Kotlin, Write and test a recursive function called harm with an expression body that takes an Int argument n and calculates the sum of 1/1 ... 1/n. For example, harm(3) should return about 1.833333333. You do not need to worry about integer division.arrow_forwardA palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes:Able was I, ere I saw ElbaA man, a plan, a canal, PanamaDesserts, I stressedKayakWrite a bool function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward andbackward. Demonstrate the function in a program.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning