Question

In: Computer Science

language python use a functions and write a python of a computer with three power operations:...

language python




use a functions and write a python of a computer with three power operations: power by two, power by three, and power by four.

input a number and operation (^2,^3,or^4).

output: the result of the power operation

Solutions

Expert Solution

The power function for ^2, ^3, and ^4 can be defined recursively as follows:

n is the number whose power is to be computed.

  • Base case:

If power is 2, return n * n

  • Recursive case 1:

If power is 3, return n * power(n,2)

  • Recursive case 2:

If power is 4, return n * power(n,3)

For all other powers, this function will return -1.

The python program is as follows:

#function power
#first parameter is the number whose power is to be calculated
#second parameter is the number for the power operation ^2, ^3 or ^4
#if invalid value of power is passed, the function returns -1
def power(n,p):
    
    #for ^2
    if (p == 2):
        return n*n
    
    #for ^3
    if (p == 3):
        return n*power(n,2)      #recursive call
        
    #for ^4
    if (p == 4):
        return n*power(n,3)      #recursive call
    
    #for invalid
    else:
        return -1
        
print(power(4,2))      #4^2
print(power(3,3))      #3^3
print(power(2,4))      #2^4
print(power(4,6))      #4^6  -> invalid power 6

Output:

Code snippet:


Related Solutions

Write a Python program that allows the user to perform three operations (implemented as three functions)...
Write a Python program that allows the user to perform three operations (implemented as three functions) on a dictionary: add_to_dict(): takes a dictionary, a key, a value and adds the <key,value> pair to the dictionary. If the key is already in the dictionary, then the program displays the error message: "Error. Key already exists.". remove_from_dict(): takes a dictionary and key and removes the key from the dictionary. If no such key is found in the dictionary then the program prints:...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement...
PYTHON PYTHON Recursive Functions. In this problem, you are asked to write three recursive functions. Implement all functions in a module called problem1.py. (10 points) Write a recursive function called remove char with two parameters: a string astr and a character ch. The function returns a string in which all occurrences of ch in astr are removed. For example, remove char("object oriented", ’e’) returns the string "objct orintd". Your implementation should not contain any loops and may use only the...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Please use Java language to write two efficient functions: Write an efficient function that compute the...
Please use Java language to write two efficient functions: Write an efficient function that compute the intersections of two sorted arrays of integers Write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each...
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each of the following functions. The function header must be implemented exactly as specified. Write a main function that tests each of your functions. Specifics In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should have #two parameters: a dictionary and a string. The string may be #a key to the dictionary. The value associated with that key, #in turn, may be another key to the dictionary. # #Keep looking up the keys until you reach a key that has no #associated value. Then, return that key. # #For example, imagine if you had the following dictionary. #This one...
you may not use the Python ord() or chr() functions you may not use the Python...
you may not use the Python ord() or chr() functions you may not use the Python ord() or chr() functions you may not use the Python ord() or chr() functions You will write a total of four functions, each of which will take two inputs and return a string: c_encrypt() c_decrypt() vig_encrypt() vig_decrypt() The first argument will be a string containing the plaintext (or clear text) message to be encrypted for the two encrypt functions, and a string containing a...
Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this...
Python Language Only! Python Language Only! Python Language Only! Python 3 is used. When doing this try to use a level one skill python, like as if you just learned this don't use anything advanced if you can please. If not then do it as you can. Assume you have a file on your disk named floatnumbers.txt containing float numbers. Write a Python code that reads all the numbers in the file and display their average. Your code must handle...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT