Nah, I am paranoid, who knows, what can happen? (certainly not k>n*n, but I am still paranoid). Also, you can quickly change function for using doubles this way
Saw in the original post of this code snippet that the compiler is smart enough to compile that code just down to two instructions that essentially becomes n*n.
I could see someone trying this if they needed to artificially slow code execution for timing reasons. I know in calculator programming people sometimes do that, because TI calculators prior to TI-84+ had no hardware clock, and since the architecture is so simple and lacks any sort of multitasking the runtime of each instruction is very predictable. So instead of sleeping, you'd just create a block of code that adds up to exactly the desired wait time. Somewhere I've still got a chart showing the execution times of each
Since we're going down that road, I have an even better solution for ya.
from random import randrange
def square(n):
'''Calculate square of n.'''
while True:
k = randrange(0, n*n+1)
if k == n*n:
return k
Guaranteed to work. Just make sure you feed it reasonably-sized numbers or, if it seems to be slow for some reason, you can always buy more a powerful CPU.
so there is a oneliner. jeez, the original code looked like a dumpster.
i came from C and i’m not very experienced in Python so i just re-wrote this C code into Python:
// len is the length of li
double mean(double li[], int len) {
double total = 0;
// you don’t need braces for oneliner for loops
for (int i = 0; i < len; i++)
total += li[i];
return total / len;
}
The numpy function is also going to be significantly faster, since it will either loop over the data in C instead of in Python, for faster looping.
If the input is in a numpy array, I believe it takes advantage of the number packing to use vector instructions in the CPU to speed it up even more. Nothing like a single SSE instruction to do a 4-wide vector addition in a single cycle.
169
u/curtmack Aug 10 '19
I'm not going to lie, I once wrote a function very similar to this while exhausted. I only realized what I had done looking over it after the weekend.
28
u/Godot17 Aug 10 '19
gotta pump those LOCs
23
u/FunSucks123 Aug 10 '19
When this was posted on programmerhumor someone pointed out that compiler optimizes it to "return n*n" anyway
21
u/KryoBright Aug 10 '19
// >= would be better than just ==
You know just... Safety...(also, screw auto quotes)
20
u/ThePyroEagle λ Aug 10 '19
Trying to use Java after spending your entire life using Prolog.
14
u/KajiTetsushi Aug 10 '19
Oh... oh, dear.
8
u/Lord_Explosion Aug 10 '19
You see if I had to write this for my class I would lose 75% credit for not including try catches for anything that wasn’t an integer.
7
u/phoncible Aug 10 '19
screams in big O
5
u/mattynmax Aug 10 '19
Imagine not using recursion to square numbers
4
u/[deleted] Aug 10 '19
My eyes!
4
u/VritraReiRei Aug 11 '19
Forget the code, I laughed harder than I should have reading the comments.
3
u/harsha1306 Aug 10 '19
Saw in the original post of this code snippet that the compiler is smart enough to compile that code just down to two instructions that essentially becomes n*n.
3
u/espriminati Aug 10 '19
3
u/Divniy Aug 10 '19
Lmao I once was in testing group of city ACM ICPC. One girl literally wrote "for i ... { for j ... { a++; } }".
It passed all the tests. We laughed hard.
6
u/TheJoestarDescendant Aug 10 '19
Then you realized the n is not a whole number...
3
u/abc_wtf Aug 10 '19
Sauce?
2
u/12xoxo13 Aug 10 '19
As a programmer and not just a guy on every single animemes subreddit I can find I totally get this well crafted meme
2
u/SpaceshipOperations Aug 14 '19
Since we're going down that road, I have an even better solution for ya.
Guaranteed to work. Just make sure you feed it reasonably-sized numbers or, if it seems to be slow for some reason, you can always buy more a powerful CPU.
1
u/Inukinator Aug 10 '19
No one seems to mention that this is an infinite loop 🤷♂️
0
u/[deleted] Aug 10 '19
[deleted] — view deleted comment