-1

Given an array of integers, determine whether each is a power of 2, where powers of 2 are [1,2,4,8,16,32,...] How do I append an array a value of 1 if it's a power of 2 or 0 otherwise.

#!/bin/python3

import math
import os
import random
import re
import sys

# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY arr as parameter.
def isPower(arr):
    # Write your code here


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    arr_count = int(input().strip())
    arr = []
    for _ in range(arr_count):
        arr_item = int(input().strip())
        arr.append(arr_item)
    result = isPower(arr)
    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')
    fptr.close()
Johnny Mopp
  • 12,641
  • 5
  • 36
  • 62
  • You have an abstract problem statement and a block of code. What is the connection between the two? Perhaps you have a specific question about the code? – Mad Physicist Dec 21 '21 at 16:17
  • 1
    It's look like homework. What do you try so far? `# Write your code here`? We can help you if you show us some effort to solve your problem. – Corralien Dec 21 '21 at 16:19
  • Welcome to Stack Overflow. It appears that your actual question is "how do I tell whether a number is a power of 2?", since you clearly know how to append values to a list (we do *not* call it an array) and I assume you can figure out a way to append `0` instead of the number once you figure out `if` it's appropriate to do so. Please read [ask] and ask the *actual question you have*, after first attempting some [research](https://meta.stackoverflow.com/questions/261592) – Karl Knechtel Dec 21 '21 at 16:19
  • Looks like the initial problem is converting the list to integers. [Get a list of numbers as input from the user](https://stackoverflow.com/q/4663306). Then finish `isPower`. [How to check if a given number is a power of two?](https://stackoverflow.com/q/57025836) – Johnny Mopp Dec 21 '21 at 16:21
  • This is another version of the code I'm working on. The other version is instead of isPower(arr), it's poweroftwo(n). I was able to solve the other one but couldn't for the ispower(arr). – TylerNG1712 Dec 21 '21 at 16:33
  • So, if now you have a list instead of an scalar, just iterate over the scalars inside the list. – Sembei Norimaki Dec 21 '21 at 16:34
  • You can try this, but it is a bit imprecise to to floating point math: `def is_power(n, base): return round(math.log(n) / math.log(base), 4).is_integer()`. Another option is to just loop through the powers of `base` until you are `>= n` Ex: `def is_power(n, base): i = base; while i < n: i *= base; return i == n` – Johnny Mopp Dec 21 '21 at 16:42
  • This looks like a question about asking for homework help. Please include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) You can find more information on how to ask a homework question in the FAQ: [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Paul Dec 29 '21 at 13:27

1 Answers1

1

To find out if the number is a power of two, count the number of set bits of its binary representation - if it is 1, then you have a power of two, if it's more, then it's not a power of two.

def isPower(arr):
    result = []
    for n in arr:
        if bin(n).count("1") == 1: # n is a power of two
            result.append(1)
        else:
            result.append(0)
    return result

The same can be done more compact:

def isPower(arr):
    result = []
    for n in arr:
        result.append(int(bin(n).count("1") == 1))
    return result

int called on a boolean value returns 1 for True and 0 for False,

Programmer
  • 5,276
  • 3
  • 9
  • 33