Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Bitsmask_problem.py #713

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Bitsmask_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
##### FUNCTION DEFINATION #####
def solution(n, q, operations):
bitmask = [0] * n
print("Bitmask = "+str(bitmask))
sum = 0

if q == 0:
sum = 0
else:
## Perform operations
print("\nOperation Started....")
for operation in operations:
type, l, r = operation

if type == 0:
for i in range(l, r+1):
bitmask[i] = bitmask[i] ^ 1

print("Updated Bitmask = "+str(bitmask))

elif type == 1:
set_bit_count = 0
for i in range(l, r+1):
if bitmask[i] == 1:
set_bit_count = set_bit_count + 1

sum = sum + set_bit_count
print("Sum after query = "+str(sum))

sum = sum % 1000000007

return sum

if name == "main":
##### PREPARATION OF INPUT #####

n = int(input("Enter No. of bits (n): "))
# n = 5
print("n = "+str(n))

q = int(input("Enter No. of Operations (q): "))
# q = 4
print("q = "+str(q))

# operations = [(0, 1, 3), (1, 1, 2), (0, 0, 4), (1, 3, 4)]

operations=[]
for i in range(q):
operations.append(tuple(map(int, input("Operation "+str(i+1)+" = ").split(" "))))

print("Operations = "+str(operations))

sum = solution(n, q, operations)

print("\nSum of all queries = "+str(sum))