1.8k
u/Anyhowtransfer554 Oct 18 '21
What knd of monster uses j instead of i in a for loop??
748
u/youngrandpa Oct 18 '21
When it’s a for loop inside of another for loop
302
u/GalaxyGamingBoy Oct 18 '21
Simple, ii
153
u/ShootyFaceMc Oct 18 '21
Ok but what if there's a loop inside a loop inside a loop
190
u/GalaxyGamingBoy Oct 18 '21
iii, What did you expect?
290
u/ShootyFaceMc Oct 18 '21
Yeah but if there's 4 is it iv or iiii
152
u/GalaxyGamingBoy Oct 18 '21
Great question! But tbh i don't know. Having 4 nested for loops seems unnecessary
92
u/Ning1253 Oct 18 '21
Ok but what if you are working with a 4d complex function input output dataset
Or like quaternions or some sh*t
Then what
221
→ More replies (9)43
u/FireElemental99 Oct 18 '21
A real programmer uses one loop and then splits the index into 4 arguments. 4 nested loops are never needed
→ More replies (19)→ More replies (3)15
6
u/DrMobius0 Oct 18 '21
Are you asking if you should or shouldn't use roman numerals when given an excuse to?
6
u/nataniel_rg Oct 19 '21
First loop is 0 so "i" for iteration. Second loop is loop 1 so "ii" ("i1"). Third is 2 so it's going to be "iii" (meaning "iteration2"). Taking this logic the 4th one will actually be "iiii" and the 5th one would be "iiv".
Yup I just really spent time on this.
3
→ More replies (1)3
128
u/Ninder975 Oct 18 '21
Then you’re doing it wrong
→ More replies (1)29
u/bigmountainbig Oct 18 '21 edited Oct 18 '21
how is 3d data implemented without something like this? serious question.
→ More replies (4)8
5
5
u/nryhajlo Oct 18 '21
k, and if there are more than three dimensions, you better be using useful names for your index variables.
→ More replies (1)4
u/LoyalSage Oct 19 '21
public class Foo { public void doLoop(3DArray array, 3DPoint dimensions) { ArrayList<2DPoint> a = new ArrayList<2DPoint>(); ArrayList<3DPoint> b = new ArrayList<3DPoint>(); for (int i = 0; i < dimensions.x; i++) { for (int ii = 0; ii < dimensions.y; ii++) { a.add(new 2DPoint(i, ii)); } } for (2DPoint point : a) { for (int i = 0; i < dimensions.z; i++) { b.add(new 3DPoint(point.x, point.y, i)); } } for (3DPoint point : b) { int x = array.getValue(point); System.out.println(x); } } } public class 2DPoint { int x; int y; public 2DPoint(int x, int y) { this.x = x; this.y = y; } } public class 3DPoint { int x; int y; int z; public 3DPoint(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } } public class 3DArrayEntry { 3DPoint key; int value public 3DArrayEntry(3DPoint key, int value) { this.key = key; this.value = value; } } public class 3DArray { ArrayList<3DArrayEntry> values = new ArrayList<3DArrayEntry>(); public void setValue(3DPoint key, int value) { boolean didntFindIt = true; for (3DArrayEntry entry : values) { if (entry.key.x != key.x) { continue; } else { if (entry.key.y != key.y) { continue; } else { if (entry.key.z != key.z) { continue; } else { didntFindIt == false; entry.value = value; } } } } if (!didntFindIt) { return; } else { values.add(new 3DArrayEntry(key, point)); } } public int getValue(3DPoint key) throws ArrayIndexOutOfBoundsException { boolean didntFindIt = true; int value = 0; for (3DArrayEntry entry : values) { if (entry.key.x != key.x) { continue; } else { if (entry.key.y != key.y) { continue; } else { if (entry.key.z != key.z) { continue; } else { didntFindIt == false; value = entry.value; } } } } if (!didntFindIt) { return value; } else { throw new ArrayIndexOutOfBoundsException(); } } }2
6
2
u/jgeez Oct 18 '21
iii is over there like: I'm just a dude, playing a dude, disguised as another dude!
1
→ More replies (1)1
2
1
5
→ More replies (1)5
41
u/Frodojj Oct 18 '21
Electrical engineers. ;-)
4
u/wasdlmb Oct 18 '21
Is that because I is current? Wouldn't j still be reserved by the imaginary unit? Or is that not used in the same places
10
3
15
6
5
u/yakesadam Oct 18 '21
I had a co-worker try to defend using
jbecause everywhere in the code he wrote when he usedjhe was looping over the same array so "it would be clearer".Of course it was actually just copy-pasted and there was no reason at all.
3
1
1
1
1
1
1
1
1
1
u/DoctorPython Oct 19 '21
I used to use f
1
u/Paval1s Oct 19 '21
my teacher told me to use i - k and x - z (inclusive) There were no further instructions if I have more than 6 loops
1
379
u/nintendojunkie17 Oct 18 '21
In college, I had an assignment to write a simple tic-tac-toe app in WinForms. Initial requirements said to make it work for any size grid from 2x2 to 5x5.
During class a couple days before it was due, professor (accidentally) says 6x6 instead. One student raises his hand and points out that the assignment said up to 5x5. Professor says "well let's make it up to 6x6 just for fun."
Looking around the room, it became immediately apparent which students had hand-built separate UIs for each grid size and now had a long night ahead of them.
175
u/javon27 Oct 18 '21
That was no accident
38
u/grillmaster4u Oct 18 '21
I read that in the voice of Obi-Wan Kenobi “that’s no moon… it’s a battle station!”
122
u/Idixal Oct 18 '21
On the one hand, an asshole move from the professor. On the other hand, it’s a very valuable lesson that hopefully at least one or two students walked away with.
33
u/ButtererOfToast Oct 19 '21
Ah yes, the fun that is a customer changing their requirements.
8
u/Idixal Oct 19 '21
That is a valuable lesson too, but I meant more in regards to always designing your code in a way that allows it to be easily extended.
65
u/O12345678927 Oct 18 '21
How do you play tic tac toe on a 2x2 grid?
82
7
587
u/jzboi Oct 18 '21
Very efficient! Only one iteration!!!
40
u/B2EU Oct 18 '21
I don’t know shit about compilers, but if you wrote the for loop as intended, couldn’t the compiler optimize it to be O(1), assuming the for loop always produces the same result?
26
8
u/bazingaa73 Oct 18 '21
Unfortunately I deleted my first comment so I will comment the same thing again: If the loop would always return the same result, then your loop can be evaluated at compile time. In C++ you can mark functions "constexpr" if all arguments and dependencies can be evaluated at compile time. However console output can not be "constexpr" since it prints stuff at runtime. So you would have to store your output in some array. It's really the same as hardcoding the output but more convenient.
→ More replies (3)4
131
u/freonblood Oct 18 '21
O(1) /s
25
u/Zyansheep Oct 18 '21
Take that /s away! It clearly did not specify that one was intended to print out a pattern. Only that a triangle was to be printed. /s /s
76
u/agentjob Oct 18 '21
Code generated by ML powered AI.
36
182
u/defalt86 Oct 18 '21
Now do it for 10,000 rows.
157
u/thelanman8000 Oct 18 '21
there has to be a way to make a program to write the print statements in a txt file and then just copy paste them
115
u/kiagam Oct 18 '21
It is called fivvr
199
u/thelanman8000 Oct 18 '21 edited Oct 18 '21
Okay so I paid a guy on fiveer to make this tool and here's what he gave me
with open("file.txt", "w"): for i in range(1000): write(f'System.out.println(\"{"i" * i} \n\"')He told me to run with a python (I don't have a python and I'm not even sure they can run) and i told him to go fuck himself and refunded the payment
58
17
Oct 18 '21
Just a question. What is the python icon next to your name and how can I get one?
14
Oct 18 '21
Its a flair. If you are on a phone just tap your pfp in the comment section of any post of the community and from there tap change user flair.
5
3
u/MagorTuga Oct 18 '21
I'm totally not replying because I don't remember if I've already done this and wanna see if it works.
→ More replies (1)6
u/seimmuc_ Oct 18 '21 edited Oct 18 '21
That'd just create an empty file then immediately throw an exception saying that "write" doesn't exist.
It'll just output an increasing number of character 'i' on each line. I think it'd be more appropriate to either output the whole number:
(str(i) + " "), or its last decimal digit:str(i % 10).It starts the loop at 0, which means the first line will be empty.
Here's the fix:
with open("file.txt", "w") as file_handle: for i in range(1, 1001): file_handle.write(f'System.out.println(\"{str(i % 10) * i} \n\"')Though just like your fiveer guy, I didn't bother actually running this code because I'm on my phone rn.
8
u/7eggert Oct 18 '21
System.out.println("#!/bin/sh\ncat<<EOF\n"+
… +
"EOF\n";
So you can run the output, too.
6
2
1
u/Faustamort Oct 18 '21
Enter =(A1*(10^(A1*(FLOOR.MATH(LOG(A1))+1))-1)/(10^((FLOOR.MATH(LOG(A1))+1))-1)) into excel, scroll to row 10,000.1
41
Oct 18 '21
In one of our company interviews, the question was to sort an array of numbers 1-10. Guy couldn't do it, couldn't figure it out. Our guy giving the interview said, "well at least write me code that will find the largest number".
The man thinks for a sec, and basically starts writing: "if number==10".
He wasn't hired.
14
u/tchukki Oct 19 '21
You can lie on you're résumé all you want but it's a lot harder to bullshit through an interview lmao
4
u/boowhitie Oct 19 '21
I once interviewed a guy who was having real trouble with my technical questions. I backed off a couple times to see where he was at, and finally stopped asking technical questions after he gave up trying to print the numbers 1 to 10, in any language or pseudocode, on the white board.
The guy got a thumbs down from all interviewers, except the guy who he was going to be working under. That guy was horribly overworked and over his head. He was a film major who somehow the primary developer of an as2 app which was hugely important to the company. He was desperate for help and hired the guy despite the poor interviews.
As expected, the guy couldn't do the job at all. Fortunately, he had the good sense to quit at the end of the first week.
3
u/Bakoro Oct 19 '21
And yet I know like 3 people with CS degrees who can't even get an interview or even just to the automated coding test stage except with companies that want to send them to Idaho or some shit....
The world's funny sometimes.
100
u/Dependent_Paper9993 Oct 18 '21
Well... The question said for loops... So a better implemtation would have been to write 5 for loops each going from i=0 to i<1 and then printing out the lines as needed.
43
u/kodosExecutioner Oct 18 '21
I mean... Thats the joke
But if we're already on the topic, you should write 2 loops, nested
The outer one is x going from 1 to 5 and the inner one goes from 1 to x, printing out x at each iteration. After the inner loop, a newline is called.
48
u/TheAJGman Oct 18 '21
One loop is all you need:
for i in range(1,6): print(str(i)*i)22
u/deprilula28 Oct 18 '21
I'm sure whatever string * number is doing has a for loop in there
19
u/TheAJGman Oct 18 '21
Removed all the extra code since we're only testing the performance of repeating a string:
# Tested by running both code fragments through the timeit module on 3.6.9 # 55.8us test = '100000' * 100000 # 14ms test = '' for i in range(0, 100000): test += '100000'Even if it is doing a for loop behind the scenes, it is far more performant to use string multiplication.
15
u/ben_g0 Oct 18 '21
It makes sense too.
With string multiplication, it knows immediately how long the final string is going to be and can allocate all the required memory at once.
With concatenation it continuously grows the string and has to keep allocating more memory for it, as it does not know how long the string will be at the end until it has gone trough the entire loop (an interpreter / JIT compiler doesn't look far enough ahead to precompute that), and continuously reallocating memory slows things down.
If you could first pre-allocate enough memory at the end of the string so that there's always enough room to append without reallocating, then the performance would probably be much closer. Unfortunately I couldn't figure out how to do this in python (and I don't know if it's even possible), so I don't have measurement data to back this up.
6
u/mug1wara26 Oct 18 '21
it will be way faster than using string concatenation in a for loop though
https://stackoverflow.com/questions/10418398/string-multiplication-versus-for-loop
→ More replies (7)3
u/kodosExecutioner Oct 18 '21
Zero loops is all you need:
cout << "1" << endl; cout << "2 2" << endl; . . .
3
u/igl_blue Oct 18 '21
The code in the picture is most-likely from Java which (to my limited knowledge of Java) does not support string multiplication. Hence, still, 2 for-loops.
(Also as said before, that most-likely has a for-loop behind the scenes anyway, so he's still correct)
→ More replies (1)→ More replies (1)1
20
u/afzdcd Oct 18 '21
The way the requirements are written, this is correct. Nested loops are only needed if it has to be dynamic.
To get the nested loops that you want the requirement neds to specify a function that takes an input from 1 to 40 and builds a the triangle.
10
14
4
14
u/XOYZ69 Oct 18 '21
Isn't it technically wrong considering there is missing a "\n" between each line?
34
u/Lezardo Oct 18 '21
System.Out.Println (print line) adds a trailing newline character.
You're thinking of System.Out.Print, which doesn't add the character
12
u/XOYZ69 Oct 18 '21
Not exactly. The code above would return:
1\n 22\n 333\n 4444\n 55555\n
But the exercise wants:
1
22
333 etc.
I know that println includes a "\n", but we need 2 of those here if I am not completely brain dead
8
8
u/xxRespixx Oct 18 '21 edited Oct 18 '21
It looks like there are empty lines between numbers. Println will make this:
1
22
333
4444
55555Not this :
122
333
4444
55555
3
u/GKP_light Oct 18 '21
for 12, should it be :
121212121212
121212121212121212121212
1333333333332
?
6
u/KREnZE113 Oct 18 '21
Do you mean if you conitnued the pattern? Then the 12th-14th row would look like this:
121212121212121212121212 13131313131313131313131313 1414141414141414141414141414Or what do you mean?
1
u/Useless_Pony Oct 19 '21
would 12 be 12 digits of 12 repeated or 12 repeated 12 times or sum(12*10[1...12] )...
i'm 99% sure that last one isn't valid notation, but hope it explains better then words.
1
u/GKP_light Oct 19 '21
you mean if you continue the pattern?
yes.
my first line is : x digit, repeating the number x
2nd line (it is also yours) : repeat the number x x time
3rd line : sum [y from 0 to x not include] (x * 10^y)
1
Oct 20 '21
/uj my 2-liner python script:
for i in range(0, 13): print(str(i)*i)returned:
1 22 333 4444 55555 666666 7777777 88888888 999999999 10101010101010101010 1111111111111111111111 121212121212121212121212So it just depends on whether you're using ints or strings for this.
3
3
2
Oct 18 '21 edited Oct 18 '21
C#
So this works perfectly. Is there any room for improvement while meeting the condition of using loops? I could switch out the number 5 for userInput so then the triangle could continue.
int length = 1;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < length; j++)
{
Console.Write($”{length}”);
}
length++;
Console.Write(“\n\n”);
}2
u/ExcdnglyGayQuilava Oct 20 '21
You don't need that
lengthvariable, just usei + 1where you need it, or count i from 1 to 5 (for (i = 1; i <= 5; i++)).
2
2
2
2
u/hiimjustin000 Oct 19 '21
for (int i = 1; i <= 5; ++i) {
System.out.println(Integer.toString(i).repeat(i));
}
found it
1
2
u/fmaz008 Oct 19 '21
The questions has loops plural. So the answer needs at least 2 loops.
OP, add an empty loop.
2
2
2
Oct 19 '21
If the question or goal is to assert if the person taking the test can program things, then it’s a fail.
He didn’t prove that he can program things, he proved he can fake his way into not doing the task if needed.
3
1
1
1
1
1
u/IlluminatiGodCoder Oct 18 '21
Except that the question specifies to use multiple loops, not one loop. So, technically, still wrong 😜.
1
1
1
1
1
1
1
1
1
1
1
u/zachary592 Oct 18 '21
We had a homework assignment in my high school programming class that was basically exactly this question with a few variations. The guy that sat next to me did exactly this except he added a for loop right above that did nothing. I guess he figured that the teacher didn’t read our code that closely. He was right because he got a 100.
1
u/DrMobius0 Oct 18 '21
"If it's stupid and it works, it's not stupid" doesn't apply to programming and you all know it. I will concede there's a fine line between stupid and clever, although I'd usually judge that based on whether the person writing it managed to make it readable.
1
u/metaconcept Oct 18 '21
The irony is that a decent compiler will unroll your loops and create equivalent code to the above if you set j to be a small value.
1
1
1
u/kiesoma Oct 18 '21
I remember my classmate doing this and our teacher just laughing his ass off looking at the code.
1
1
1
1
1
u/C8R1550-CBM6510-T45 Oct 18 '21
To print out this specific triangle:
system.out.println("1\n22\333\n4444\55555\n");
...0 loops necessary.
Who comes up with such tasks? Noone's gonna learn something from this.
1
1
Oct 19 '21
Holy shit, this is the first meme I get on this site
I just learned about loops in my Cs class
1
1
Oct 19 '21
I remember getting a question to print a diamond shape with the maximum width based on the input given by the user, I manually made around 20 if else conditions for input 1 to 20.
And it worked lol, I the teacher just checked the output, not the actual program lol.
1
1
1
1
1
1
1
u/cheezpnts Oct 19 '21
This always produces the best looks from instructors. And opens the great argument of our time: the sdlc. “It’s workable code. Customer satisfied. Can rework later. “
1
1
1
1
u/MarcusTullius247 Oct 19 '21
Maybe enclosing this coffee in couple of while loops would work.
If that ain't good enough, try recursion
1
Oct 20 '21
python: took me about 5 seconds and zero tries to success:
for i in range(0, 6):
print(str(i)*i)
Edit: forgot to note that I stole this off someones github, ctrl+cv'd it, redistributed it inside my app, changed some code, and that python for loops are pure magic.
1
u/Conversant32 Oct 29 '21
That loop will execute exactly one time. The same thing would have been achieved by serially printing the text with no for loop at all.
0
Oct 18 '21
guys I am a beginner and I think I found the most chaotic way to do this XD
int x = 1, y = 0, i = 1;
label:
while (y < i)
{
Console.Write(x);
y++;
}
if (i != number)
{
x++;
i++;
y = 0;
Console.WriteLine();
goto label;
}
1
u/oscareczek Oct 18 '21
It doesn't work since number can be 0 or less. Also, Console.WriteLine() should be outside the if, you should always end output with a newline, mainly for Unix terminals (idk how CMD/PowerShell handles that). No newline:
1 22 333[oscareczek@pc]$Newilne:
1 22 333 [oscareczek@pc]$1
Oct 19 '21
who said number can be 0 or less? no, WriteLine is inside the if and it works fine. CMD has no problem with it. maybe unlike unix terminal idk.
1
u/oscareczek Oct 19 '21
Who said number can't be 0 or less? You hadn't specified that in initial comment, so it might've as well been.
It doesn't matter then if it works fine in CMD, unless you make a Windows-only program (and this program can't possibly use anything that is + C# is multiplatform), you should always keep that in mind.
2
Oct 19 '21
Who said number can't be 0 or less?
there is a condition. it can't be 0 or less.
unless you make a Windows-only program
literally nobody ever is gonna use this program lmao
0
u/nik_1206 Oct 18 '21
This would be full points in my University no jokes it does exactly what it needs to.
0
u/GoogleIsYourFrenemy Oct 19 '21
for(int a = 1, j =1; a < 6; a++, j = 1 + j *10) System.out.println(a * j);

497
u/[deleted] Oct 18 '21
O(1)
Hired