Question: Function should not contain for/while loops Function should not contain for/while loops Function should not contain for/while loops Function should not contain for/while loops Function

"Function should not contain for/while loops"

"Function should not contain for/while loops"

"Function should not contain for/while loops"

"Function should not contain for/while loops"

"Function should not contain for/while loops"

"Function should not contain for/while loops"

 

is_power_of(base, num)       (1.5 points) Determines whether num is a power of base (there is some non-negative integer k such that pow(base, k) equals num.

•You are not allowed to use pow, the ** operator, or import any function (such as math.log)

 

Preconditions base: int -> Positive integer

num: int -> Non-negative integer

 

Examples:

>>> is_power_of(7, 7)      # 7**1 = 7

True

>>> is_power_of(7, 0)      # 0 is not a power of any base

False

>>> is_power_of(7, 2401)   # 7**4 = 2401

True

>>> is_power_of(7, 1)      # 7**0 = 1

True

>>> is_power_of(7, 2458)   # 2458 is not a power of 7 False

 

 

        cut(a_list)                                                                                                                           (2 points)

Takes a list of integers and returns a list identical to a_list, but when a negative number appears, the function deletes the negative number and the next (x - 1) elements, where x is the absolute value of the negative number deleted. The function should not mutate a_list.

You are not allowed to use the in operator
You are not allowed to use the index() method
You are not allowed to use the copy() method
You are not allowed to make full copies of numList
 

Preconditions

a_list: list -> sequence of items

 

Examples:

>>> cut([7, 4, -2, 1, 9])   # Found -2: Delete -2 and 1

[7, 4, 9]

>>> cut([-4, -7, -2, 1, 9]) # Found -4: Delete -4, -7, -2 and 1 [9]

 

right_max(num_list)     (2.5 points) Given a list of numbers, return a new list in which each element is replaced by the maximum of itself and all the elements following it. The function should not mutate num_list.

You are not allowed to use the in operator
You are not allowed to use the index() method
You are not allowed to use the copy() method
You are not allowed to make full copies of numList
You are not allowed to use any built-in sorting methods
 

Preconditions

num_list: list -> sequence of items

 

Examples:

>>> right_max([3, 7, 2, 8, 6, 4, 5])  [8, 8, 8, 8, 6, 5, 5]

>>> right_max([1, 2, 3, 4, 5, 6])    

[6, 6, 6, 6, 6, 6]

>>> right_max([1, 25, 3, 48, 5, 6, 12, 14, 89, 3, 2])

[89, 89, 89, 89, 89, 89, 89, 89, 89, 3, 2]

 

 consecutive_digits (num)                                                                                             (1.5 points)

Returns True if num contains two consecutive digits that are the same, False otherwise. As described in Homework 1, floor division (//) and modulo (%) operators are useful here.

You are not allowed to convert n to other data type such str() or list()
You are not allowed to use lists
You are not allowed to use pow, the ** operator, or import any function (such as math.log)
 

Preconditions

n: int -> positive integer

 

 

>>> consecutive_digits(2222466666678)  True

>>> consecutive_digits(12345684562)  

False

>>> consecutive_digits(122)          True

 

 only_evens (num)                                                                                                       (2.5 points)

Takes a positive number and returns an integer formed by removing the odd digits from num. If num is zero or does not have even digits, return 0. As described in Homework 1, floor division (//) and modulo (%) operators are useful here.

You are not allowed to convert n to other data type such str() or list()
You are not allowed to use lists
You are not allowed to use pow, the ** operator, or import any function (such as math.log)
 

Preconditions

n: int -> positive integer

 

>>> only_evens(4386112)

4862

>>> only_evens(0)      

0

>>> only_evens(357997555531)

0

>>> only_evens(13847896213354889741236)

84862488426

 

 

 

 

 

 

 

 

def is_power_of(base, num):

"""

>>> is_power_of(5, 625) # pow(5, 4) = 5 * 5 * 5 * 5 = 625

True

>>> is_power_of(5, 1) # pow(5, 0) = 1

True

>>> is_power_of(5, 5) # pow(5, 1) = 5

True

>>> is_power_of(5, 15) # 15 is not a power of 5 (it's a multiple)

False

>>> is_power_of(3, 9)

True

>>> is_power_of(3, 8)

False

>>> is_power_of(3, 10)

False

>>> is_power_of(1, 8)

False

>>> is_power_of(2, 0) # 0 is not a power of any positive base.

False

>>> is_power_of(4, 16)

True

>>> is_power_of(4, 64)

True

>>> is_power_of(4, 63)

False

>>> is_power_of(4, 65)

False

>>> is_power_of(4, 32)

False

"""

## YOUR CODE STARTS HERE

pass





 

def cut(a_list):

"""

>>> cut([7, 4, 0])

[7, 4, 0]

>>> myList=[7, 4, -2, 1, 9]

>>> cut(myList) # Found(-2) Delete -2 and 1

[7, 4, 9]

>>> myList

[7, 4, -2, 1, 9]

>>> cut([-4, -7, -2, 1, 9]) # Found(-4) Delete -4, -7, -2 and 1

[9]

>>> cut([-3, -4, 5, -4, 1]) # Found(-3) Delete -2, -4 and 5. Found(-4) Delete -4 and 1

[]

>>> cut([5, 7, -1, 6, -3, 1, 8, 785, 5, -2, 1, 0, 42]) # Found(-1) Delete -1. Found(-3) Delete -3, 1 and 8. Found(-2) Delete -2 and 0

[5, 7, 6, 785, 5, 0, 42]

"""

## YOUR CODE STARTS HERE

pass





 

def right_max(num_list):

"""

>>> right_max([3, 7, 2, 8, 6, 4, 5])

[8, 8, 8, 8, 6, 5, 5]

>>> right_max([1, 2, 3, 4, 5, 6])

[6, 6, 6, 6, 6, 6]

>>> right_max([1, 25, 3, 48, 5, 6, 12, 14, 89, 3, 2])

[89, 89, 89, 89, 89, 89, 89, 89, 89, 3, 2]

"""

## YOUR CODE STARTS HERE

pass






 

def consecutive_digits(num):

"""

>>> consecutive_digits(2222466666678)

True

>>> consecutive_digits(12345684562)

False

>>> consecutive_digits(122)

True

"""

## YOUR CODE STARTS HERE

pass




 

def only_evens(num):

"""

>>> only_evens(4386112)

4862

>>> only_evens(0)

0

>>> only_evens(357997555531)

0

>>> only_evens(13847896213354889741236)

84862488426

"""

## YOUR CODE STARTS HERE

pass




 

def run_tests():

import doctest


 

# Run tests in all docstrings

doctest.testmod(verbose=True)

 

# Run tests per function - Uncomment the next line to run doctest by function. Replace is_power_of with the name of the function you want to test

#doctest.run_docstring_examples(is_power_of, globals(), name='LAB3',verbose=True)


 

if __name__ == "__main__":

run_tests()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!