r/badcode • u/zshadowjon • Jul 29 '21
python Was doing some bug hunting and found essentially what was a sum to n function but of O(fuck) time complexity
1.2k
Upvotes
r/badcode • u/zshadowjon • Jul 29 '21
126
u/zshadowjon Jul 29 '21
If you're curious, the original code was attempting to do two for loops consecutively, but a mistake in indentation produced this. This is a modified version of the code to show just how silly it is. Also it's O(n^2) lol
285
u/Notimecelduv Jul 29 '21
Counting empty strings? That's original. I don't know anything about space-time complexity but there's a simple formula to calculate this:
n * (n + 1) / 2.84
u/beaucephus Jul 29 '21
Not exactly true. That particular function is O(shit). If you want O(fuck) complexity you need to make it recursive.
28
u/fuwafuwa7chi Jul 29 '21
This is why you pay attention in your math classes, kids.
18
u/finitearth Jul 29 '21 edited Jul 29 '21
return 0.5*n*(n+1)
Edit: formating
27
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 29 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
9
u/jzia93 Jul 29 '21 edited Jul 29 '21
Just as an aside : If you know the formula then it's definitely ideal at O(1),
However if you're in a similar situation and can't work out an algebraic solution, in python you can use the
@lru_cacheto make drastic speed improvements in such cases with recursive functions.7
u/Tha_Gazer Jul 29 '21
Big O(fuck) time complexity indeed
5
u/[deleted] Jul 29 '21
How’d you take this screenshot? Looks really cool.
3
u/Jackiboi307 Jul 29 '21
whats the theme!!111!11
2
u/GitHub- Jul 29 '21
This is hot nonsense
2
u/[deleted] Jul 29 '21
My god analysing this gets so much worse every step of the way
2
u/Uchimamito Jul 29 '21
It is actually hard to understand this code it’s so bad. Could have just done return sum(range(n+1))
2
u/Voxelgon_Gigabyte Jul 29 '21
broke:
sum = 0
for i in range(n):
sum += n
return sum
woke:
this shit
2
u/stahkh Jul 30 '21
Ignoring all the nonsensical complexity, what I am curious about is how did you come up with counting the empty string array instead of just using a number? I'm shocked.
2
u/StaryMD Jul 30 '21
hol' up
isn't the exact complexity of the algorithm
n * (n + 1) / 2?my man has goals beyond our understanding
1
u/circuit10 Jul 29 '21
This can be O(1) with the triangle number formula
1
u/Taeker2005 Jul 29 '21
Gauss is rolling in his grave...
1
u/DesecrateUsername Jul 29 '21
Gonna go back and ask my data structures professor why he didn’t teach us about O(fuck) operations
1
u/britreddit Jul 29 '21
O(fuck) has absolutely floored me. Gonna have to remember that for future use
1
u/sixtyfifth_snow Jul 30 '21
At least it won't be affected by plagiarism
1
u/i-can-sleep-for-days Jul 30 '21
Love the font and color scheme. What is it?
1
u/Lambda_Wolf Jul 30 '21
O(hell no)
1
u/jank_lord Jul 30 '21
O(fuck) I'm using that in my next interview.
1
u/weflown Jul 30 '21
o(fuck) is when you reach complexity o(n!) and bigger so don't cry
1
u/[deleted] Aug 05 '21
How are people generating these nicely formatted screenshots? Is it a special feature of a specific code editor? An online tool?
1
u/Pesk0r Jul 29 '21
Error on line 2, unknown use of "hold", did you mean "hodl" /r/dogecoin
0
u/[deleted] Jul 29 '21
[deleted] — view deleted comment
0
u/[deleted] Jul 29 '21
[deleted] — view deleted comment