给定一个仅由0和1组成的数字列表,请将它们视为二进制表现形式,编写一个函数,将其转换成十进制数字。
示例:
输入:[0, 0, 0, 1]
,输出:1。因为 0b0001
对应十进制数字1
。
题目难度:简单
题目来源:Codewars: Ones and Zeros
def solution(nums: list) -> int:
# your code
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
Kawi
2021 年12 月 13 日 02:33
2
def solution(nums: list) -> int:
num = 0
j = 0
for i in nums[::-1]:
if i == 1:
num = num + pow(2, j)
j += 1
return num
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
wfcsm
(Wfcsm)
2021 年12 月 13 日 02:40
3
def solution(nums: list) -> int:
nums=nums[::-1]
sum=0
for i in range(len(nums)):
sum=sum+nums[i]*pow(2,i)
return sum
public Integer solution(Integer[] arr){
StringBuilder binStr = new StringBuilder();
for (Integer integer : arr) {
binStr.append(integer);
}
Integer sum = 0;
int len = binStr.length();
for (int i=1;i<=len;i++){
int dt = Integer.parseInt(binStr.substring(i-1,i));
sum+=(int)Math.pow(2,len-i)*dt;
}
return sum;
}
nobugs
2021 年12 月 13 日 02:50
5
def solution(nums: list) -> int:
return int(''.join([str(i) for i in nums]), 2)
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
Amoyshmily:
给定一个仅由0和1组成的数字列表,请将它们视为二进制表现形式,编写一个函数,将其转换成十进制数字。
示例:
输入:[0, 0, 0, 1]
,输出:1。因为 0b0001
对应十进制数字1
。
def solution(nums: list) -> int:
# your code
num_str = ""
for i in nums:
num_str += num_str.join(str(i))
return int(num_str, 2)
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
lekaixin
(ALe阿乐)
2021 年12 月 13 日 05:23
7
def solution(nums: list) -> int:
return int('0b'+''.join([str(i) for i in nums]),2)
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
def solution(nums: list) -> int:
return int(‘’.join([str(n) for n in nums]),2)
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
sunyanfen
(sunyanfen)
2021 年12 月 13 日 07:51
9
def solution1213(nums: list) -> int:
""" 给定一个仅由0和1组成的数字列表,请将它们视为二进制表现形式,编写一个函数,将其转换成十进制数字 """
str1 = '0b'+''.join(str(i) for i in nums)
return int(str1,2)
print(solution1213([1, 1, 1, 1]))
assert solution1213([0, 0, 0, 1]) == 1
assert solution1213([0, 0, 1, 0]) == 2
assert solution1213([1, 0, 1, 1]) == 11
assert solution1213([1, 1, 1, 1]) == 15
def solution(listname=[]):
num = 0
for i in range(0, len(listname)):
num += listname[i]*pow(2,len(listname)-i-1)
return num
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
public int solution(List<Integer> list) {
int res = 0;
for (int i = list.size() - 1; i >= 0; i--) {
res += list.get(i) * Math.pow(2, list.size()-i-1);
}
return res;
}
standy
(standy)
2021 年12 月 13 日 12:01
12
def solution(nums: list) -> int:
return int("".join((str(i) for i in nums)), base=2)
Mooii
2021 年12 月 14 日 03:04
13
def solution(nums: list) -> int:
str2 = ''
for i in nums:
str2 += str(i)
str1 = '0b'+str2
return int(str1,2)
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15
def solution(nums: list) -> int:
sum = 0
for i in range(len(nums)):
sum = sum + nums[i] * 2 ** (len(nums) - 1 - i)
return sum
assert solution([0, 0, 0, 1]) == 1
assert solution([0, 0, 1, 0]) == 2
assert solution([1, 0, 1, 1]) == 11
assert solution([1, 1, 1, 1]) == 15