patternpythonMinor
"Changing Bits" challenge
Viewed 0 times
changingbitschallenge
Problem
Problem Statement:
Let A and B be two N bit numbers (MSB to the left). You are given initial values for A and B, and you have to write a program which processes three kinds of queries:
-
-
-
Input Format:
First line of input contains two integers \$N\$ and \$Q\$ consecutively (\$1 \le N \le 100000\$, \$1\le Q \le 500000\$). Second line is an \$N\$-bit binary number which denotes initial value of \$A\$, and the third line is an \$N\$-bit binary number denoting initial value of \$B\$. \$Q\$ lines follow, each containing a query as described above.
Output Format:
For each query of the type
Sample Input:
Sample Output:
I solved this question and I tried to optimize by reducing the number of calculations for bit addition, but am still getting time-limit-exceeded where the time exceeds 10 seconds for Python.
```
n, q = map(int, raw_input().split())
A_inp = list(raw_input())
B_inp = list(raw_input())
Query_output = []
def LSB(idx, n):
return (n - 1) - (int(idx))
def Bit_Add(a, b, n):
length = len(a)
output = []
carry = 0
for i in range(length):
if a[i] == "1" and b[i] == "1":
if carry == 1:
output.append(1)
carry = 0
else:
output.append(0)
carry = 1
elif a[i] == "1" or b[i] == "1":
if carry == 1:
output.append(1)
carry = 0
else:
output.append(0)
else:
if carry == 1:
output.append(1)
carry = 0
if carry == 1:
o
Let A and B be two N bit numbers (MSB to the left). You are given initial values for A and B, and you have to write a program which processes three kinds of queries:
-
set_a idx x: Set A[idx] to x, where \$0 \le idx -
set_b idx x: Set B[idx] to x, where \$0 \le idx -
get_c idx: Print C[idx], where \$C=A+B\$, and \$0\le idx\$Input Format:
First line of input contains two integers \$N\$ and \$Q\$ consecutively (\$1 \le N \le 100000\$, \$1\le Q \le 500000\$). Second line is an \$N\$-bit binary number which denotes initial value of \$A\$, and the third line is an \$N\$-bit binary number denoting initial value of \$B\$. \$Q\$ lines follow, each containing a query as described above.
Output Format:
For each query of the type
get_c, output a single digit \$0\$ or \$1\$. Output must be placed in a single line.Sample Input:
5 5
00000
11111
set_a 0 1
get_c 5
get_c 1
set_b 2 0
get_c 5Sample Output:
100I solved this question and I tried to optimize by reducing the number of calculations for bit addition, but am still getting time-limit-exceeded where the time exceeds 10 seconds for Python.
```
n, q = map(int, raw_input().split())
A_inp = list(raw_input())
B_inp = list(raw_input())
Query_output = []
def LSB(idx, n):
return (n - 1) - (int(idx))
def Bit_Add(a, b, n):
length = len(a)
output = []
carry = 0
for i in range(length):
if a[i] == "1" and b[i] == "1":
if carry == 1:
output.append(1)
carry = 0
else:
output.append(0)
carry = 1
elif a[i] == "1" or b[i] == "1":
if carry == 1:
output.append(1)
carry = 0
else:
output.append(0)
else:
if carry == 1:
output.append(1)
carry = 0
if carry == 1:
o
Solution
The trick to this challenge is to actually change the number into bit operations, and not to do string manipulation as you do. So here are some hints on how to do this to get you started:
Combine these, and you'll get a fast enough solution.
Style review of current code
Some tips according to PEP8:
Refactored code running within time limit
So as not to spoil the fun for those wanting to solve this challenge by themselves, I've chosen to have the code hidden beneath a spoiler. That is if you hover over the block below you'll see the code.
def getBit(n, offset):
return (n & (1 > offset
def clearBit(n, offset):
return n & ~(1
- Convert a binary string,
text, of0's and1's into an int, useint(text, 2)
- To clear a bit at a given offset, use
n & ~(1
- To set a bit at a given offset, use n | (1
- To get a bit at a given offset (shifted down again to one bit), use
(n & (1 > offset)
Combine these, and you'll get a fast enough solution.
Style review of current code
Some tips according to PEP8:
- Use
lower_snake_casefor variable and function names - This looks a little better, and wouldn't throw off the syntax highlighter here on Code Review either... :-)
- Add a little vertical space in your code – Add two blank lines between functions, and add a blank line in natural segments within functions, like before
if,else,forandwhile.
- Put all top level code in a
main()function – Try to only keep imports, constants and functions at the top level, and start your code using aif __name__ == '__main__':' followed bymain()
- Start using the print
function, andstr.format'ing– The%syntax will disappear some time, so start using the newer string formatting options already available.
- For Python 2 and large \$n\$'s use xrange()
inforloops – Usingrange()will create the entire list, whilstxrange()will create a generator, and only give you one element at a time. For large numbers this has a noticeable effect on memory and time performance
- No need to create a list for join()
– Instead of''.join([ ... ]), you can simply use''.join( ... ). The latter uses a generator instead of a list, similar to therange()vsxrange()`
Refactored code running within time limit
So as not to spoil the fun for those wanting to solve this challenge by themselves, I've chosen to have the code hidden beneath a spoiler. That is if you hover over the block below you'll see the code.
def getBit(n, offset):
return (n & (1 > offset
def clearBit(n, offset):
return n & ~(1
Context
StackExchange Code Review Q#113340, answer score: 5
Revisions (0)
No revisions yet.