r/ProgrammerHumor • • Oct 18 '21

If it works its not wrong right? /s

Post image
8.0k Upvotes

268 comments sorted by

497

u/[deleted] Oct 18 '21

O(1)

Hired

181

u/shnicklefritz Oct 18 '21

O(1) time and space

Promoted

35

u/r3dD1tC3Ns0r5HiP Oct 19 '21

It said for loops not for loop.

CEO

20

u/DeVeenix Oct 19 '21

You mean more like this?

do { for(;;) { ... break; } }while(false);

39

u/[deleted] Oct 19 '21

Though, it may be done better in terms of space:

for(;;) { ... break; }

6

u/soul-san Oct 19 '21

If I'm not mistaken, it's more space demanding than approach with 2 for cycles. This code needs to store 5 string of total memory usage of 15 bytes (and also index with a size of 4 bytes, but that can be ommited). Code with 2 for cycles and printing index + '0' would need only to store this 4 byte index.

Counterargument is that program size could be higher due to longer machine code though.

6

u/TheGuyWithTheSeal Oct 19 '21

The compiler will unroll the loops anyway

1

u/Giest95 Oct 19 '21

Thank you for the joke programming internet stranger. This reminded me that I need to learn why the solution is O(n) instead of O(n2) like it looks and got my ass outta bed

1

u/RelevantCollege Oct 19 '21

i laughed at this but i realized and still want to argue that the right/expected way is still O(1)

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

u/lunch431 Oct 18 '21

Then you're completely fucked, simple as that.

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 (9)
→ More replies (3)

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

u/Reddalos Oct 18 '21

Hahahahaha, killed me with this one

3

u/[deleted] Oct 19 '21

I did this once and it was iiii

→ More replies (1)

128

u/Ninder975 Oct 18 '21

Then you’re doing it wrong

29

u/bigmountainbig Oct 18 '21 edited Oct 18 '21

how is 3d data implemented without something like this? serious question.

8

u/Ninder975 Oct 18 '21

Probably library calls

57

u/eloel- Oct 18 '21

It's a good thing libraries grow on trees.

→ More replies (3)
→ More replies (1)
→ More replies (4)
→ More replies (1)

5

u/AntinotyY Oct 18 '21

Then you use k

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

u/nctrd Oct 19 '21

r/foundsatan

Also, you're hired. Such a large code cannot be wrong and indicates a pro.

6

u/[deleted] Oct 18 '21

iii

Duh. (/j)

2

u/jgeez Oct 18 '21

iii is over there like: I'm just a dude, playing a dude, disguised as another dude!

1

u/druid_137 Oct 19 '21

K. That is the only answer

→ More replies (1)

1

u/bomphcheese Oct 19 '21

… captain.

5

u/Harmonic_Gear Oct 18 '21

when i have to retrospectively add an outer loop to an existing loop

5

u/EnglishMobster Oct 19 '21

"i" for iterator, "j" for jiterator.

2

u/ButtererOfToast Oct 19 '21

"k" for kill me now?

→ More replies (1)

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

u/Harmonic_Gear Oct 18 '21

imaginary unit part is the joke, ee use j where everybody else use i

7

u/Frodojj Oct 18 '21

It’s a complex joke. :-p

3

u/IbanezPGM Oct 18 '21

That would make me less inclined to use j

15

u/StarshipSatan Oct 18 '21

The giant japanese one

9

u/nondxm Oct 18 '21

So, the Godzjlla then?

6

u/Mr_SlimShady Oct 18 '21

I use ‘a’. Sue me.

5

u/yakesadam Oct 18 '21

I had a co-worker try to defend using j because everywhere in the code he wrote when he used j he 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

u/Tarc_Axiiom Oct 18 '21

Only in the second one if you're not a sex offender.

1

u/anotherbutterflyacc Oct 18 '21

That’s the real question 😂

1

u/nono30082 Oct 18 '21

I use for loop in range

1

u/[deleted] Oct 18 '21

It was a copy / paste.

1

u/Rhyan567 Oct 18 '21

Imagine not using c

1

u/Pretend_Education_37 Oct 19 '21

I actually use roman numerals for this. Its much better. 😋🙃

1

u/TobiasH2o Oct 19 '21

When I was first learning my teacher would always use a-z

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

u/edgy_emo_fgt Oct 19 '21

... I always use n, am I uncultured?

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

u/bennyboy_ Oct 18 '21

Player that goes first always wins lol

0

u/[deleted] Oct 19 '21

[deleted]

7

u/No-Recipe-4578 Oct 19 '21

The game is now “tic tac”

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

u/IMovedYourCheese Oct 18 '21

Pretty sure the intention is to have a variable number of rows.

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.

4

u/druid_137 Oct 19 '21

If it was hard coded to 5, the big O is still one.

→ More replies (3)

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

u/[deleted] Oct 18 '21

How could an AI be powered by Marxism-Leninism?

10

u/agentjob Oct 19 '21

Using ML, anything is possible.

2

u/yazalama Oct 20 '21

seize the servers of production

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

u/Thf_killer Oct 18 '21

Yea you got scammed sorry buddy, Atleast he refunded.

17

u/[deleted] Oct 18 '21

