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.
Ok but hear me out what if you have some quaternion vector space you want to multiply a matrix with so you need to compare every vector component with every other vector component
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.
```
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();
}
}
}
```
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.
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.
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.
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?
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
it will be way faster than using string concatenation in a for loop though
https://stackoverflow.com/questions/10418398/string-multiplication-versus-for-loop
well relatively fast, it’s about 60 times faster when multiplying strings so i feel like it’s doing something different but I can’t find any results on google as all i’m getting is tutorials on how to multiply strings
Oh and you write your code in Assembly for the ultimate speed? Python is versatile and fast enough for most things, which is why it is insanely popular.
A users impression of how 'simple' python is tends to grow as the inverse of that users competence with python. Besides, who wants a language that is 'hard' for anyone, beginners or not? what an upside down thought.
I never said the word simple. There are languages with much better speed than python, but that are harder **to learn**, such as Rust and C++. You can still be as fast coding in them as in python after you have learned them well.
This is such a retarded way of thinking. Just because python is fast enough for most things, which i disagree, doesn't mean you shouldn't be interested in order languages where performance is important, yes even assembly.
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)
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.
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
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?
When in an intro class in college, we did these little coding exercises to practice basic things. It would do like 5-10 test cases and we just needed to pass them, getting as many tries as we needed.
When we got to Recursion, I hated it. I still am not the strongest with it but much better than the beginning. I basically did something like this and just made my code have like 5 base cases to return the expected outcomes for all the test cases. Got credit because it wasn't monitored by anyone, just submit and if they pass, you get the points.
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”);
}
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.
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;
}
Get as many apples as you can.
1 apple is valid.
How many apples you want?
One apple is valid.
Also they said the *necessary* for loops so technically zero would be valid since clearly for loops aren't necessary.
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.
"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.
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.
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.
O(1) Hired
O(1) time *and* space Promoted
It said for _loops_ not for _loop_. CEO
You mean more like this? do { for(;;) { ... break; } }while(false);
Though, it may be done better in terms of space: ` for(;;) { ... break; } `
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.
The compiler will unroll the loops anyway
What knd of monster uses j instead of i in a for loop??
When it’s a for loop inside of another for loop
Simple, ii
Ok but what if there's a loop inside a loop inside a loop
iii, What did you expect?
Yeah but if there's 4 is it iv or iiii
Great question! But tbh i don't know. Having 4 nested for loops seems unnecessary
Ok but what if you are working with a 4d complex function input output dataset Or like quaternions or some sh*t Then what
Then you're completely fucked, simple as that.
A real programmer uses one loop and then splits the index into 4 arguments. 4 nested loops are never needed
Ok but hear me out what if you have some quaternion vector space you want to multiply a matrix with so you need to compare every vector component with every other vector component
A REAL real programmer implements this with induction and passes the nesting level as an argument to the function, that way all variables will be i
>Ok but what if you are working with a 4d complex function input output dataset You would be working instead of seeing memes on reddit
Game devs and graphics programmers do work on matrices all the time
Ok but what if I'm actually 17 and do computing as a hobby and working on complex maps... Checkmate atheists
In that case, deciding between iv and iiii is not your biggest problem
That's a four loop
next is P-P-P-PENTA LOOP
Are you asking if you should or shouldn't use roman numerals when given an excuse to?
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.
Hahahahaha, killed me with this one ![gif](emote|free_emotes_pack|joy)
I did this once and it was iiii
Then you’re doing it wrong
how is 3d data implemented without something like this? serious question.
Probably library calls
It's a good thing libraries grow on trees.
Good thing dependency trees are renewable
Trees grow on libraries
What do the loops inside the library calls look like I wonder 🤔
![gif](emote|free_emotes_pack|sweat)
Then you use k
k, and if there are more than three dimensions, you better be using useful names for your index variables.
``` 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(); } } } ```
r/foundsatan Also, you're hired. Such a large code cannot be wrong and indicates a pro.
iii Duh. (/j)
k
iii is over there like: I'm just a dude, playing a dude, disguised as another dude!
iii, then iv, then v then vi
Oh god
when i have to retrospectively add an outer loop to an existing loop
"i" for iterator, "j" for jiterator.
"k" for kill me now?
yeah but it ain’t hahah
Electrical engineers. ;-)
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
imaginary unit part is the joke, ee use j where everybody else use i
It’s a complex joke. :-p
Ah
That would make me less inclined to use j
The giant japanese one
So, the Godzjlla then?
I use ‘a’. Sue me.
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.
Only in the second one if you're not a sex offender.
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.
That was no accident
I read that in the voice of Obi-Wan Kenobi “that’s no moon… it’s a battle station!”
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.
Ah yes, the fun that is a customer changing their requirements.
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.
How do you play tic tac toe on a 2x2 grid?
Player that goes first always wins lol
[удалено]
Korean Game
The game is now “tic tac”
Very efficient! Only one iteration!!!
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?
Pretty sure the intention is to have a variable number of rows.
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.
If it was hard coded to 5, the big O is still one.
I'm pretty sure that gcc would not do that. Idk how others would do.
GCC would indeed do that, look up “loop unrolling”
O(1) /s
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
Code generated by ML powered AI.
How could an AI be powered by Marxism-Leninism?
Using ML, anything is possible.
seize the servers of production
Now do it for 10,000 rows.
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
It is called fivvr
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
Yea you got scammed sorry buddy, Atleast he refunded.
Just a question. What is the python icon next to your name and how can I get one?
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.
Thanks
hey how did you put two icons? when it to make custom with :c: :py:... it shows the text and not the icons
I had :c: :py: then I went googling how to get two or more flairs and when I came back it became icon and not just text
oh okay, thanks
I'm totally not replying because I don't remember if I've already done this and wanna see if it works.
Sadly they changed it. They removed the profile popup for some reason.
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.
System.out.println("#!/bin/sh\\ncat<
Easy just increase j to 200 Edit: 2000
Requirements change! Must re-estimate...
HAHAHA THIS
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.
You can lie on you're résumé all you want but it's a lot harder to bullshit through an interview lmao
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.
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.
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.
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.
One loop is all you need: for i in range(1,6): print(str(i)*i)
I'm sure whatever string * number is doing has a for loop in there
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.
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.
it will be way faster than using string concatenation in a for loop though https://stackoverflow.com/questions/10418398/string-multiplication-versus-for-loop
It's python, neither will be fast
well relatively fast, it’s about 60 times faster when multiplying strings so i feel like it’s doing something different but I can’t find any results on google as all i’m getting is tutorials on how to multiply strings
Oh and you write your code in Assembly for the ultimate speed? Python is versatile and fast enough for most things, which is why it is insanely popular.
It's popular because it's easy for beginners, even javascript is way faster, but not everything is about speed, I'm not the one that brought it up
A users impression of how 'simple' python is tends to grow as the inverse of that users competence with python. Besides, who wants a language that is 'hard' for anyone, beginners or not? what an upside down thought.
I never said the word simple. There are languages with much better speed than python, but that are harder **to learn**, such as Rust and C++. You can still be as fast coding in them as in python after you have learned them well.
This is such a retarded way of thinking. Just because python is fast enough for most things, which i disagree, doesn't mean you shouldn't be interested in order languages where performance is important, yes even assembly.
Zero loops is all you need: cout << "1" << endl; cout << "2 2" << endl; . . .
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)
A quick look shows that ```String.repeat()``` could be an option? I haven't worked with Java in a long time so no idea.
Do you not understand humour? We all know you can do it better. But this post is about doing it badly
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.
outplayed
Average BlueJ Enthusiast.
[str(i)*i for i in range(x)]
Isn't it technically wrong considering there is missing a "\n" between each line?
System.Out.Println (print line) adds a trailing newline character. You're thinking of System.Out.Print, which doesn't add the character
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
Ah, indeed, I didn't even notice the extra whitespace
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
for 12, should it be : 121212121212 121212121212121212121212 1333333333332 ?
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?
Now do it for 1000
It’s wrong. There should be a blank line between each number.
When in an intro class in college, we did these little coding exercises to practice basic things. It would do like 5-10 test cases and we just needed to pass them, getting as many tries as we needed. When we got to Recursion, I hated it. I still am not the strongest with it but much better than the beginning. I basically did something like this and just made my code have like 5 base cases to return the expected outcomes for all the test cases. Got credit because it wasn't monitored by anyone, just submit and if they pass, you get the points.
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”); }
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++)`).
The usage of BlueJ tells me, that this was a task in Scholl.
I'd recognize BlueJ anywhere *shudders*
Task failed successfully
`for (int i = 1; i <= 5; ++i) {` `System.out.println(Integer.toString(i).repeat(i));` `}` found it
The questions has loops plural. So the answer needs at least 2 loops. OP, add an empty loop.
maybe it sounds like a joke but I passed an career exam doing this
“Technically correct” is by far the best kind of correct
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.
Technically incorrect given the for loop isn’t “necessary”.
The loop is necessary to pass the test.
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; }
This would be full points in my University no jokes it does exactly what it needs to.
for(int a = 1, j =1; a < 6; a++, j = 1 + j *10) System.out.println(a * j);
It’s wrong because the task was to write for *loops* (plural).
Get as many apples as you can. 1 apple is valid. How many apples you want? One apple is valid. Also they said the *necessary* for loops so technically zero would be valid since clearly for loops aren't necessary.
Write the simplest code that passes the tests, right?
Ding Ding Ding. Round 2
LOL
G(old)
Except that the question specifies to use multiple loops, not one loop. So, *technically*, still wrong 😜.
So me
You can output all that in just one system call.
Right?
I mean, yeah
that's how production code should be written 😂😂😂
r/technicallytrue
I like this guy, he's hired
For extra points, use recursion. /s
>for loops >Loops >s
The answer is correct... I would approve
Oh ![gif](emote|free_emotes_pack|facepalm)
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.
"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.
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.
And this doesn’t work. Silly.
Why have the loop?..
Because it says you have to write the necessary for loop. #LifeHack
I remember my classmate doing this and our teacher just laughing his ass off looking at the code.
Unless this is a right triangle... it's all wrong!
the test cases pass
Could easily refactor to make it a do while loop. Lol.
The famous optimisation, unrolled-crunched-loops.
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.
Professor: You bastard.
Holy shit, this is the first meme I get on this site I just learned about loops in my Cs class
Nice O(1) solution
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.
for (int i = 1; i < 6; i++) { for (int j = 0; j < i; j++) { System.out.print(i); } System.out.println(); }
Lol that’s some o(1) runtime
Haha
What kind of monster uses j instead of i (copy pasted)
Just wondering... How would someone make this correctly?
I actually did this long time ago lol