88
u/Lystrodom Nov 04 '18
Hey this is better code than the extra loops. More efficient!
37
u/loopsdeer Nov 04 '18
Way easier to maintain: entry level engineers can be trusted with the copy-paste-modify workflow
3
48
u/commit_bat Nov 04 '18
Deducting points for using j instead of i in the outermost loop but I commend it for running in O(1)
5
u/JuhaJGam3R Nov 13 '18
wont println still run O(n) by looping trough all the characters to print it though. and then if we take the JVM into question its just going to become a fucking mess
6
u/commit_bat Nov 13 '18
That's not what that means
5
u/JuhaJGam3R Nov 13 '18
i know it doesn't but i'm now questioning the efficiency of java itself wait a moment
2
u/punkinpumpkin Dec 29 '18
Wouldn't it be O(1) regardless since the method doesnt take any arguments and therefore would always run in the same amount of time...
1
u/commit_bat Dec 29 '18
...yeah, I did say it is O(1).
1
u/punkinpumpkin Dec 29 '18
I meant its O(1) even if you implemented the entire thing with nested for loops like the intended solution to the exercise, so it's not really that special that it is O(1)
1
u/commit_bat Dec 29 '18
Maybe my fantasy ran a little wild there but I imagined that ideally the algorithm would not be hardcoded to five loops
1
u/punkinpumpkin Dec 30 '18
Well, it was an exercise on how to use nested loops anyway, so while, yeah, ideally the above is the simplest solution it wasn't really the point of the exercise. I totally agree that if printing that exact thing was what was asked id default to something like OPs solution too
20
u/randfur Nov 04 '18
Just for the sake of comparison.
for (int i = 1; i <= 5; ++i) {
for (int j = 0; j < i; ++j) {
System.out.print(i);
}
System.out.println();
}6
u/archpawn Nov 09 '18
That displayed on one line for me. Using four spaces before each line instead of the three graves seems to work:
for (int i = 1; i <= 5; ++i) { for (int j = 0; j < i; ++j) { System.out.print(i); } System.out.println(); }11
u/MemeDaddyTech Nov 04 '18
Python:
for i in range(1, 6): print(str(i) * i)2
1
Feb 07 '19
Woah, didn't know you could print something multiple times by adding the "* x"
At first, when I read this, I was like, "Ha! This guy thinks you can perform math on something you've turned into a string!"
And then I went to run your line of Python, and it gave me the correct output, and I doubted everything I knew about the world.
Then I found this Stack Overflow answer that confirmed it for me. Suddenly I realized I'm not crazy, it's just that I'm a noob at Python and that you're better at it. I'm now my own example of the Dunning-Kruger Effect.
24
u/Besquiter Nov 04 '18 edited Nov 04 '18
Waiting for someone to find the mathematical formula to do it in one line without python
Edit:found it
#!/bin/bash
for i in {1..5};do
echo $((10**i*i/9))
done10
u/adm7373 Nov 04 '18
Question should have specified that there should only be one System.out.print call
3
0
7
u/prof_hobart Nov 04 '18
Assuming that this person could also demonstrate that they could solve it the way that the question author intended, I'd be tempted to hire them.
One of my bugbears is devs inventing huge piles of requirements around extensibility etc and then spending weeks building a solution to those requirements rather than the day it would have taken to solve the actual requirements they'd been set.
14
u/AprilSpektra Nov 04 '18
Some programmers are so obsessed with genericity that they act like it would be heresy to write code that just solves the problem you need it to solve.
5
u/ANCHSIQ Nov 04 '18
Would this work in java?
One line.
for(int i=1;i--;System.out.println("1\n22..."));
1
u/jarfil Nov 04 '18 edited Jul 17 '23
CENSORED
15
u/Istanfin Nov 04 '18
The question specified to write for loops. Of course it is not necessary with this solution.
20
u/galaktos Nov 04 '18
And since the task specifically requires loops, let’s add one level:
for(int j = 0; j < 1; j++) { for(int y = 0; y < 1; y++) { System.out.println("1"); System.out.println("22"); System.out.println("333"); System.out.println("4444"); System.out.println("55555"); } }8
1
u/spur Nov 07 '18
I hope we agree that it's the redundant single-iteration for loop that makes it bad code, not the comical (due to missing the point of the question) directness of the solution.
1
1
0
172
u/RTracer Nov 04 '18