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
Concept explainers
Question
Chapter 13, Problem 5PE
Program Plan Intro
Base Conversion
- Define a function “baseConversion()”.
- Declare a variable “digit” and initialize.
- Print the values of “num” and “digit”.
- Divide the value of “num” with “base”.
- If the condition “num == 0” is true
- Return true.
- Check another condition “num < base”.
- Assign the value of “num” to “digit”.
- Set “num” equal to “0”.
- If the conditions fails
- Call the function “baseConversion()”.
- Call the function “baseConversion()”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Fibonacci numbers are a sequence of integers, starting with 1, where the value of each number is the sum of the two previous numbers, e.g. 1, 1, 2, 3, 5, 8, etc. Write a function called fibonacci that takes a parameter, n, which contains an integer value, and have it return the nth Fibonacci number. (There are two ways to do this: one with recursion, and one without.)
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,…
This is in Python
This has two parts. This is a problem on recursion.
Part a:
Write a recursive function that accepts an integer argument, n. The user needs to be asked for the number n. The function should display n lines of asterisks on the screen, with the first (the top) showing 1 asterisk, the second from the top showing two asterisks, up to the nth line which shows n asterisks.
Part b.
Write a recursive function that accepts an integer argument, n. The user needs to be asked for the number n. The function should display n lines of asterisks on the screen, with the first (the top) showing n asterisks, the second from the top showing n-1 asterisks, up to the nth line which shows 1 asterisk.
Submit the two files and two sample outputs for each of the parts.
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
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Write a function sum_of_digits(a,b), that takes two arguments a and b, computes a to the power of b, then recursively finds the sum of digits until there is only one digit left. You must PRINT all steps of the process as shown in the below example. sum_of_digits(5,3) will PRINT on the screen the following: 5ˆ3 = 125 = 1 + 2 + 5 = 8 sum_of_digits(2,8) will PRINT on the screen the following: 2ˆ8 = 256 = 2 + 5 + 6 = 13 = 1 + 3 = 4arrow_forwardGiven a string str and number n, write a program that recursively appends a copy of string str n times and returns the resulting string. For example: RecuriveCopy("a", 10) // -> "aaaaaaaaaaa" RecursiveCopy("blah", 5) // -> "blahblahblahblahblahblah" For this exercise, complete the following function. n is the number of copies to add to str: function RecursiveCopy(str, n) { // your code here } Part 2: Call RecursiveCopy Write another function that calls RecursiveCopy() and returns the following string output: "The resulting string [x] has [y] characters". [x] should be replaced by the result from RecursiveCopy) and [y] should be replaced by the number of characters in the resulting string. You may pick the arguments to RecursiveCopy in CountChars or have the user provide input. I will leave this up to you. function CountChars() { } // your code here // call RecursiveCopy(..) // returns a formatted string For example: console.log(CountChars()) // -> "The resulting string blahblahblahblah…arrow_forwardHelp me please: A palindrome is a string that reads the same both forward and backward. For example, the string madam is a palindrome. Write a program that uses a recursive function to check whether a string is a palindrome. Prompt the user for a string. If the string is a palindrome output a message in the following format: madam is a palindrome else Hello is not a palindrome Your program must contain a value-returning recursive function that returns true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.arrow_forward
- Write a program that uses a recursive function, decToBin, to convert a non- negative integer in decimal format (that is base 10) into the equivalent binary number (that is base 2). In the main function, prompt the user to enter a non-negative integer in decimal format, then output its equivalent number in binary formatarrow_forwardWrite the definition of a recursive function int simpleSqrt(int n) The function returns the integer square root of n, meaning the biggest integer whose square is less than or equal to n. You may assume that the function is always called with a nonnegative value for n. Use the following algorithm: If n is 0 then return 0. Otherwise, call the function recursively with n-1 as the argument to get a number t. Check whether or not t+1 squared is strictly greater than n. Based on that test, return the correct result. For example, a call to simpleSqrt(8) would recursively call simpleSqrt(7) and get back 2 as the answer. Then we would square (2+1) = 3 to get 9. Since 9 is bigger than 8, we know that 3 is too big, so return 2 in this case. On the other hand a call to simpleSqrt(9) would recursively call simpleSqrt(8) and get back 2 as the answer. Again we would square (2+1) = 3 to get back 9. So 3 is the correct return value in this case.arrow_forward5. Write a python function that takes one integer parameter, makes sure it is an integer, prints sum of every two digits and return the sum of digits from the number entered by the user up to 1, using recursion. Sample input: 4 Sample output: 4+3 = 7 7+2 = 9 9+1 = 10arrow_forward
- Write a recursive function that converts a decimal number into a binary number as a string. The function header is as follows:def decimalToBinary(value): Write a test program that prompts the user to enter a decimal number and displays its binary equivalent.arrow_forwardWrite a recursive function called draw_triangle() that outputs lines of '*' to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting. Hint: The number of '*' increases by 2 for every line drawn. Ex: If the input of the program is: 3 the function draw_triangle() outputs: * *** Ex: If the input of the program is: 19 the function draw_triangle() outputs: * *** ***** ******* ********* *********** ************* *************** ***************** ******************* Note: No space is output before the first '*' on the last line when the base length is 19. if __name__ == '__main__': base_length = int(input()) draw_triangle(base_length)arrow_forwardUsing C++. Write a program using recursive a function to generate the following pattern of stars: ** ** * ** * * ** * ** ** Also, the program should prompt the user to enter the number of lines inthe pattern and uses the recursive function to generate the pattern. Forexample, specifying 4 as the number of lines generates the preceding pattern.arrow_forward
- For this exercise, you are going to write a recursive function that counts down to a Blastoff! Your recursive function will not actually print. It will return a String that can be printed from the main function. Each recursive call will add on to that string. In your main function, prompt the user for a starting value, then print the results. Sample Output Please enter a number to start: 5 5 4 3 2 1 Blastoff! import java.util.Scanner; public class Countdown{public static void main(String[] args){// Start here}public static String countdown(int number){// Base case - return Blastoff!// Recursive call}}arrow_forwardWrite a program in python with a function repeatPhrase(phrase, n), which prints the given phrase n times, alternating between lowercase and uppercase. For example, repeat('The sky is yellow', 5) would print: the sky is yellow THE SKY IS YELLOW the sky is yellow THE SKY IS YELLOW the sky is yellow Use the function in a program where you ask the user for the phrase and the value of narrow_forwardDesign a function that accepts an integer argument and returns the sum of all the integers from 1up to the number passed as an argument. For example, if 50 is passed as an argument, thefunction will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum.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
Computational Software for Intelligent System Design; Author: Cadence Design Systems;https://www.youtube.com/watch?v=dLXZ6bM--j0;License: Standard Youtube License