Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
A friend asked me to make her coding more "pythonic" but I'm pretty new at it myself. This is what I came up with, and I'm a little concerned that it won't hit all of the numbers (6, 7, 8, 9, 10 and 11). I also KNOW that there is a better way, but I just don't know what it is. Can you help?
prob = 0
for r in range(6,11):
prob += binom.pmf(k=r, n=11, p=0.2)
print(‘The probability is {}’,format(prob))
I think that your code is perfectly pythonic, for what that's worth
You could make it a little more compact and more performant with a list comprehension, the sum() function, and an f-string. Also, I took into account that you wanted 11 inclusive, so your range should be from 6 to 12.
probs = [binom.pmf(k=r, n=11, p=0.2) for r in range(6, 12)]
print(f'The probability is {sum(probs)}')
Use list comprenhension:
def binom(n,r,p):
# dummy function
return n+r+p
prob = sum([binom(r,12, 0.2) for r in range(6,11)])
print(f"The probability is {prob}")
Edit:
To make 11 inclusive, increase your range to 12
Edit 2:
As suggested by OneCricketeer, if only need to compute the sum, you can remove the square brackets, like so:
prob = sum(binom(r,12, 0.2) for r in range(6,11))
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!
There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]
You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]
import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to generate 100 random values from a specific mean and specific range values.
Do you know a function that do that in R or Python ?
Thanks a lot.
that R solution could meet your expectations
first (if needed) use install.packages("truncnorm") to install package
then
library(truncnorm)
rtruncnorm(n=100, a=0, b= 10, mean=5, sd=5)
This depends on what distribution you want your random values to come from. Another option would be a uniform distribution. In R you could do
n <- 100
mu <- 10
myrange <- 10
runif(n, mu - myrange/2, mu + myrange/2)
to get 100 draws from a uniform around the mean 10.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
There has been a solution to find the possible combination of numbers to reach a given target number. However, I have a different situation below, where a,b, and c are product types and I like to find the combination of sum products of a,b and c to reach the target total.
a = 50sqft
b = 70sqft
c = 100sqft
Total = 5000sqft
I like to find all possible combinations of numbers (integer solution) of a,b,c to get to 5000, and how can I create a python function for that?
Results :
(100a,0b,0c)=5000
(23a,5b,8c)=5000
...
...
Thanks in advance!!
I got a solution :
a=50
b=70
c=100
for i in range(101): # This si 101 here to give 100a=5000
for j in range(100):
for k in range(100):
if i*a + j*b + k*c == 5000:
print('({}a,{}b,{}c)=5000'.format(i,j,k))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
The question is just pure curiosity, since I quite recently started learning algorithms and data structures and applying knowledge in python.
my code is:
def sorting(listik):
n = range(len(listik))
for i in n:
lowest = min(listik[i:])
for j in n:
if listik[j] < listik[lowest:]:
lowest, listik[j] = i, j
listik = [2,8,5,4,7,1,6,9,10,3]
sorting(listik)
print listik
output is correct, but it is sorted by indexes as it's supposed to.
I have seen codes that do that with for loop, but those were not selection sort algorithms, however i can't be 100% sure if this is possible applying this algorithm.(also, would love to hear a comment, if this algorithm could be simplified,w/o using external libraries tho)
Thank you !
you can use sort :
>>> listik = [2,8,5,4,7,1,6,9,10,3]
>>> listik.sort()
>>> listik
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The built-in sorted function will sort the list of numbers, regardless of index.
print(sorted(listik))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I'm kinda new to python and was given a problem as follows:
Given the list names , find the largest element in the list and swap it with the last element. For example, the list ["Carlton", "Quincy" "Adam", "Bernard"] would become ["Carlton", "Bernard", "Adam", "Quincy"] . Assume names is not empty
I thought about doing list Comprehension but I don't know how I would write that out in code
EDIT: Largest in this case would be the length of the string (sorry for not clarifying!!!))
names = [foo, fooooo, bar, baaar]
a, b = i.index(max(name, key=len)), -1
i[b], i[a] = i[a], i[b]
Courtesy of this.
If by largest, you mean longest, then you could iterate over the list to find the longest name, and then swap them.
maxLen = 0
maxI = 0
for i in range(0, len(names)):
if len(names[i]) > maxLen:
maxLen = len(names[i])
maxI = i
temp = names[-1]
names[-1] = names[maxI]
names[maxI] = temp
This is an overly convoluted way of doing it, but it's so drawn out to make it more obvious as to what's going on. With that said, you really should be more specific about what "largest" means.