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;









2














$begingroup$


This is a programming challenge.



I have been given the following input:





  1. $n$ where n is size of sequence


  2. $k$ is an integer

  3. 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










share|improve this question









New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$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


















2














$begingroup$


This is a programming challenge.



I have been given the following input:





  1. $n$ where n is size of sequence


  2. $k$ is an integer

  3. 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










share|improve this question









New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$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














2












2








2





$begingroup$


This is a programming challenge.



I have been given the following input:





  1. $n$ where n is size of sequence


  2. $k$ is an integer

  3. 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










share|improve this question









New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




This is a programming challenge.



I have been given the following input:





  1. $n$ where n is size of sequence


  2. $k$ is an integer

  3. 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






share|improve this question









New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question



share|improve this question








edited 8 hours ago









AJNeufeld

12.8k1 gold badge13 silver badges40 bronze badges




12.8k1 gold badge13 silver badges40 bronze badges






New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 10 hours ago









U_S02199U_S02199

212 bronze badges




212 bronze badges




New contributor



U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




U_S02199 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • $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$
    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











2 Answers
2






active

oldest

votes


















3
















$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.






share|improve this answer










$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


















0
















$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:



  1. First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$

  2. First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$

  3. 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).






share|improve this answer









New contributor



vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





$endgroup$
















    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.









    draft saved

    draft discarded
















    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









    3
















    $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.






    share|improve this answer










    $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















    3
















    $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.






    share|improve this answer










    $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













    3














    3










    3







    $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.






    share|improve this answer










    $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.







    share|improve this answer













    share|improve this answer




    share|improve this answer



    share|improve this answer










    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
















    • $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













    0
















    $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:



    1. First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$

    2. First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$

    3. 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).






    share|improve this answer









    New contributor



    vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    $endgroup$



















      0
















      $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:



      1. First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$

      2. First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$

      3. 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).






      share|improve this answer









      New contributor



      vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      $endgroup$

















        0














        0










        0







        $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:



        1. First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$

        2. First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$

        3. 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).






        share|improve this answer









        New contributor



        vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        $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:



        1. First $a leftarrow xwedge y$, then $b leftarrow (xwedge y)wedge y = x$

        2. First $a leftarrow (xwedge y) wedge x = y$, then $bleftarrow ywedge x$

        3. 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).







        share|improve this answer









        New contributor



        vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        share|improve this answer




        share|improve this answer



        share|improve this answer






        New contributor



        vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.








        answered 48 mins ago









        vujazzmanvujazzman

        1




        1




        New contributor



        vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




        New contributor




        vujazzman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.


























            U_S02199 is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded

















            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.




            draft saved


            draft discarded














            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





















































            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









            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單