Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following code. It is supposed to take a list of numbers from the u

ID: 3833475 • Letter: C

Question

Consider the following code. It is supposed to take a list of numbers from the user. Make a new list containing only the numbers which are powers of 2. Finally it is supposed to print these numbers 5 numbers in a line.

It uses two functions ispow2 which takes a number n and returns the boolean value True if that number is a power of 2 otherwise False.

print5 which takes a list and prints the list 5 numbers per line separated by spaces.

Please write code the correct ispow2 and print5 functions and submit the complete program. Everything in main() is already correct.

def ispow2(n):

<your code here>

def print5(lst):

<your code here>

def main():
    lst = input("Please enter the list")
    nums = [int(j) for j in lst.split()]

    newlst = []
    for i in nums:
        if (ispow2(i)):
            newlst.append(i)

    print5(newlst)
   
main()

Sample output:

Please enter the list:2 4 8 5 7 9 2 4 8 2 16 17

2 4 8 2 4

8 2 16

Explanation / Answer

Here is the code for you:

def ispow2(n):
   val = 1
   while val <= n:
       if val == n:
           return True
       val *= 2
   return False      
  
def print5(lst):
   for i in range(len(lst)):
       if i % 5 == 0:
           print
       print lst[i],

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote