11
u/centarx Nov 03 '18
Ew bluej
6
u/thehunter699 Nov 04 '18
Yeah. Lecturer makes it compulsory to submit using bluej...
3
u/Bainos Nov 04 '18
Honestly it makes sense. You probably don't want to throw new students in Eclipse, or they won't find their way to the editor.
The unfortunate side-effect is that some of them will still use BlueJ in projects two years from now.
3
u/thehunter699 Nov 04 '18
Yeah I can see it being useful. The stupid thing is you have to do intro it IT before this unit. That unit uses eclipse, so your going backwards pretty much.
2
u/totemcatcher Nov 04 '18
Persistence of vision version. Requires very old CRT or projector + glow in the dark wall.
int i = 0, l;
for(;;i++) {
l = i % 5;
System.out.print(l!=0?String.format("%" + (1+l-(int)(Math.random()*l)) + "d", l) + "\n":"\033\143");
System.out.flush();
}"\033[H\033[2J" might work for windows users. ;)
2
80
u/urbanek2525 Nov 03 '18
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 needs to specify a function that takes an input from 1 to 40 and builds a the triangle.
12
u/Salanmander Nov 03 '18
I mean, that's a good idea, but you learn things one at a time in programming classes, and it's entirely reasonable to be learning loops before functions. Sure, you can still specify it with something like "have a variable declared at the beginning of your code such that if you change the number stored in the variable..." etc., but that just makes it more confusing for the most confused students in order to prevent the least confused students from deliberately avoiding the assignment.
5
Nov 04 '18
it's entirely reasonable to be learning loops before functions
Scheme guy here, how is that even possible?
7
u/Salanmander Nov 04 '18
I do it when teaching Java. It's not actually without using methods, but at the beginning you just say "here is the magic incantation that you put all your code inside, we'll understand it later". That's basically mandatory in Java anyway, since the first line of the magic incantation is "public class [insert class name]", and fuck if I'm going to talk about public vs. private before we write "hello world".
4
Nov 04 '18
I think you miss interpreted the joke: in scheme the most favorable way to loop is through tail-call recursion, which require defining a function (or named let) before hand.
Joke asides, I completely agree with you: neither OO C++ or Java is straight forward for beginner. Even for sane human beings, declaring a class/template for a simple computation is total madness.
3
4
u/TheMoonMaster Nov 04 '18
The way the requirements are written, this is correct. Nested loops are only needed if it has to be dynamic.
Is it though? It says loops as in plural, but there is only 1 loop.
0
342
u/tenhourguy Nov 03 '18
Using loops to print that would be an over-engineered solution anyway.
System.out.println("1\n22\n333\n4444\n55555");
156
Nov 03 '18
O(1)
63
u/snowball7241 Nov 03 '18
The original is also O(1)
7
Nov 03 '18
can someone explain how is that O(1)? I'm still struggling with that.
30
u/snowball7241 Nov 03 '18
The way I was taught big O is that all constant times are O(1)
6
Nov 03 '18 edited Nov 03 '18
and whats O(n) for example?
edit: thanks!
33
u/cbbuntz Nov 03 '18 edited Nov 03 '18
O(1)
[any constant combination of statements not in a loop]O(n)
for (i = 0; i < a.size(); i++){ [do stuff with a, but no further nested loops] }O(n2)
for (i = 0; i < a.size(); i++){ for (j = 0; j < a.size(); j++){ [do stuff with a, but no further nested loops] } }Note: there can be other nested loops, so long as they don't grow with the number of elements.
Big O only accounts for the fastest growing part of an algorithm; it estimates the growth rate as n approaches infinity.
So if you have some initialization, and then a loop, the initialization part is ignored since it doesn't grow at all. So:
1 + n => O(n)
n log n + n => O(n log n))
n log n + n + n2 => O(n2)
1000000 + log n + n2 + 2n => O(2n)
Edit: I realized I didn't even answer the question. The loop runs once, so it's O(1).
If you made it so that n is the number of rows it prints, it would be O(n2) (assuming nested loops). If you ever see something like:
for (i = 0; i < n; i++) for(j = 0; j < i; j++)It simplifies to O(n2)
3
2
u/bestjakeisbest Nov 04 '18
technically even if the original for loop ran multiple times, it would still be considered constant time, in this case it works out fine because the amount of times it runs is once but:
int sum = 0; for(int i = 0; i < 10; i++){ sum++; }would also be constant time, the function you would have to plug into big o is: f(x) = 10, but since constants are for the most part ignored in big o the O(f(x)) === O(1).
not that you are wrong, just a point of clarification.
7
u/Hifoz Nov 03 '18
Time needed is grows linearly with n
8
u/DudeValenzetti Nov 03 '18
Similarly:
O(n log n) stands for linearithmic complexity: directly proportional to n times the logarithm of n (base irrelevant, but usually represents logβ). Examples: mergesort's and heapsort's time complexity. Common in "divide and conquer" algorithms.
O(n2) stands for quadratic complexity: directly proportional to the square of n. Things with one
forloop inside another tend to have O(n2) time complexity, but it can go to O(n3) or higher with deeper nesting. Examples: bubble sort, selection sort, insertion sort average case, quick sort worst case.O(2n) stands for exponential complexity. It's rarely the limit for something, but stuff happens. Example: time complexity of brute-forcing the discrete knapsack problem (most dynamic solutions are O(nC) both in time and memory, where C is the capacity of the knapsack).
1
1
u/Bainos Nov 04 '18
O(1) loops iterations, but O(n) I/O calls (with n constant defined at compile time).
28
u/munchingfoo Nov 03 '18
*
System.out.println("1\n\n22\n\n333\n\n4444\n\n55555");.The original code makes the same presentational error.
13
11
Nov 03 '18
Disagree this is the quickest and only way
8
u/dem_paws Nov 03 '18
It's my professional opinion that your sexmachine function needs to be passed a callback and return a promise.
function sexmachine(x) { return new Promise(function(resolve,reject) { resolve(x('333')); }); } sexmachine(x => x).then((response) => { alert(response); }).then(()=> { // 4444 and 55555 });6
Nov 03 '18
Hmm... you're right god damn it, but I'm a bit concerned about mutation so I think I have to recommend for us to rework this from vanilla js into redux React, and we are probably going to need a separate staging environ for beta testing.
I'm afraid this can't be done for south of $250,000.
2
u/tenhourguy Nov 03 '18
I like it, and to add to the confusion, I'm on mobile, only able to read a section the size of a postage stamp at a time in horizontal mode. In portrait mode, it has to wrap onto a new line after every single character. I can't even copy it to the clipboard.
1
4
u/serigne007mor Nov 03 '18
But students that just started always get those things as practice problems
5
u/tenhourguy Nov 03 '18
Bah! Practice problems! Give them a problem that's feasible in the real world, I say.
Or maybe not. I'm aware of my lack of teaching abilities so maybe they know what they're doing when creating these practice problems.
2
2
2
u/Samiam23322 Nov 03 '18
For that matter
while ( j == 0 ) { System.out.println("1\n22\n333\n4444\n55555"); j++;}
1
Nov 04 '18
But the question asks for it.
2
42
u/Kosba2 Nov 04 '18
for(int i = 1; i <= 5; i++) {
for(int j = 0; j < i; j++) {
System.out.print(i);
}
System.out.println();
}14
u/PlusUltraBeyond Nov 04 '18
+/u/CompileBot Python
print("\n".join(reversed([str(6-i)*(6-i) for i in range(1,6)])))13
u/CompileBot Green security clearance Nov 04 '18
15
u/exmachinalibertas Nov 04 '18
12
u/CompileBot Green security clearance Nov 04 '18
10
u/exmachinalibertas Nov 04 '18
Oh right, your documentation says you can't read or write files. But we can create a file that gets run by an interpreter or a compiler, because that's how you work, right. Well, I guess let's first see if I'm banned yet, and try to get more directory info.
+/u/CompileBot Python
import os; os.system("pwd; ls -lah ../")6
u/CompileBot Green security clearance Nov 04 '18
10
u/exmachinalibertas Nov 04 '18
Alright, not banned yet, that's good. So it looks like we get ourselves an ephemeral user with a short random name. This is probably in a virtualized container, but let's still poke around a little bit. Maybe there's programs we can run that have read/write access even if we don't.
+/u/CompileBot Python3
import os print(os.environ["PATH"] + "\n") for dir in os.environ["PATH"].split(":"): print(dir) os.system("ls -lah " + dir) print("\n")8
u/CompileBot Green security clearance Nov 04 '18
Output:
total 68K drwxr-xr-x 2 root root 4.0K Feb 7 2017 . drwxr-xr-x 10 root root 4.0K Feb 7 2017 .. -rwxr-xr-x 1 root root 223 Feb 7 2017 epylint -rwxr-xr-x 1 root root 213 Feb 7 2017 isort -r-xr-xr-x 1 root root 6.7K Feb 7 2017 json_xs -rwxr-xr-x 1 root root 182 Feb 7 2017 numba -rwxr-xr-x 1 root root 53 Feb 7 2017 pycc -rwxr-xr-x 1 root root 221 Feb 7 2017 pylint -rwxr-xr-x 1 root root 229 Feb 7 2017 pylint-gui -rwxr-xr-x 1 root root 227 Feb 7 2017 pyreverse -rwxr-xr-x 1 root root 223 Feb 7 2017 symilar -rwxr-xr-x 1 root root 3.4K Feb 7 2017 theano-cache -rwxr-xr-x 1 root root 11K Feb 7 2017 theano-nose -rwxr-xr-x 1 root root 604 Feb 7 2017 theano-test total 113M drwxr-xr-x 2 root root 32K Feb 7 2017 . drwxr-xr-x 10 root root 4.0K Feb 7 2017 .. -rwxr-xr-x 1 root root 51K Feb 7 2017 [ lrwxrwxrwx 1 root root 8 Jan 24 2017 2to3 -> 2to3-2.7 -rwxr-xr-x 1 root root 96 Jan 19 2017 2to3-2.7 -rwxr-xr-x 1 root root 96 Jan 19 2017 2to3-3.5 lrwxrwxrwx 1 root root 25 Feb 7 2017 aclocal -> /etc/alternatives/aclocal -rwxr-xr-x 1 root root 36K Jan 25 2017 aclocal-1.15 -rwxr-xr-x 1 root root 23K Feb 7 2017 addpart lrwxrwxrwx 1 root root 26 Feb 5 2017 addr2line -> x86_64-linux-gnu-addr2line lrwxrwxrwx 1 root root 6 Dec 13 2016 apropos -> whatis -rwxr-xr-x 1 root root 15K Feb 7 2017 apt -rwxr-xr-x 1 root root 79K Feb 7 2017 apt-cache -rwxr-xr-x 1 root root 23K Feb 7 2017 apt-cdrom -rwxr-xr-x 1 root root 23K Feb 7 2017 apt-config -rwxr-xr-x 1 root root 43K Feb 7 2017 apt-get -rwxr-xr-x 1 root root 26K Feb 7 2017 apt-key -rwxr-xr-x 1 root root 43K Feb 7 2017 apt-mark lrwxrwxrwx 1 root root 19 Feb 5 2017 ar -> x86_64-linux-gnu-ar -rwxr-xr-x 1 root root 35K Feb 7 2017 arch lrwxrwxrwx 1 root root 19 Feb 5 2017 as -> x86_64-linux-gnu-as -rwxr-xr-x 1 root root 15K Mar 26 2016 autoconf -rwxr-xr-x 1 root root 8.4K Mar 26 2016 autoheader -rwxr-xr-x 1 root root 32K Mar 26 2016 autom4te lrwxrwxrwx 1 root root 26 Feb 7 2017 automake -> /etc/alternatives/automake -rwxr-xr-x 1 root root 248K Jan 25 2017 automake-1.15 -rwxr-xr-x 1 root root 26K Jan 23 2017 autopoint -rwxr-xr-x 1 root root 21K Mar 26 2016 autoreconf -rwxr-xr-x 1 root root 17K Mar 26 2016 autoscan -rwxr-xr-x 1 root root 34K Mar 26 2016 autoupdate lrwxrwxrwx 1 root root 21 Feb 7 2017 awk -> /etc/alternatives/awk -rwxr-xr-x 1 root root 55K Feb 7 2017 b2sum -rwxr-xr-x 1 root root 39K Feb 7 2017 base32 -rwxr-xr-x 1 root root 39K Feb 7 2017 base64 -rwxr-xr-x 1 root root 31K Feb 7 2017 basename ...10
u/exmachinalibertas Nov 04 '18
Whoops, typo! Let's try again.
+/u/CompileBot Python3 --include-errors
import os, base64 def showme(s): b64_1 = base64.b64encode("p" + "rint(" + s + ")") exec(base64.b64decode(b64_1)) print("This is the first test.\n") showme("This is the second test.")→ More replies (0)6
u/exmachinalibertas Nov 04 '18
Alright, so print() statements seem to get cut out, and you have a 50 display limit on what you can show me. You also appear to have no updated anything in the last year and a half, although I still suspect you're just a docker container. And the apt-get tells me it's a Debian/Ubuntu docker. Well let's first see if I can get print working for me.
+/u/CompileBot Python3
import os, base64 def showme(s): b64_1 = base64.base64encode("p" + "rint(" + s + ")") exec(base64.b64decode(b64_1)) print("This is the first test.\n") showme("This is the second test.")4
u/OCOWAx Nov 04 '18
2
1
4
2
1
u/Besquiter Nov 04 '18 edited Nov 04 '18
for (int i=1;i<=5;i++){ System.out.println(Math.round(Math.pow(10,i))/9*i); }
15
Nov 03 '18 edited Nov 05 '18
int i = 0;
string line;
int MAX = SomeMaxValue;
while (i < MAX)
{
for (int j = 0; j<i; j++)
{
line += String.Format("{0," + Math.Ceiling((Math.Log10(MAX)+1)*-1).ToString()"}", i.ToString());
}
i++;
line.AddNewLine();
}Something like that should work with any number in a reasonable format. C#. Just need to print the string.
Edit: forgot I commented on here before and wrote another solution:
do
{
System.out.println("1");
System.out.println("22");
System.out.println("333");
System.out.println("4444");
System.out.println("55555");
} while (false);8
2
u/RandomMagus Nov 04 '18
Shouldn't this be Floor, not Ceiling? You'd get 2-character strings starting from MAX==2 when you want 2-character strings at MAX between 10 and 99.
(Also missing a "+" after the .ToString() on the Ceiling call)
2
u/tobysmith568 Nov 04 '18
int maxValue = 6; string[] row; for (int i = 0; i <= maxValue; i++) { row = Enumerable.Repeat(i.ToString(), i).ToArray(); Console.WriteLine(String.Join("", row)); }Seems a little bit simpler to me, and it also works for any value.
13
8
u/baquea Nov 03 '18
This is wrong as it says "for loops" not "for loop".
8
u/Notsononymous Nov 04 '18
It says "the necessary for loops", not "the more than one necessary for loops", implying the minimum is fine.
The plural is used because the number is used because we don't know a priori how many loops will be used.
3
3
u/elhs16 Nov 04 '18 edited Nov 04 '18
+/u/CompileBot C#
using System;
using System.Linq;
namespace HelloWorld
{
class Program
{
static void Main (string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(string.Join("", Enumerable.Repeat(i.ToString(), i).ToArray()));
}
}
}
}2
u/CompileBot Green security clearance Nov 04 '18
3
2
2
2
u/sheya55 Nov 04 '18
>>>for x in range (1, 55555):
>>>....z = [int(_) for _ in str(x)]
>>>....if len(str(x)) == z[0] and set(*z)[0] == x: print(x)2
u/muzzbuzz789 Nov 04 '18
+/u/CompileBot JavaScript
for(i = 1; i < 6; i++){
for (j = i; j <= i; j++){
console.log(i.toString().repeat(j));
}
}2
u/o4ub Nov 04 '18
Am l the only one here that sees most of the answers (including the post answer) wrong as there is the extra empty line missing every god damn time?
1
1
Nov 05 '18
using System;
public class Program
{
static void printNums(string y, int x)
{
for (int j = 1; j <= x; j++)
{
Console.Write("{0}", y);
}
Console.WriteLine();
}
public static void Main()
{
for (int i = 1; i <= 5; i++)
{
printNums(i.ToString(), i);
}
}}
0
u/Melon_Chief Nov 04 '18
#include <iostream>
int main() {
const int TRIANGLE_LEN = 5;
for (int i = 1; i <= TRIANGLE_LEN; i++) {
for (int j = i; j--;) {
std::cout << i;
}
std::cout << std::endl;
}
}It literally takes less time to do it properly ._.
Also, what's humor?
2
u/thehunter699 Nov 04 '18
notice the /s ?
2
u/Melon_Chief Nov 04 '18
I did ask "What's humor?", you can't blame me (well yes you can but please don't)
Also when I said "properly" I meant "in C++" ;)
-3
u/dosangst Nov 03 '18 edited Nov 05 '18
for ( j = 1; j < 6; j++){
var s = "";
for ( i = 1; i <= j; i++){
s = s + j.toString()
};
console.log(s);
}2
-1
u/McBrown83 Nov 04 '18
Yur grammars suk... It saids "loops". As in 2+
2
3
0
Nov 03 '18
[deleted]
176
u/cbbuntz Nov 03 '18 edited Nov 03 '18
int n = strlen("1\n22\n333\n4444\n55555"); char buf[n + 1]; for (buf[n] = '\0'; strcmp("1\n22\n333\n4444\n55555", buf);) for(int i = 0; i < n; i++) buf[i] = rand() & 0x7f; puts(buf);Best case: O(n)
Average case: O(128n)
Worst case: O(β)
86
Nov 04 '18
O(β)
i mean, that's some true efficiency right there
6
u/cbbuntz Nov 04 '18
Yeah. This this is worse than a bogosort for small n (but the bogosort gets worse once you get a bit past 128 elements since its average case is O(n!)). By the time you get past 10 elements or so, you may as well give up hope on either one finishing.
5
8
10
u/THANKYOUFORYOURKIND Nov 04 '18
Why not keep it simple, stupid?
for ;; { System.out.println("1"); System.out.println("22"); System.out.println("333"); System.out.println("4444"); System.out.println("55555"); break; }2
2
0
Nov 04 '18
[deleted]
0
Nov 04 '18
[deleted]
9
4
u/tinyhitman Nov 05 '18
- for (int i = 1, i <= 55555, i++) {+ for(int i=1; + i <=55555 ; + i++){ + if(i == 1] print (i + β\nβ); + if(i == 22) print (i + β\nβ); + if (i == 333) print (i + β\nβ); + if (i == 4444) print (i + β\nβ); + if (i == 55555) print (i + β\nβ); }
- if (i == 1) print (i + β\nβ);
- if (i == 22) print (i + β\nβ);
- if (i == 333) print (i + β\nβ);
- if (i == 4444) print (i + β\nβ);
- if (i == 55555) print (i + β\nβ);
Pls merge
0
0
Nov 03 '18
[deleted]
6
u/CompileBot Green security clearance Nov 03 '18
5
u/thatfatgamer Nov 03 '18
ah! no wonder! I was trying this
for(var i=1;i<=5;i++){ for(var j=1; j<=i;j++){ console.log(i); } }facepalm
2
u/PlusUltraBeyond Nov 04 '18
+/u/CompileBot Python
print("\n".join(reversed([str(6-i)*(6-i) for i in range(1,6)])))2
u/CompileBot Green security clearance Nov 04 '18
0
Nov 03 '18
[deleted]
8
u/CompileBot Green security clearance Nov 03 '18
4
u/Silver-Core Nov 04 '18
+u/CompileBot scala
val z = (1 to 4).map(x => x.toString * x).mkString("\n") println(z)4
u/PotatosFish Nov 03 '18
Thereβs probably a way to do that in one line
3
u/Bainos Nov 04 '18
You can just remove the new line. This one compiles:
+/u/compilebot Python 3
for x in range(1,6): print(str(x)*x)2
u/CompileBot Green security clearance Nov 04 '18
2
u/PotatosFish Nov 04 '18
Well, while also following pep8
1
u/Bainos Nov 05 '18
I don't think there is anything about adding a new line after an
ifor adefin PEP8. It's mostly enforced by the language, and when not enforced, it's for a good reason.Usually, when it's possible, it also improves readability. For example,
if error: return Noneandif debug: print(var)don't justify two lines.0
0
3
108
u/[deleted] Nov 03 '18
What kind of monster uses j instead of i in a for loop??