How to write this algorithm in python code?

I have the following code.

for k in range( ( N + 1 ) * K ): if k >= 0 and k <= K-1: # do something # n = 0 elif k >= K and k <= 2*K-1: # do something # n = 1 elif k >= 2*K and k <= 3*K-1: # do something # n = 2 ... ... 

β€œDoing something” is hard to explain, but I replaced it with the affectation n = p.

How can I write this explicitly?

More specifically, if k is in the set {p * K, ..., (p + 1) * K-1} for p = 0 to N, then do something. How can I do this in code?

+5
source share
2 answers
 for loop_id in xrange(N): for i in xrange(K): k = K * loop_id + i do_smth(loop_id, i, k) 
+3
source

You can only have three loops, no?

 for k in range(K): # do something for k in range(K, 2*K-1): # do something for k in range(2*K-1, (N+1)*K): # do the rest 
+5
source

Source: https://habr.com/ru/post/1239529/


All Articles