I am reading a javascript source and I need to convert it to Python.
Here is the code, since I am just a beginner, I dont get .reduce() function at all
function bin2dec(num){
return num.split('').reverse().reduce(function(x, y, i){
return (y === '1') ? x + Math.pow(2, i) : x;
}, 0);
}
Here is what I have tried so far
def bin2dec(num):
listNum = list(num)
listNum = map(int, listNum)
x = listNum[-1]
listNum.reverse()
for i in range (len(listNum)):
if listNum[i] == 1:
x = x + 2**listNum[i]
else:
x = x
return x
But it is not correct.
let bin2dec = num => parseInt(num, 2);.