This repository has been archived on 2025-12-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LUT-yliopisto/BM40A1500 Data Structures and Algorithms/Assignments/Week 2/bitpairs.py
2023-01-08 18:43:01 +02:00

14 lines
355 B
Python

def pairs(s):
result = 0
loc = [x for x in range(len(s)) if s[x] == "1"]
for i in range(len(loc)):
result += loc[i]*(i) - loc[i]*(len(loc)-i-1)
return result
if __name__ == "__main__":
print(pairs("100101")) # 10
print(pairs("101")) # 2
print(pairs("100100111001")) # 71
print(pairs("0011110001110011010111"))