Perform a predetermined set of operations on a large sequencePrinting a list as “a, b, c.” using PythonHow can I make my solution for Chef and Digits run faster?Cutting The Sticks“Angry Professor” Python implementationFind Watson's integerC++ implementation of Hackerrank's “Maximum Element in a Stack”Parallelogram ConnectivitySummed absolute difference of two arrayTLE in FOXLINGS SPOJCodeChef's Tree MEX (Minimum Excludant) challenge
How to respond to "Why didn't you do a postdoc after your PhD?"
Skewer removal without quick release
What happens if a geocentric model of the world were correct?
Trying to add electrical outlets off of a junction box but the junction box has a lot more wires than Ive been shown so which ones to run it off?
Why does b+=(4,) work and b = b + (4,) doesn't work when b is a list?
'Pound' meaning in this context
What is a recommended strategy on exercises in a mathematical textbook at graduate level?
What's the current zodiac?
Proofreading a novel: is it okay to use a question mark with an exclamation mark - "?!"
Variable fixing based on a good feasible solution
Extra battery in the gap of an HDD
counter in hexadecimal base
How to print and use a command output in a one-liner?
d-Menthol vs dl-menthol: Does an enantiomer and its racemic mixture have different melting points?
Perform a predetermined set of operations on a large sequence
I’m having a hard time deciding whether this is a redemption arc
Where is the 'zone of reversed commands...'?
What are the physical limits that determine a camera's flash sync speed?
Is the Thief Rogue's Thief's Reflexes feature optional?
What is the fastest algorithm for finding the natural logarithm of a big number?
How to get the SMILES of all compounds on PubChem?
"Table" method for expanding brackets vs "each term in the first bracket gets multiplied by each term in the second bracket"
Is it plausible that an interrupted Windows update can cause the motherboard to fail?
What is the design rationale for having armor and magic penetration mechanics?
Perform a predetermined set of operations on a large sequence
Printing a list as “a, b, c.” using PythonHow can I make my solution for Chef and Digits run faster?Cutting The Sticks“Angry Professor” Python implementationFind Watson's integerC++ implementation of Hackerrank's “Maximum Element in a Stack”Parallelogram ConnectivitySummed absolute difference of two arrayTLE in FOXLINGS SPOJCodeChef's Tree MEX (Minimum Excludant) challenge
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
$begingroup$
This is a programming challenge.
I have been given the following input:
$n$ where n is size of sequence
$k$ is an integer
- a sequence $A_0, A_1, A_2, ldots A_n-1$
I need to perform for each index from 0 to (k-1) inclusive
find a,b
where a = A[i % n] and b = A[n - (i % n) - 1]
then set A[i % n] = a XOR b
and output the final sequence, separated by spaces
The inputs are within the ranges:
$n leq 10^4$
$k leq 10^12$
$A_i leq 10^7$
I have applied the following naive approach
n,k=map(int,input().split())
arr=list(map(int,input().split())) #read input sequence and store it as list type
for i in range(k): #iterate over 0 to (k-1)
t=i%n #module of i wrt n
arr[t]=arr[t]^arr[n-(t)-1] #xor between two list elements and then set result to the ith list element
for i in arr:
print(i,end=" ") #print the final sequence
This code runs in 2 seconds when $K leq 10^6$, but it shows Time Limit exceeded for large test cases.
I am looking for an alternate suggestion for the problem
python programming-challenge time-limit-exceeded
New contributor
$endgroup$
|
show 1 more comment
$begingroup$
This is a programming challenge.
I have been given the following input:
$n$ where n is size of sequence
$k$ is an integer
- a sequence $A_0, A_1, A_2, ldots A_n-1$
I need to perform for each index from 0 to (k-1) inclusive
find a,b
where a = A[i % n] and b = A[n - (i % n) - 1]
then set A[i % n] = a XOR b
and output the final sequence, separated by spaces
The inputs are within the ranges:
$n leq 10^4$
$k leq 10^12$
$A_i leq 10^7$
I have applied the following naive approach
n,k=map(int,input().split())
arr=list(map(int,input().split())) #read input sequence and store it as list type
for i in range(k): #iterate over 0 to (k-1)
t=i%n #module of i wrt n
arr[t]=arr[t]^arr[n-(t)-1] #xor between two list elements and then set result to the ith list element
for i in arr:
print(i,end=" ") #print the final sequence
This code runs in 2 seconds when $K leq 10^6$, but it shows Time Limit exceeded for large test cases.
I am looking for an alternate suggestion for the problem
python programming-challenge time-limit-exceeded
New contributor
$endgroup$
$begingroup$
From which site comes this problem? Are you interested in alternative implementations or a review of the code provided? Please take a look at the help center before answering the latter and modify your question accordingly.
$endgroup$
– Mast
9 hours ago
$begingroup$
Link to problem : codechef.com/OCT19B/problems/MARM
$endgroup$
– U_S02199
9 hours ago
$begingroup$
I am looking for an alternative approach as i know my naive approach wouldn't work for large values of K
$endgroup$
– U_S02199
9 hours ago
2
$begingroup$
@Mast Not looking for the review of my code is against the very purpose of this site: Code Review :)
$endgroup$
– dfhwze
9 hours ago
3
$begingroup$
For a next question, I give you this link (codereview.stackexchange.com/help/on-topic). I hope we can avoid ping-ponging your next question between sites :)
$endgroup$
– dfhwze
9 hours ago
|
show 1 more comment
$begingroup$
This is a programming challenge.
I have been given the following input:
$n$ where n is size of sequence
$k$ is an integer
- a sequence $A_0, A_1, A_2, ldots A_n-1$
I need to perform for each index from 0 to (k-1) inclusive
find a,b
where a = A[i % n] and b = A[n - (i % n) - 1]
then set A[i % n] = a XOR b
and output the final sequence, separated by spaces
The inputs are within the ranges:
$n leq 10^4$
$k leq 10^12$
$A_i leq 10^7$
I have applied the following naive approach
n,k=map(int,input().split())
arr=list(map(int,input().split())) #read input sequence and store it as list type
for i in range(k): #iterate over 0 to (k-1)
t=i%n #module of i wrt n
arr[t]=arr[t]^arr[n-(t)-1] #xor between two list elements and then set result to the ith list element
for i in arr:
print(i,end=" ") #print the final sequence
This code runs in 2 seconds when $K leq 10^6$, but it shows Time Limit exceeded for large test cases.
I am looking for an alternate suggestion for the problem
python programming-challenge time-limit-exceeded
New contributor
$endgroup$
This is a programming challenge.
I have been given the following input:
$n$ where n is size of sequence
$k$ is an integer
- a sequence $A_0, A_1, A_2, ldots A_n-1$
I need to perform for each index from 0 to (k-1) inclusive
find a,b
where a = A[i % n] and b = A[n - (i % n) - 1]
then set A[i % n] = a XOR b
and output the final sequence, separated by spaces
The inputs are within the ranges:
$n leq 10^4$
$k leq 10^12$
$A_i leq 10^7$
I have applied the following naive approach
n,k=map(int,input().split())
arr=list(map(int,input().split())) #read input sequence and store it as list type
for i in range(k): #iterate over 0 to (k-1)
t=i%n #module of i wrt n
arr[t]=arr[t]^arr[n-(t)-1] #xor between two list elements and then set result to the ith list element
for i in arr:
print(i,end=" ") #print the final sequence
This code runs in 2 seconds when $K leq 10^6$, but it shows Time Limit exceeded for large test cases.
I am looking for an alternate suggestion for the problem
python programming-challenge time-limit-exceeded
python programming-challenge time-limit-exceeded
New contributor
New contributor
edited 8 hours ago
AJNeufeld
12.8k1 gold badge13 silver badges40 bronze badges
12.8k1 gold badge13 silver badges40 bronze badges
New contributor
asked 10 hours ago
U_S02199U_S02199
212 bronze badges
212 bronze badges
New contributor
New contributor
$begingroup$
From which site comes this problem? Are you interested in alternative implementations or a review of the code provided? Please take a look at the help center before answering the latter and modify your question accordingly.
$endgroup$
– Mast
9 hours ago
$begingroup$
Link to problem : codechef.com/OCT19B/problems/MARM
$endgroup$
– U_S02199
9 hours ago
$begingroup$
I am looking for an alternative approach as i know my naive approach wouldn't work for large values of K
$endgroup$
– U_S02199
9 hours ago
2
$begingroup$
@Mast Not looking for the review of my code is against the very purpose of this site: Code Review :)
$endgroup$
– dfhwze
9 hours ago
3
$begingroup$
For a next question, I give you this link (codereview.stackexchange.com/help/on-topic). I hope we can avoid ping-ponging your next question between sites :)
$endgroup$
– dfhwze
9 hours ago
|
show 1 more comment
$begingroup$
From which site comes this problem? Are you interested in alternative implementations or a review of the code provided? Please take a look at the help center before answering the latter and modify your question accordingly.
$endgroup$
– Mast
9 hours ago
$begingroup$
Link to problem : codechef.com/OCT19B/problems/MARM
$endgroup$
– U_S02199
9 hours ago
$begingroup$
I am looking for an alternative approach as i know my naive approach wouldn't work for large values of K
$endgroup$
– U_S02199
9 hours ago
2
$begingroup$
@Mast Not looking for the review of my code is against the very purpose of this site: Code Review :)
$endgroup$
– dfhwze
9 hours ago
3
$begingroup$
For a next question, I give you this link (codereview.stackexchange.com/help/on-topic). I hope we can avoid ping-ponging your next question between sites :)
$endgroup$
– dfhwze
9 hours ago
$begingroup$
From which site comes this problem? Are you interested in alternative implementations or a review of the code provided? Please take a look at the help center before answering the latter and modify your question accordingly.
$endgroup$
– Mast
9 hours ago
$begingroup$
From which site comes this problem? Are you interested in alternative implementations or a review of the code provided? Please take a look at the help center before answering the latter and modify your question accordingly.
$endgroup$
– Mast
9 hours ago
$begingroup$
Link to problem : codechef.com/OCT19B/problems/MARM
$endgroup$
– U_S02199
9 hours ago
$begingroup$
Link to problem : codechef.com/OCT19B/problems/MARM
$endgroup$
– U_S02199
9 hours ago
$begingroup$
I am looking for an alternative approach as i know my naive approach wouldn't work for large values of K
$endgroup$
– U_S02199
9 hours ago
$begingroup$
I am looking for an alternative approach as i know my naive approach wouldn't work for large values of K
$endgroup$
– U_S02199
9 hours ago
2
2
$begingroup$
@Mast Not looking for the review of my code is against the very purpose of this site: Code Review :)
$endgroup$
– dfhwze
9 hours ago
$begingroup$
@Mast Not looking for the review of my code is against the very purpose of this site: Code Review :)
$endgroup$
– dfhwze
9 hours ago
3
3
$begingroup$
For a next question, I give you this link (codereview.stackexchange.com/help/on-topic). I hope we can avoid ping-ponging your next question between sites :)
$endgroup$
– dfhwze
9 hours ago
$begingroup$
For a next question, I give you this link (codereview.stackexchange.com/help/on-topic). I hope we can avoid ping-ponging your next question between sites :)
$endgroup$
– dfhwze
9 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
$begingroup$
Now that that pesky "Not looking for the review of my code" is gone...
Step 1: White space
Follow the PEP 8 guidelines, specifically (but not limited to) put a space around operators and after commas:
n, k = map(int, input().split())
arr = list(map(int, input().split())) # read input sequence and store it as list type
for i in range(k): # iterate over 0 to (k-1)
t = i % n # module of i wrt n
arr[t] = arr[t] ^ arr[n - (t) - 1] # xor between two list elements and then set result to the ith list element
for i in arr:
print(i, end=" ") # print the final sequence
Much easier to read.
Step 2: Avoid multiple lookups
Python is an interpreted language, and the meaning of a line of code -- or even a fragment of code -- can change by the time the interpreter returns to execute the code as second time. This means the interpreter cannot truly compile the code; unless something is a well defined short-circuiting operation, every operation must be executed.
Consider:
arr[t] = arr[t] ^ arr[n - (t) - 1]
The interpreter must compute the address of arr[t]
twice; once to fetch the value, and a second time to store the new value, because some side-effect which occurs during the execution of arr[n - (t) - 1]
may change the meaning of arr[t]
. In your case, arr
is a list
, and n
and t
are simple integers, but with user-defined types, anything can happen. As such, the Python interpreter can never make the following optimization:
arr[t] ^= arr[n - (t) - 1]
It is a tiny speed-up, but considering the code may execute $10^12$ times, it can add up.
Step 3: Avoid calculations
Speaking of avoiding work: because we know the length of the array is fixed, arr[n - 1]
is the same as arr[-1]
. So we can further speed up the line of code as follows:
arr[t] ^= arr[-1 - t]
Instead of two subtractions, we now have only one. Yes, Python has to index from the back of the array, which internally is going to involve a subtraction, BUT that will be an optimized, C-coded subtraction operation on ssize_t
values, instead of subtraction on variable byte length integers, which must be allocated and deallocated from the heap.
Step 4: Printing space-separated lists
The following is slow:
for i in arr:
print(i, end=" ")
This is faster:
print(*arr)
And for long lists, this may be fastest:
print(" ".join(map(str, arr)))
For a detail discussion, including timing charts, see my answer and this answer on another question.
Step 5: The Algorithm
Consider the list [A, B, C, D, E]
.
After applying a single pass of the operation on it (ie, k = n
), you'll get:
[A^E, B^D, C^C, D^(B^D), E^(A^E)]
which simplifies to:
[A^E, B^D, 0, B, A]
If we apply a second pass (ie, k = 2*n
), you'll get:
[(A^E)^A, (B^D)^B, 0^0, B^((B^D)^B), A^((A^E)^A)]
which simplifies to:
[E, D, 0, B^D, A^E]
A third pass, (ie, k = 3*n
) gives:
[E^(A^E), D^(B^D), 0^0, (B^D)^(D^(B^D)), (A^E)^(E^(A^E))]
or:
[A, B, 0, D, E]
Now k
does not need to be an exact multiple of n
, so you'll have to figure out what to do in the general cases, but you should be able to use the above observation to eliminate a lot of unnecessary calculations.
Implementation left to student.
$endgroup$
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
add a comment
|
$begingroup$
Any solution which does something $k$ times will be hopelessly slow. We have to make use of the structure of the problem here. Suppose $i<lfloor n/2rfloor$ and write $ a = A_i $ and $b= A_n-i$. Then each time through the list we execute $ a leftarrow awedge b $ and then $bleftarrow awedge b$. Lets record the values of $a $ and $b$ on each iteration of the loop, starting with values $x$ and $ y$ for $a$ and $ b$, respectively:
- First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$
- First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$
- First $a leftarrow y wedge (ywedge x) = x$, then $bleftarrow x wedge (ywedge x) = y$
Notice that the effect of three such iterations is to do nothing at all. Therefore we can subtract any multiple of $ 3n$ from $k$ and not change the result. If you reduce $k$ mod $3 n$ before executing your original code, you should be good. (Actually we have to be a little careful to handle the case of odd $n$, because the middle element has to be handled specially. I leave this as an exercise).
New contributor
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
U_S02199 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230272%2fperform-a-predetermined-set-of-operations-on-a-large-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Now that that pesky "Not looking for the review of my code" is gone...
Step 1: White space
Follow the PEP 8 guidelines, specifically (but not limited to) put a space around operators and after commas:
n, k = map(int, input().split())
arr = list(map(int, input().split())) # read input sequence and store it as list type
for i in range(k): # iterate over 0 to (k-1)
t = i % n # module of i wrt n
arr[t] = arr[t] ^ arr[n - (t) - 1] # xor between two list elements and then set result to the ith list element
for i in arr:
print(i, end=" ") # print the final sequence
Much easier to read.
Step 2: Avoid multiple lookups
Python is an interpreted language, and the meaning of a line of code -- or even a fragment of code -- can change by the time the interpreter returns to execute the code as second time. This means the interpreter cannot truly compile the code; unless something is a well defined short-circuiting operation, every operation must be executed.
Consider:
arr[t] = arr[t] ^ arr[n - (t) - 1]
The interpreter must compute the address of arr[t]
twice; once to fetch the value, and a second time to store the new value, because some side-effect which occurs during the execution of arr[n - (t) - 1]
may change the meaning of arr[t]
. In your case, arr
is a list
, and n
and t
are simple integers, but with user-defined types, anything can happen. As such, the Python interpreter can never make the following optimization:
arr[t] ^= arr[n - (t) - 1]
It is a tiny speed-up, but considering the code may execute $10^12$ times, it can add up.
Step 3: Avoid calculations
Speaking of avoiding work: because we know the length of the array is fixed, arr[n - 1]
is the same as arr[-1]
. So we can further speed up the line of code as follows:
arr[t] ^= arr[-1 - t]
Instead of two subtractions, we now have only one. Yes, Python has to index from the back of the array, which internally is going to involve a subtraction, BUT that will be an optimized, C-coded subtraction operation on ssize_t
values, instead of subtraction on variable byte length integers, which must be allocated and deallocated from the heap.
Step 4: Printing space-separated lists
The following is slow:
for i in arr:
print(i, end=" ")
This is faster:
print(*arr)
And for long lists, this may be fastest:
print(" ".join(map(str, arr)))
For a detail discussion, including timing charts, see my answer and this answer on another question.
Step 5: The Algorithm
Consider the list [A, B, C, D, E]
.
After applying a single pass of the operation on it (ie, k = n
), you'll get:
[A^E, B^D, C^C, D^(B^D), E^(A^E)]
which simplifies to:
[A^E, B^D, 0, B, A]
If we apply a second pass (ie, k = 2*n
), you'll get:
[(A^E)^A, (B^D)^B, 0^0, B^((B^D)^B), A^((A^E)^A)]
which simplifies to:
[E, D, 0, B^D, A^E]
A third pass, (ie, k = 3*n
) gives:
[E^(A^E), D^(B^D), 0^0, (B^D)^(D^(B^D)), (A^E)^(E^(A^E))]
or:
[A, B, 0, D, E]
Now k
does not need to be an exact multiple of n
, so you'll have to figure out what to do in the general cases, but you should be able to use the above observation to eliminate a lot of unnecessary calculations.
Implementation left to student.
$endgroup$
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
add a comment
|
$begingroup$
Now that that pesky "Not looking for the review of my code" is gone...
Step 1: White space
Follow the PEP 8 guidelines, specifically (but not limited to) put a space around operators and after commas:
n, k = map(int, input().split())
arr = list(map(int, input().split())) # read input sequence and store it as list type
for i in range(k): # iterate over 0 to (k-1)
t = i % n # module of i wrt n
arr[t] = arr[t] ^ arr[n - (t) - 1] # xor between two list elements and then set result to the ith list element
for i in arr:
print(i, end=" ") # print the final sequence
Much easier to read.
Step 2: Avoid multiple lookups
Python is an interpreted language, and the meaning of a line of code -- or even a fragment of code -- can change by the time the interpreter returns to execute the code as second time. This means the interpreter cannot truly compile the code; unless something is a well defined short-circuiting operation, every operation must be executed.
Consider:
arr[t] = arr[t] ^ arr[n - (t) - 1]
The interpreter must compute the address of arr[t]
twice; once to fetch the value, and a second time to store the new value, because some side-effect which occurs during the execution of arr[n - (t) - 1]
may change the meaning of arr[t]
. In your case, arr
is a list
, and n
and t
are simple integers, but with user-defined types, anything can happen. As such, the Python interpreter can never make the following optimization:
arr[t] ^= arr[n - (t) - 1]
It is a tiny speed-up, but considering the code may execute $10^12$ times, it can add up.
Step 3: Avoid calculations
Speaking of avoiding work: because we know the length of the array is fixed, arr[n - 1]
is the same as arr[-1]
. So we can further speed up the line of code as follows:
arr[t] ^= arr[-1 - t]
Instead of two subtractions, we now have only one. Yes, Python has to index from the back of the array, which internally is going to involve a subtraction, BUT that will be an optimized, C-coded subtraction operation on ssize_t
values, instead of subtraction on variable byte length integers, which must be allocated and deallocated from the heap.
Step 4: Printing space-separated lists
The following is slow:
for i in arr:
print(i, end=" ")
This is faster:
print(*arr)
And for long lists, this may be fastest:
print(" ".join(map(str, arr)))
For a detail discussion, including timing charts, see my answer and this answer on another question.
Step 5: The Algorithm
Consider the list [A, B, C, D, E]
.
After applying a single pass of the operation on it (ie, k = n
), you'll get:
[A^E, B^D, C^C, D^(B^D), E^(A^E)]
which simplifies to:
[A^E, B^D, 0, B, A]
If we apply a second pass (ie, k = 2*n
), you'll get:
[(A^E)^A, (B^D)^B, 0^0, B^((B^D)^B), A^((A^E)^A)]
which simplifies to:
[E, D, 0, B^D, A^E]
A third pass, (ie, k = 3*n
) gives:
[E^(A^E), D^(B^D), 0^0, (B^D)^(D^(B^D)), (A^E)^(E^(A^E))]
or:
[A, B, 0, D, E]
Now k
does not need to be an exact multiple of n
, so you'll have to figure out what to do in the general cases, but you should be able to use the above observation to eliminate a lot of unnecessary calculations.
Implementation left to student.
$endgroup$
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
add a comment
|
$begingroup$
Now that that pesky "Not looking for the review of my code" is gone...
Step 1: White space
Follow the PEP 8 guidelines, specifically (but not limited to) put a space around operators and after commas:
n, k = map(int, input().split())
arr = list(map(int, input().split())) # read input sequence and store it as list type
for i in range(k): # iterate over 0 to (k-1)
t = i % n # module of i wrt n
arr[t] = arr[t] ^ arr[n - (t) - 1] # xor between two list elements and then set result to the ith list element
for i in arr:
print(i, end=" ") # print the final sequence
Much easier to read.
Step 2: Avoid multiple lookups
Python is an interpreted language, and the meaning of a line of code -- or even a fragment of code -- can change by the time the interpreter returns to execute the code as second time. This means the interpreter cannot truly compile the code; unless something is a well defined short-circuiting operation, every operation must be executed.
Consider:
arr[t] = arr[t] ^ arr[n - (t) - 1]
The interpreter must compute the address of arr[t]
twice; once to fetch the value, and a second time to store the new value, because some side-effect which occurs during the execution of arr[n - (t) - 1]
may change the meaning of arr[t]
. In your case, arr
is a list
, and n
and t
are simple integers, but with user-defined types, anything can happen. As such, the Python interpreter can never make the following optimization:
arr[t] ^= arr[n - (t) - 1]
It is a tiny speed-up, but considering the code may execute $10^12$ times, it can add up.
Step 3: Avoid calculations
Speaking of avoiding work: because we know the length of the array is fixed, arr[n - 1]
is the same as arr[-1]
. So we can further speed up the line of code as follows:
arr[t] ^= arr[-1 - t]
Instead of two subtractions, we now have only one. Yes, Python has to index from the back of the array, which internally is going to involve a subtraction, BUT that will be an optimized, C-coded subtraction operation on ssize_t
values, instead of subtraction on variable byte length integers, which must be allocated and deallocated from the heap.
Step 4: Printing space-separated lists
The following is slow:
for i in arr:
print(i, end=" ")
This is faster:
print(*arr)
And for long lists, this may be fastest:
print(" ".join(map(str, arr)))
For a detail discussion, including timing charts, see my answer and this answer on another question.
Step 5: The Algorithm
Consider the list [A, B, C, D, E]
.
After applying a single pass of the operation on it (ie, k = n
), you'll get:
[A^E, B^D, C^C, D^(B^D), E^(A^E)]
which simplifies to:
[A^E, B^D, 0, B, A]
If we apply a second pass (ie, k = 2*n
), you'll get:
[(A^E)^A, (B^D)^B, 0^0, B^((B^D)^B), A^((A^E)^A)]
which simplifies to:
[E, D, 0, B^D, A^E]
A third pass, (ie, k = 3*n
) gives:
[E^(A^E), D^(B^D), 0^0, (B^D)^(D^(B^D)), (A^E)^(E^(A^E))]
or:
[A, B, 0, D, E]
Now k
does not need to be an exact multiple of n
, so you'll have to figure out what to do in the general cases, but you should be able to use the above observation to eliminate a lot of unnecessary calculations.
Implementation left to student.
$endgroup$
Now that that pesky "Not looking for the review of my code" is gone...
Step 1: White space
Follow the PEP 8 guidelines, specifically (but not limited to) put a space around operators and after commas:
n, k = map(int, input().split())
arr = list(map(int, input().split())) # read input sequence and store it as list type
for i in range(k): # iterate over 0 to (k-1)
t = i % n # module of i wrt n
arr[t] = arr[t] ^ arr[n - (t) - 1] # xor between two list elements and then set result to the ith list element
for i in arr:
print(i, end=" ") # print the final sequence
Much easier to read.
Step 2: Avoid multiple lookups
Python is an interpreted language, and the meaning of a line of code -- or even a fragment of code -- can change by the time the interpreter returns to execute the code as second time. This means the interpreter cannot truly compile the code; unless something is a well defined short-circuiting operation, every operation must be executed.
Consider:
arr[t] = arr[t] ^ arr[n - (t) - 1]
The interpreter must compute the address of arr[t]
twice; once to fetch the value, and a second time to store the new value, because some side-effect which occurs during the execution of arr[n - (t) - 1]
may change the meaning of arr[t]
. In your case, arr
is a list
, and n
and t
are simple integers, but with user-defined types, anything can happen. As such, the Python interpreter can never make the following optimization:
arr[t] ^= arr[n - (t) - 1]
It is a tiny speed-up, but considering the code may execute $10^12$ times, it can add up.
Step 3: Avoid calculations
Speaking of avoiding work: because we know the length of the array is fixed, arr[n - 1]
is the same as arr[-1]
. So we can further speed up the line of code as follows:
arr[t] ^= arr[-1 - t]
Instead of two subtractions, we now have only one. Yes, Python has to index from the back of the array, which internally is going to involve a subtraction, BUT that will be an optimized, C-coded subtraction operation on ssize_t
values, instead of subtraction on variable byte length integers, which must be allocated and deallocated from the heap.
Step 4: Printing space-separated lists
The following is slow:
for i in arr:
print(i, end=" ")
This is faster:
print(*arr)
And for long lists, this may be fastest:
print(" ".join(map(str, arr)))
For a detail discussion, including timing charts, see my answer and this answer on another question.
Step 5: The Algorithm
Consider the list [A, B, C, D, E]
.
After applying a single pass of the operation on it (ie, k = n
), you'll get:
[A^E, B^D, C^C, D^(B^D), E^(A^E)]
which simplifies to:
[A^E, B^D, 0, B, A]
If we apply a second pass (ie, k = 2*n
), you'll get:
[(A^E)^A, (B^D)^B, 0^0, B^((B^D)^B), A^((A^E)^A)]
which simplifies to:
[E, D, 0, B^D, A^E]
A third pass, (ie, k = 3*n
) gives:
[E^(A^E), D^(B^D), 0^0, (B^D)^(D^(B^D)), (A^E)^(E^(A^E))]
or:
[A, B, 0, D, E]
Now k
does not need to be an exact multiple of n
, so you'll have to figure out what to do in the general cases, but you should be able to use the above observation to eliminate a lot of unnecessary calculations.
Implementation left to student.
answered 7 hours ago
AJNeufeldAJNeufeld
12.8k1 gold badge13 silver badges40 bronze badges
12.8k1 gold badge13 silver badges40 bronze badges
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
add a comment
|
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
$begingroup$
One of the potential offenders is also the mod operation. It might (or might not) be faster to use an increment with an if after that.
$endgroup$
– Ilkhd
4 hours ago
add a comment
|
$begingroup$
Any solution which does something $k$ times will be hopelessly slow. We have to make use of the structure of the problem here. Suppose $i<lfloor n/2rfloor$ and write $ a = A_i $ and $b= A_n-i$. Then each time through the list we execute $ a leftarrow awedge b $ and then $bleftarrow awedge b$. Lets record the values of $a $ and $b$ on each iteration of the loop, starting with values $x$ and $ y$ for $a$ and $ b$, respectively:
- First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$
- First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$
- First $a leftarrow y wedge (ywedge x) = x$, then $bleftarrow x wedge (ywedge x) = y$
Notice that the effect of three such iterations is to do nothing at all. Therefore we can subtract any multiple of $ 3n$ from $k$ and not change the result. If you reduce $k$ mod $3 n$ before executing your original code, you should be good. (Actually we have to be a little careful to handle the case of odd $n$, because the middle element has to be handled specially. I leave this as an exercise).
New contributor
$endgroup$
add a comment
|
$begingroup$
Any solution which does something $k$ times will be hopelessly slow. We have to make use of the structure of the problem here. Suppose $i<lfloor n/2rfloor$ and write $ a = A_i $ and $b= A_n-i$. Then each time through the list we execute $ a leftarrow awedge b $ and then $bleftarrow awedge b$. Lets record the values of $a $ and $b$ on each iteration of the loop, starting with values $x$ and $ y$ for $a$ and $ b$, respectively:
- First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$
- First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$
- First $a leftarrow y wedge (ywedge x) = x$, then $bleftarrow x wedge (ywedge x) = y$
Notice that the effect of three such iterations is to do nothing at all. Therefore we can subtract any multiple of $ 3n$ from $k$ and not change the result. If you reduce $k$ mod $3 n$ before executing your original code, you should be good. (Actually we have to be a little careful to handle the case of odd $n$, because the middle element has to be handled specially. I leave this as an exercise).
New contributor
$endgroup$
add a comment
|
$begingroup$
Any solution which does something $k$ times will be hopelessly slow. We have to make use of the structure of the problem here. Suppose $i<lfloor n/2rfloor$ and write $ a = A_i $ and $b= A_n-i$. Then each time through the list we execute $ a leftarrow awedge b $ and then $bleftarrow awedge b$. Lets record the values of $a $ and $b$ on each iteration of the loop, starting with values $x$ and $ y$ for $a$ and $ b$, respectively:
- First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$
- First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$
- First $a leftarrow y wedge (ywedge x) = x$, then $bleftarrow x wedge (ywedge x) = y$
Notice that the effect of three such iterations is to do nothing at all. Therefore we can subtract any multiple of $ 3n$ from $k$ and not change the result. If you reduce $k$ mod $3 n$ before executing your original code, you should be good. (Actually we have to be a little careful to handle the case of odd $n$, because the middle element has to be handled specially. I leave this as an exercise).
New contributor
$endgroup$
Any solution which does something $k$ times will be hopelessly slow. We have to make use of the structure of the problem here. Suppose $i<lfloor n/2rfloor$ and write $ a = A_i $ and $b= A_n-i$. Then each time through the list we execute $ a leftarrow awedge b $ and then $bleftarrow awedge b$. Lets record the values of $a $ and $b$ on each iteration of the loop, starting with values $x$ and $ y$ for $a$ and $ b$, respectively:
- First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$
- First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$
- First $a leftarrow y wedge (ywedge x) = x$, then $bleftarrow x wedge (ywedge x) = y$
Notice that the effect of three such iterations is to do nothing at all. Therefore we can subtract any multiple of $ 3n$ from $k$ and not change the result. If you reduce $k$ mod $3 n$ before executing your original code, you should be good. (Actually we have to be a little careful to handle the case of odd $n$, because the middle element has to be handled specially. I leave this as an exercise).
New contributor
New contributor
answered 48 mins ago
vujazzmanvujazzman
1
1
New contributor
New contributor
add a comment
|
add a comment
|
U_S02199 is a new contributor. Be nice, and check out our Code of Conduct.
U_S02199 is a new contributor. Be nice, and check out our Code of Conduct.
U_S02199 is a new contributor. Be nice, and check out our Code of Conduct.
U_S02199 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230272%2fperform-a-predetermined-set-of-operations-on-a-large-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
From which site comes this problem? Are you interested in alternative implementations or a review of the code provided? Please take a look at the help center before answering the latter and modify your question accordingly.
$endgroup$
– Mast
9 hours ago
$begingroup$
Link to problem : codechef.com/OCT19B/problems/MARM
$endgroup$
– U_S02199
9 hours ago
$begingroup$
I am looking for an alternative approach as i know my naive approach wouldn't work for large values of K
$endgroup$
– U_S02199
9 hours ago
2
$begingroup$
@Mast Not looking for the review of my code is against the very purpose of this site: Code Review :)
$endgroup$
– dfhwze
9 hours ago
3
$begingroup$
For a next question, I give you this link (codereview.stackexchange.com/help/on-topic). I hope we can avoid ping-ponging your next question between sites :)
$endgroup$
– dfhwze
9 hours ago