Campbell Ritchie wrote:A Stream‑based solution might be eminently suitable for beginners, but we shall have to make them do it the hard way with a loop () because these exercises are for teaching people how to use loops.
Andrzej Zahorski wrote:But printing every iteration is not efficient, because I am creating multiple Strings in String Pool
Mike Simmons wrote:But I was playing along with what Junilu himself said earlier about lines of code. Anyway, we shouldn't focus on that.
Mike Simmons wrote:
However, writing it like that is often treated as heresy for some reason.
Mike Simmons wrote:My own streaming solution was just:
There are usually so many ways to do anything.
Junilu Lacar wrote:. . . so many ways to build a triangle of stars . . .
About 0.5sec to 1sec for 1,000,000 append()s earlier today if I supplied the exact size for the StringBuilder in advance.
Mike Simmons wrote:. . . the CampbellRitchie example can be written in a much more performant way (O(N)) by using a single StringBuilder . . . ,
Yes, it will run in n² time, But nobody has the patience to look at enough asterisks for the concatenation to exceed the time for IO.
the triangle of asterisks will always be O(N²) . . .
Campbell Ritchie wrote:Yes, it will run in n² time, But nobody has the patience to look at enough asterisks for the concatenation to exceed the time for IO.
Mike Simmons wrote:the triangle of asterisks will always be O(N²) . . .