I hate that my brain made me sort out what this does…
Funny how all (or most) comments are about the formatting and not the horrific implementation of permute. I can't even figure out if it works.
This is why its so good at permuting
It's the permuta triangle algorithm
Depression is iteratively calling a recursive algorithm inside of itself.
It does nothing, as that class only has two methods and both are private. (The closing brace for the class is at the end of the last line.)
Whatever its permute
method would do, if anyone were allowed to call it, it would have a time and console output complexity of O((n+1)!) (factorial time), unless n > a.length - 1
, in which case it'll throw an ArrayIndexOutOfBoundsException
.
Edit: Off by one in the time complexity.
Doesn't it also just not have a termination case? The case presented is a print and not a return.
The recursion is in the else clause, so it does eventually terminate. Also, for negative values of n, the for loop makes zero iterations.
Oh, that's awful.
I’m responding after your edit and if it was to edit your time complexity to add the “+1”, that’s unnecessary. Big O notation is concerned with the asymptotic growth of the function relative to n, and as n approaches infinity the difference in the function output due to any constant factor approaches 0.
That’s what I thought initially, but (n+1)!/n! tends to n+1 as n tends to infinity, so it's not a constant factor. Instead of the +1, it should perhaps rather be expressed as O(n(n!)) to be more idiomatic.
I was like “what friggin language is this” until I saw the horrifying brackets 🤢
Jathon? Pyva?
is this the "Jason" every web developer seems to be so obsessed over?
Jython probably, since there's the cython
I was like “is this python…? I’m so confused” and then saw the brackets and physically startled lol
IRL depression doesn't really look like anything, though
True. I should post a selfie.
I was so confused until I finally looked in the right margin.
Kill it. Kill it with fire.
This isn't depression. This is heresy.
Brother, get the flamer... the heavy flamer.
kris…this code is not a [BIG SHOT]
This is autism
I- really can't tell if I love or hate this
Aside from the obvious, why are the methods static and not extension methods? Or just injected as a singleton?
Everything in JVM-based languages needs to be encased within a class, even if you just need to define a collection of pure functions. Kotlin allows you to create "classless" files in which you define these pure functions, but that's ultimately syntactic sugar.
Sure but static functions like this are an anti pattern. It bypasses DI and makes the code less testable
Pure static functions are not per se an anti-pattern, and forcing everything into classes for the sake of DI and testability can be just as much of an anti-pattern itself. That being said, I'll admit that I misread the class and that the actual issue is that the functions aren't pure: it's got a stealth dependency of a PrintStream
instance where it's printing out the permutation result. The class should be rewritten to include a PrintStream
member field that gets used in the System.out.println()
call, and the functions would then become instance functions instead of static - there's the DI that you'd want.
Because Java still doesn't have extension methods…
The singleton code would be awful complex, especially if it needs to be thread safe.
Didn't know Java doesn't have extension methods. How would it be more complex to make this thread safe in a di context than it would be with this static method. I mean it isn't thread safe right now
I've just meant that a fully written out singleton, which is thread safe, is quite some code in Java. You asked about injected singletons.
A static class is not a singleton. You can't inject different static classes by DI. Using static stuff means it's hard coded to the concrete type name.
And yes, this doesn't look thread safe in the current state…
No, I think that's the manic stage.
We write code in a readable form for humans not machines.
It’s not even good code
Variable width font coders BTFO
I'm not sure what you mean.
The chars align perfectly in columns, so this isn't a variable with font used for that code.
The RHS is killing me bro 😶
Anything other than 1TBS is mental illness, change my mind.
What's "1TBS"?
LOL the code segment is the most accurate depiction of how my brain feels on a daily basis 😂 #ProgrammerLife
Hi police? I'd like to report an assault.
ON MY EYES
This isn't depression, it's a cry for help.
Indeed! At the moment someone makes you use some language that requires useless syntax noise like braces and semicolons one starts to cry for help, that's true.
Lmao, both are accurate, but that Java code hits a bit too close to home! Anyone else debugging till 3 AM feeling like a part of their soul is being permuted? 😅
Ugh reminds me of the time I accidentally took the harder coding class that was required. One of the worst classes ever (teacher sucked)
When a python dev tries have for the first time :D
Most of my programming experience is python, so I never understood what is wrong until comment section. My only guess was "Java surely must have built-in function for that, like itertools.permutations in python. Maybe it's the joke, that depressed person rewrites library functions"
Java surely must have built-in function for that, like itertools.permutations in python
LOL, no. That's Java.
You do such things in Java like so:
https://www.baeldung.com/java-array-permutations 😂
The language you're looking for is Scala (see my other comment).
I had my C++ segfault in the inplementation of GTest on a random ass move. We debugged it for 3 days, than we came to the conclusion, that my linux distro is fucked, and the problem isn't in the code.
This sounds scary!
Which Linux distri was this, and what exactly was fucked?
Took me a while to see wtf
That's not depression, that's insanity
You wanted a python job but got hired for java.
Gotta make it work somehow... 🤙
Set up default code format.
Autoformat on Save.
Save.
Problem Fixed.
Tru depression is if you have spent two days debugging this shit before having to ask someone to help you
Depression? Homicide more like.
I will find you and I will k1ll you! 😂
So nobody remembers c++?
Yep, that make my readability drop to -1
java code that looks like python yeah, nice
Must add this comment:
// Do not reformat the braces {}. It breaks the production.
"Java" code (or actually JVM code) that looks like Python would be more this here:
That's Scala, a kind of Python + Haskell for the JVM.
This code wouldn't be useful of course for obvious reasons: The static methods (methods on an object in Scala) are private, so you can't call them from the outside…
Also the
Range(0, n+1)
expression isn't idiomatic Scala. You would usually use the0 to n
syntax sugar instead. But theRange
looks more like Python so I've written it like that.Beside that, that's anyway not idiomatic Scala as it uses mutable values and imperative loops. Also
Array
is just the "naked" Java Array, and not a Scala collection, and one does usually not use Array directly. Actually also not in Java!Writing that above code makes also no sense in general as Scala comes with a permutations method on collections (and
Array
through extension methods).But syntactical the code above is pretty close to Python, imho.
It is now looking more like ruby lol :D