This question already has answers here:
Big O, how do you calculate/approximate it?
(24 answers)
Closed 4 years ago.
I have been asked to explaine about Big O notation and to calculate Big o Notation for an algorithm. I'm done with the defining part but I'm still wondering how I can calculate it. Can someone help me to calculate the Big O for the below given code?
new = int (input("enter number" ))
if new <= 10000:
comm=new*2/100
print (comm)
else :
comm= new*5/100
print (comm)
Since there is no loop, it is O(1).
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why does the following code return the following output?
def z(x, y):
return (x + 2*y + 1)**2
delta = 0.1
x_derivative = (z(delta, 0) - z(0, 0)) / delta
print(x_derivative)
Output: 2.100000000000002
If my maths is correct, it should just be 2.1.
This has probably be asked before, but I don't know the right terms to search for.
This is due to the representation of floating point numbers internally that causes errors of precisions like that one.
0.1 in binary has an infinite representation (much like 1/3 in base 10), so that's why you get that deviation.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
when i try to add 1 to answer python adds 10^-16 to the answer tried printing in different ways but i think it will not help. Any answers ?
x = 0.8475
print(1 + x)
print(1 + 0.8475)
print(1.0 + 0.8475)
y = 1 + x
print(y)
output :
1.8475000000000001
1.8475000000000001
1.8475000000000001
1.8475000000000001
Its called a floating point error, and isn't specific to python.
A float can't represent all values perfectly accurately, so you get these weird inaccuracies like you're seeing.
Use Decimal instead of float if you need your numbers to be that accurate. In most cases though, its acceptable to just round your answer to a few decimal places.
https://docs.python.org/2/library/decimal.html
This question already has answers here:
Adding Values From Tuples of Same Length
(6 answers)
Closed 4 years ago.
This is an example of the code
elif longitude > longitudel and latitude < latitudel:
thor = 1,-1 + thor
I want it to change if say thor is = 1,1 I want it to become 2,0 I have been messing around with it for a while but I cant seem to find a way to get this to work and keep it as a tuple. Is there a way to keep it as a tuple or do I have to set seperate integers to get this to work ?
You can do it like this:
thor = tuple(sum(x) for x in zip(thor, (1,-1)))
A shorter way would be:
thor = tuple(map(sum, zip(thor, (1,-1))))
Taken from here.
This question already has answers here:
Smooth color transition algorithm
(5 answers)
Closed 5 years ago.
How to get a list of color values between two given ones? For example, I have #ab7878 and #da9933, what if I want the first color to be gradually transformed into the second color, how could I get those values in between?
You can do this by incrementing the values for R, G and B at the same time.
#4477AA
#5588BB
#6699CC
#77AADD
#88BBEEE
var a = 0xab7878;
var b = 0xda9933;
while(b > a)
{
a = a + 0xFF;
console.log(a.toString(16));
}
Full output:
Pastebin
This question already has answers here:
How to format a floating number to fixed width in Python
(10 answers)
Closed 6 years ago.
I'm making a percentage finder, pretty basic. But, I would only like to print 4 characters in the float or int. Can anyone help?
Here is my code so far:
numer = int(input("What's the numerator?"))
denom = int(input("What's the denominator?"))
percent = (numer / denom * 100, "%")
print(percent)
I want the output for 5/6 to be:
83.33%
But it actually prints:
83.33333333333334%
you can use % in print function
print("%.2f"%(a/b))