This question already has answers here:
How can I clamp (clip, restrict) a number to some range?
(9 answers)
Closed 4 years ago.
i have a simple problem in Python where i need to return a value that is equal to floor if it is smaller than floor, and return ceiling if the value is larger than ceiling. I'm not sure if there is a simpler way to do so. What i will do is:
def floor_ceil(x):
floor = 0
ceil = 100
if x < floor:
return floor
elif x > ceil:
return ceil
return x
Are there simpler ways to do so? The code looks clunky
You could reuse the built-in max and min:
def floor_ceil(x):
return max(0, min(100, x))
Related
This question already has answers here:
Is there any method like divide by or multiply by in python range()?
(4 answers)
Closed 2 years ago.
The C code here:
for(i = 1; i <= 1000; i *= 3){
//task
}
How can I write this in python with a for loop?
So far I can only think of a solution using while loops:
i = 1
while i <= 1000:
#task
i *= 3
Is there a way I can write this with a for loop in python?
try this
step = 3
_i=1
for x in range(0,1000):
if x%_i!=0:
continue
_i*=step
# task
print(x)
This question already has answers here:
A weighted version of random.choice
(28 answers)
Closed 6 years ago.
I am using a number generator currently using the code below
from random import randint
print(randint(0,1))
which while it works, i need to generate either 0 or 1 but i need the generator to generate 1 with a x (say 80%) probability
how can i achieve this effectively in python ?
random.random() will return a random decimal between 0 and 1. Use this value to determine your choice:
from random import random
print(0 if random() > 0.8 else 1)
and of course, 0.8 can be replaced with any probability (60% -> 0.6).
As #TigerhawkT3 noted, you might use the shorter version random() < 0.8 to generate a boolean variable, if you want to do an action based on that random probability, like
x = random() < 0.8
if x:
print('hello world!')
This question already has answers here:
calculating catalan numbers using memoization
(2 answers)
Closed 7 years ago.
I am trying to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change?
def catalan_mem(n, mem = None):
if n==0:
return 1
if mem==None:
mem={}
sum=0
if n not in mem:
for i in range(0, n):
sum = sum + catalan_mem(i) * catalan_mem(n-i-1)
mem[n]=sum
return mem[n]
You need to pass the cache to your recursive calls:
sum = sum + catalan_mem(i, mem) * catalan_mem(n-i-1, mem)
Otherwise, every method call creates a new empty cache.
This question already has answers here:
Is there a ceiling equivalent of // operator in Python?
(9 answers)
Closed 7 years ago.
It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes.
Is there a way to divide that rounds upwards if there is a non-zero remainder?
For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:
items = 102
boxsize = 10
num_boxes = (items + boxsize - 1) // boxsize
Alternatively, use negation to convert floor division to ceiling division:
num_boxes = -(items // -boxsize)
Negate before and after?
>>> -(-102 // 10)
11
from math import ceil
print(ceil(10.3))
11
You can try :
import math
math.ceil( x )
This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 7 years ago.
I'm attempting to calculate e^x using recursion, e^x = e^(x/2)*e^(x/2), and the third order Maclaurin expansion for e^x and the script keeps returning 1. I'm not looking for a higher accuracy solution, just simply to understand where the script goes wrong : )
My thought is that with enough iterations it should end up with (1+x/N+(x/N)^2/2)^N when the function value goes below the limit.
def exp(x):
if abs(x)<0.0001:
return 1+x+x**2/2
else:
y=exp(x/2)
return y*y
Try this instead (note the 2.0 in the recursive call):
def exp(x):
if abs(x) < 0.0001:
return 1 + x + x**2 / 2.0
else:
y = exp(x / 2.0)
return y * y
It is failing because if you pass an integer in for x, say 1, then x / 2 does integer division (in python 2.x), which would result in 0 instead of 0.5. By using x / 2.0, it forces python to use float division.
def exp(x):
if abs(x)<0.0001:
return 1+x+(x**2)/2.0
else:
y=exp(x/2.0)
return y*y
Integer division truncates. You need floats here.