Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
added 3 characters in body
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the rowrows and one offor the columns, it's usually shorter to usewrite a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
  do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be shorter to assign their values i=k/n, j=k%n inside the loop.

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's usually shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
  do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be shorter to assign their values i=k/n, j=k%n inside the loop.

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the rows and one for the columns, it's usually shorter to write a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
 do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be shorter to assign their values i=k/n, j=k%n inside the loop.

faster? since when do we care about fast in code golf? ;)
Source Link
pxeger
  • 25.3k
  • 4
  • 59
  • 146

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's usually shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
  do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be fastershorter to assign their values i=k/n, j=k%n inside the loop.

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's usually shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
  do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be faster to assign their values i=k/n, j=k%n inside the loop.

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's usually shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
  do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be shorter to assign their values i=k/n, j=k%n inside the loop.

deleted 58 characters in body
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676

CollapsingCollapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's oftenusually shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:Original code:

  
for i in range(m):
 for j in range(n):
  DoStuffdo_stuff(i,j)

Golfed code:Golfed code:

for k in range(m*n):
  DoStuffdo_stuff(k/n,k%n)

In effect, you're iterating over the cartesianCartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. Note that theThe order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be faster to assign their values i=k/n, j=k%n inside the loop.

Collapsing two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's often shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

 
for i in range(m):
 for j in range(n):
  DoStuff(i,j)

Golfed code:

for k in range(m*n):
  DoStuff(k/n,k%n)

In effect, you're iterating over the cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. Note that the order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be faster to assign their values i=k/n, j=k%n inside the loop.

Collapse two numerical loops into one

Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one for the row and one of the columns, it's usually shorter to use a single loop to iterate over the m*n cells of the grid. You can extract the row and column of the cell inside the loop.

Original code:

 
for i in range(m):
 for j in range(n):
  do_stuff(i,j)

Golfed code:

for k in range(m*n):
  do_stuff(k/n,k%n)

In effect, you're iterating over the Cartesian product of the two ranges, encoding the pair (i,j) as x=i*n+j. You've save a costly range call and a level of indentation inside the loop. The order of iteration is unchanged.

Use // instead of / in Python 3. If you refer to i and j many times, it may be faster to assign their values i=k/n, j=k%n inside the loop.

Mod Removes Wiki by Doorknob
added 1 character in body
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676
Loading
added 3 characters in body
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676
Loading
Source Link
xnor
  • 149.6k
  • 26
  • 287
  • 676
Loading