r/ProgrammerHumor • • Jun 06 '20

It's the law!

Post image
38k Upvotes

1.1k comments sorted by

View all comments

Show parent comment

135

u/_zsa Jun 06 '20

If you nest 4 for loops you should apologize and retire.

35

u/xorgol Jun 06 '20

My defense to all criticism is that the choices were driven by domain-specific issues. It's not necessarily true, but it sure shuts people up :D

14

u/splendidsplinter Jun 06 '20

just claim it's a heuristic

2

u/MattTheGr8 Jun 09 '20

It’s the case in my domain. We do neuroimaging research and our datasets often have 5, 6, 7 dimensions or more, depending on what we’re trying to do. And if you’re trying to do the same thing to every element in a 5-D matrix (and it’s not something that can be vectorized), there’s not much else to do but write five nested loops.

9

u/why_rob_y Jun 06 '20

Or write a sequel to Primer.

3

u/Steeped_In_Folly Jun 06 '20

If anyone ever is considering getting into coding, show them Primer and if they love it, they’ll love programming.

→ More replies (2)

25

u/LonelyContext Jun 06 '20

My record is 26 in solving ProjectEuler #96:

import extended as xt
import numpy as np
from tqdm import tqdm
import sympy as sp
sum([int(''.join([str(z) for z in y[0,:3]])) for y in [xt.nest(lambda f: [[np.vstack([g[:f[1]],[[w[x] for x in range(9)]],g[f[1]+1:]]) for g in f[0] for l in [np.array([g[:3,:3],g[:3,3:6],g[:3,6:],g[3:6,:3],g[3:6,3:6],g[3:6,6:],g[6:,:3],g[6:,3:6],g[6:,6:]]).reshape([3,3,9])] for p in [[h([k,l[f[1]//3, m//3],g[f[1]]]) for m,k in enumerate(g.T)]] for q in [[n for n,o in enumerate(g[f[1]]) if o==0]] for s in [[n for n,o in enumerate(g[f[1]]) if o!=0]] for t in sp.utilities.iterables.cartes(*[p[r] for r in q]) for w in [{**dict(zip(q,t)),**{u:g[f[1]][u] for u in s}}] if len(t)==len(np.unique(t))], f[1]+1],([[e],0]),9)[0][0] for h in [lambda i: [j for j in range(1,10) if j not in sp.utilities.iterables.flatten(i)]] for a in [open('sudoku.txt').read()+'\n'] for b in tqdm(a.split('Grid')[1:],position=0,leave=True) for e in [np.array([[int(d) for d in c] for c in b.split('\n')[1:-1]])]]])

40

u/BattlePope Jun 06 '20

You've managed to make python ugly

9

u/Rellac_ Jun 06 '20

This is what most people see when they look at normal code

→ More replies (1)

0

u/[deleted] Jun 06 '20

[deleted] — view deleted comment

6

u/JuniorSeniorTrainee Jun 06 '20

But look how compact it is! 🙄

11

u/JohnDoen86 Jun 06 '20

That is terrifying

6

u/BattlePope Jun 06 '20

It's brutality

8

u/deljaroo Jun 06 '20

omg surely you could have done that in separate lines

8

u/coldnebo Jun 06 '20

“oh no, vectorization sounds too complicated, I’ll just write some loops”

8

u/HodortheGreat Jun 06 '20

Eli5 vectorization vs loops for a newbie? :)

4

u/coldnebo Jun 06 '20

Say you have an apple orchard full of trees with ripe apples on them.

nested loops: I’m going to pick each apple one at a time from this tree, then go to the next tree and pick those apples, and so on until I’ve picked all the apples.

vectorization: I’m going to hire one person for every tree and ask them to pick all the apples in parallel and put them in the truck when they are done.

2

u/ThePretzul Jun 06 '20

Loops are easy, vectors are slightly less easy. Programmers get pissy when they see loops because vector look fancier.

2

u/MattTheGr8 Jun 09 '20

I work mostly in Matlab which is already heavily vectorized, but sometimes you are working with enormous multi-dimensional datasets and what you’re doing to each element of them can’t be easily vectorized. For example, it’s not uncommon for me to be dealing with a 4- or 5-D matrix of data, where each element of the matrix is itself a 2-D or 3-D matrix. So you can vectorize some of the operations you’re doing on the innermost elements, but if you’re running through the entire dataset, nested loops it is!

2

u/Minimum_T-Giraff Jun 06 '20

I NEED MY NESTED LOOPS

2

u/MangoCats Jun 06 '20

When developing a single histogram of data collected in a 4 dimensional space....

→ More replies (1)

2

u/clever_cow Jun 06 '20

I mean if you absolutely had to nest 4 or more loops for some odd reason you should just call another function with the loops in it and do i,j,k, again. You get that deep nested in for loops your code will start to spaghettify.

→ More replies (4)

1

u/seetadat Jun 06 '20

I did that just the other day and then worked it back down to 2. Sometimes you have to take 2 loops forward to take 2 loops back.

→ More replies (1)

1

u/MattTheGr8 Jun 09 '20

With all due respect, I’m guessing you don’t work with a lot of multi-dimensional data. Most of our stuff is done in Matlab and we are frequently working with datasets that have five or more dimensions. Nothing wrong with writing this:

for i = 1:n_i
  for j = 1:n_j
    for k = 1:n_k
      for m = 1:n_m
        do_some_function( my_mega_matrix{i,j,k,m} );
      end
    end
  end
end

There’s not much of a cleaner way to write it in cases like this. If you bury some of that in a sub-function, the semantics don’t really make sense and it adds lines of code unnecessarily. And before you ask, this would be for something that can’t be vectorized.