물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Ones and Zeros

BEstyle 2022. 9. 18. 11:20

Given an array of ones and zeroes, convert the equivalent binary value to an integer.

Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.

Examples:

Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11

However, the arrays can have varying lengths, not just limited to 4.

 

 


def square_sum(numbers):
    ans=0
    for num in numbers:
        ans+=num**2
    return ans

def binary_array_to_number(arr):
    return int("".join(map(str, arr)), 2)

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Array.diff  (0) 2022.09.19
Get the Middle Character  (1) 2022.09.19
Duplicate Encoder  (0) 2022.09.19
Two Sum  (0) 2022.09.17
Two to One  (0) 2022.09.14