Revel, Chapter 12, Python CS 119

Ace your homework & exams now with Quizwiz!

Write a function called fact that recursively calculates the factorial value of its parameter, which is an integer value.

def fact(n): if n<=1: return 1 else: n*fact(n-1) return fact(n-1) or def fact(n): if n==0 or n==1: return 1 else: return n * fact(n-1)

Write a recursive function, len, that accepts a parameter that holds a string value, and returns the number of characters in the string. The length of the string is: 0 if the string is empty ("") 1 more than the length of the string beyond the first character

def len(x): if(x==""): return 0 else: return 1+len(x[1:]) or def len(string): if string == "": return 0 else: return 1 + len(string[1:])

Write a function called printStars. The function receives a parameter containing an integer value. If the parameter is positive, the function prints (to standard output) the given number of asterisks. Otherwise the function does nothing. The function does not return a value. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed. The function must not use a loop of any kind (for, while, do-while) to accomplish its job. Instead, it should examine its parameter, returning if the parameters value is not positive. If the parameter is positive, it: -prints a single asterisk (and no other characters) -then crecursively calls itself to print the remaining asterisks

def main(): printStars(8) def printStars(num): if num > 0: print("*", end="") printStars(num-1) or def printStars(num): if num > 0: print("*", end="") printStars(num-1)

Assume the availability of a function named printStars that can be passed a parameter containing a non-negative integer value. The function prints out the given number of asterisks. Write a function named printTriangle that receives a parameter that holds a non-negative integer value and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 askterisks, and then a line of n-2 asterisks, and so on. For example, if the function received 5, it would print: ***** **** *** ** * The function must not use a loop of any kind (for, while, do-while) to accomplish its job. The function should invoke printStars to accom;lish the task of printing a single line.

def printTriangle(n): if n > 0: print("*"*n) printTriangle(n-1) or def printTriangle(n): printStars(n) if n > 1: print() printTriangle(n-1)

Write a recursive function, reverse, that accepts a parameter containing a string value and returns the original string in reverse. For example, calling reverse('goodbye') would return 'eybdoog'. Reversing a string involves: No action if the string is empty or only has 1 character Concatenating the last character with the result of reversing the string consisting of the second through next-to-last character, followed by the first character

def reverse(text): if text == "": return text else: return text[-1] + reverse(text[:-1]) or def reverse(string): if len(string) == 0 or len(string) == 1: return string else: return string[len(string)-1] + reverse(string[:-1])


Related study sets

Unclassified Antidysrhythmic Drugs

View Set

Chem 2 (Quiz 2): Chapter 15 (Acids and Bases)

View Set

Prepu | Chapter 69: Next Generation | NGN

View Set

REVIEW QUESTIONS KINESIOLOGY (INTRO, SHOULDER GIRDLE, & SHOULDER JOINT)

View Set

6.3: "Indigenous Response to Industrialization"

View Set

Chapter 8: Political Parties, Candidates, and Campaigns

View Set

Numbers 22 - Flashcard MC questions - Ted Hildebrandt

View Set

HESI Exam Fundamental of Nursing

View Set

WEEK 9 >>>Chapter 43 Liver, Biliary Tract, and Pancreas Problems Pancreatic Cancer

View Set