Question

Write a python program (recursive.py) that contains a main() function. The main() should test the following...

Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions:

1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n.

2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 7*4=4+4+4+4+4+4+4. (To keep the function simple, assume that x and y will always hold positive nonzero integers.)

3) rlines - Recursive Lines: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks.

4) rLargestList-LargestListItem:Designafunctionthatacceptsalist as an argument, and returns the largest value in the list. The function should use recursion to find the largest item.

5) rMultList - Recursive List Product: Design a function that accepts a list of numbers as an argument. The function should recursively calculate the product of all the numbers in the list and return that value.

6) rSum - Sum of Numbers: Design a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum.

7) rPower - Recursive Power Method: Design a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a 0 or a positive integer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def rprint(n):
    if n > 0:
        rprint(n-1)
        print(n)

def rmult(x,y):
    if y == 0:
        return 0
    elif y < 1:
        return -( x - rmult(x, y + 1))
    else:
        return (x + rmult(x, y - 1))

def rlines(n):
    if n > 0:
        rlines(n-1)
        print("*" * n)

def rLargestList(A):
    if len(A) == 1:
        return A[0]
    else:
        return max(A[0], rLargestList(A[1:]))

def rMultList(A):
    if len(A) == 1:
        return A[0]
    else:
        return (rMultList([A[0]]) * rMultList(A[1:]))

def rSum(n):
    if n == 1:
        return 1
    return n + rSum(n-1)  

def rPower(a, b):
    if b == 0:
        return 1
    if b == 1:
        return a
    else:
        return a * rPower(a, b-1)

def main():
    rprint(5)
    print(rmult(7,5))
    rlines(5)
    print(rLargestList([1,5,2,9,7]))
    print(rMultList([1,5,2,9,7]))
    print(rSum(50))
    print(rPower(3,4))

main()

Add a comment
Know the answer?
Add Answer to:
Write a python program (recursive.py) that contains a main() function. The main() should test the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • I need help writing this program in C++. Thanks so much for your help in advance...

    I need help writing this program in C++. Thanks so much for your help in advance Function 1: Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will return the sum of 1, 2, 3, 4, ... 50. Use recursion to calculate the sum. Demonstrate the function in a program. A sample...

  • Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum...

    Sum of Numbers Problem: Write a method that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the method will return the sum of 1, 2, 3, 4, . . . 50. Use recursion to calculate the sum. Demonstrate the method in a program. The program should be called SumOfNumbers.java.

  • Recursive Multiplication in Python Design a recursive function that accepts two arguments into the parameters x...

    Recursive Multiplication in Python Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 6 × 4 = 4 + 4 + 4 + 4 + 4 + 4

  • d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes...

    d printAllIntegers () Write a C++ program that defines and tests a function largest(....) that takes as parame- ters any three integers and returns the largest of the three integers. Your output should have the same format and should work for any integers a user enters Desired output: Enter three integers: 6 15 8 The largest integer is: 15 7.3 Recursive Functions Write a program that uses a function sum(int) that takes as an argument a positive integer n and...

  • NEED HELP THE PROBLEM-SOLVING PROCESS WITH THE UML DIAGRAM + ACTIVITY DIAGRAMS TO RUN THE CODE...

    NEED HELP THE PROBLEM-SOLVING PROCESS WITH THE UML DIAGRAM + ACTIVITY DIAGRAMS TO RUN THE CODE PROGRAM PLEASE!! Problem1. (20 points) Recursive Coding Problem. Using the Problem Solving Process. Write a Java application that implements these two functions using recursive methods (recursion must be used). First Recursive Function. When one passes an integer as input (n for example), the return should return (output) the sum as follows: 1 + 1/2 + 1/3 + 1/4 + ... 1/(n-1) + 1/n Example:...

  • Question 1 100 pts Design a function that accepts a list of numbers as an argument...

    Question 1 100 pts Design a function that accepts a list of numbers as an argument (this function only has one parameter, which is a list). The function should recursively calculate the sum of all the numbers in the list and return that value. Hint: refer to the Summing a range of list elements with recursion" example in slides.

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT