如何在我的情况下正确倍增

时间:2018-02-26 09:28:28

标签: python python-3.x python-requests python-3.6

def polmul(n1,n2):
      n1 = int (n1, 2)
      s = int ('0', 2)
      for c in n2 [:: -1]:
            if (c == '1'):
                 s = s ^ n1
            n1*=2
      Bin_out = bin(s)[2:].zfill(2)
      Mul = str(Bin_out)
      return Mul
n1 = '0000011'
n2 = '100010111'
x = polmul(n1,n2)

(结果是1100111001)

一开始就跳过零。 我需要结果000001100111001 请帮忙

1 个答案:

答案 0 :(得分:0)

var arr = [
    {"id": 111, "wbs_name": "Mechanical", "parent": 'root'},
    {"id": 222, "wbs_name": "Electrical", "parent": 111},
    {"id": 333, "wbs_name": "Systems", "parent": 111}
];

var { mechanical, electrical, systems} = Object.assign({}, ...arr.map(e => { 
  var parent = arr.find(i => i.id === e.parent);  
  return { [e.wbs_name.toLowerCase()] : {...e, parent: parent ? parent.wbs_name.toLowerCase() : 0 } };
 }));
 
 console.log("mechanical: ", mechanical);
 console.log("electrical: ", electrical);
 console.log("systems: ", systems);

可以解决这个问题,因为Bin_out = bin(s)[2:].zfill(15) 会在左侧填充字符串,并为*.zfill(fill_width) fill_widthfill_width=15