Just a question. What is the python icon next to your name and how can I get one?

14

u/[deleted] 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

u/[deleted] Oct 18 '21

Thanks

→ More replies (3)

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
  1. That'd just create an empty file then immediately throw an exception saying that "write" doesn't exist.

  2. 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).

  3. 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

u/EggThumbSalad Oct 18 '21

Easy just increase j to 200 Edit: 2000

2

u/danielt1263 Oct 18 '21

Requirements change! Must re-estimate...

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

u/[deleted] Oct 18 '21

HAHAHA THIS

41

u/[deleted] 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.

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)

1

u/MattieShoes Oct 19 '21

For funzies, perl:

foreach $i (1..5) {
    print $i x $i . "\n";
}
→ More replies (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

u/WinzuMonk Oct 18 '21

outplayed

14

u/The-CPMills Oct 18 '21

Average BlueJ Enthusiast.

4

u/Sese_Mueller Oct 18 '21

[str(i)*i for i in range(x)]

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

u/Lezardo Oct 18 '21

Ah, indeed, I didn't even notice the extra whitespace

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
55555

Not this :
1

22

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 1414141414141414141414141414

Or 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

u/[deleted] 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 121212121212121212121212

So it just depends on whether you're using ints or strings for this.

3

u/Mad-chuska Oct 18 '21

Now do it for 1000

3

u/quixoticM3 Oct 19 '21

It’s wrong. There should be a blank line between each number.

2

u/[deleted] 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 length variable, just use i + 1 where you need it, or count i from 1 to 5 (for (i = 1; i <= 5; i++)).

2

u/SuperD0D0 Oct 18 '21

The usage of BlueJ tells me, that this was a task in Scholl.

2

u/Ellipses22 Oct 18 '21

I'd recognize BlueJ anywhere shudders

2

u/bearfuckerneedassist Oct 18 '21

Task failed successfully

2

u/hiimjustin000 Oct 19 '21

for (int i = 1; i <= 5; ++i) {

System.out.println(Integer.toString(i).repeat(i));

}

found it

1

u/yazalama Oct 20 '21

holy shit been writing java for 3 years and had no idea repeat existed!

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

u/[deleted] Oct 19 '21

maybe it sounds like a joke but I passed an career exam doing this

2

u/WalkingAFI Oct 19 '21

“Technically correct” is by far the best kind of correct

2

u/[deleted] 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

u/Keith_Kong Oct 18 '21

Technically incorrect given the for loop isn’t “necessary”.

3

u/Kind-Insect-3586 Oct 18 '21

The loop is necessary to pass the test.

1

u/giggluigg Oct 18 '21

Write the simplest code that passes the tests, right?

1

u/HxA1337 Oct 18 '21

Ding Ding Ding. Round 2

1

u/IlluminatiGodCoder Oct 18 '21

Except that the question specifies to use multiple loops, not one loop. So, technically, still wrong 😜.

1

u/[deleted] Oct 18 '21

So me

1

u/7eggert Oct 18 '21

You can output all that in just one system call.

1

u/[deleted] Oct 18 '21

I mean, yeah

1

u/masao_kakihara Oct 18 '21

that's how production code should be written 😂😂😂

1

u/MrBananaStorm Oct 18 '21

I like this guy, he's hired

1

u/nekokattt Oct 18 '21

For extra points, use recursion.

/s

1

u/RadiantHC Oct 18 '21

for loops

Loops

s

1

u/Sawertynn Oct 18 '21

The answer is correct... I would approve

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

u/[deleted] Oct 18 '21

And this doesn’t work. Silly.

1

u/the-real-vuk Oct 18 '21

Why have the loop?..

2

u/GreatBarrier86 Oct 18 '21

Because it says you have to write the necessary for loop.

LifeHack

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

u/[deleted] Oct 18 '21

Unless this is a right triangle... it's all wrong!

1

u/obsoleteconsole Oct 18 '21

the test cases pass

1

u/BrokeAlmighty Oct 18 '21

Could easily refactor to make it a do while loop. Lol.

1

u/Cley_Faye Oct 18 '21

The famous optimisation, unrolled-crunched-loops.

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

u/SteeleDynamics Oct 19 '21

Professor: You bastard.

1

u/[deleted] 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

u/MannyS7 Oct 19 '21

Nice O(1) solution

1

u/[deleted] 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

u/JunkiYarde Oct 19 '21

Lol that’s some o(1) runtime

1

u/Godly-Dev Oct 19 '21

What kind of monster uses j instead of i (copy pasted)

1

u/thicc_dog- Oct 19 '21

Just wondering... How would someone make this correctly?

1

u/RedditAlready19 Oct 19 '21

Convert i to a string and print it without newlines i times

1

u/noobmaster692291 Oct 19 '21

I actually did this long time ago lol

1

u/donaldhobson Oct 19 '21
a=["1","22","333","4444","55555"]
for (i in a){
    System.out.println(i);
}

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

u/NoSpoonToday Oct 19 '21
for i in range(1, 6):
    print(i * str(i))

1

u/[deleted] Oct 19 '21

Bad answer.

The question says "loops". I only see one.

1

u/Willing_Function Oct 19 '21

Fail. There is only a single loop.

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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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);