Python Forum
Looping exercise Bacteria growth
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping exercise Bacteria growth
#1
Hi. I am taking a python course(I am a complete beginner) and I am unable to solve one of the assigned exercises. Can someone help me understand how can I input the "t" into the code to get tN as output?
so far my code:
import numpy as np
import math
def bacteriaGrowth(n0, alpha, K, N):
    a= alpha  #" Growth rate"
    K= K #Capacity
    t= x
    
    if N[x+1] == (1 + a*[1- n0/K])*n0:
        N>n0 and N<K
        tN= x +1 
        return tN
The exercise description below....

It has been discovered that a population of bacteria grows according to the following law: If there are nt bacteria
at time t then one hour later at time t + 1 there will be
nt+1 = (1 + *α[1- nt/K])*nt

where α is a positive number that controls the growth rate and K is a positive number that defines the capacity,
i.e., the maximum number of bacteria in the population.
Problem definition

Write a program that simulates the bacteria growth hour by hour and stops when the number of bacteria exceeds
some fixed number, N. Your program must return the time t at which the population first exceeds N. Even
though the actual number of bacteria is really a whole number (integer) your program must work with nt as a
decimal number (real), i.e., you should not round the numbers.


Solution template
def bacteriaGrowth(n0, alpha, K, N):
# Insert your code here
return tN
Input
n0 Initial number of bacteria (scalar)
alpha Growth rate (scalar)
K Capacity (scalar)
N Final population size (scalar, n0<N<K)
Output
tN Time t at which population size exceeds N (scalar)

Thank you for your help in advance....
Reply
#2
It's like obfuscation contest - all these n0, K, N, tK, x.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Here's a start, see comments below
import numpy as np
import math
def bacteriaGrowth(bac_count, growth_rate, capacity, max_size):
    #a= alpha  #" Growth rate"
    #K= K #Capacity
    #t= x
    time = 0
    while max_size < bac_count:
        time = time + 1
        bac_count = (1 + growth_rate * (1 - bac_count/capacity)) 
    #if N[x+1] == (1 + a*[1- n0/K])*n0:
        #N>n0 and N<K
        #tN= x +1 
    return time

bac_count = input("Initial Bacterial count")
growth_rate = input("And So On")

print(bacterialGrowth(bac_count, growth_rate, capacity, max_size))
It is considered "unPythonic" to use variable names that do not describe their function, and as pointed out above it makes the code incredibly difficult to read. I hope I translated them properly. I also hope I translated your formula properly.

Note that when you use [] in Python it means a list. You were using them as an extra level of parentheses. N[x+1] is a list called N, and you are accessing the x+1 element. I don't think that is what you wanted to do. Don't confuse scientific notation with programming syntax. They are very different, as in my statement time = time + 1. In math that means 0=1, which is obviously not the intent. That line, btw could be shortened as time+=1, but I thought that would add a level of confusion given the other syntax errors you had.

Finish up the input statements, note that I used while instead of if when making a loop, and post your progress!
Reply
#4
Thank you for your reply:)

I have updated my code with your help and since its an assignment I have to submit, it has to have a return "tN" as an output of the function. I used what you told me and the solution of the exercise says that I should get an output of 7, but with my function I get an output of 0 for some reason...Can you tell me what am I doing wrong?
import numpy as np
import math
def bacteriaGrowth(n0, alpha, K, N):
tN=0
while N<n0:
n0=(1 + alpha*(1- n0/K))*n0
tN=tN+1

return tN

print(bacteriaGrowth(100.0, 0.4, 1000.0, 500.0))
- I use the above print to check if the program gives me an expected output of 7
Reply
#5
Please post with Python tags!
Your while statement is reversed. The loop only occurs while 500 is less than 100. It isn't, the loop never executes.

Change it to while n0<N and the result is 7.
Reply
#6
Yes, I have noticed that mistake in my code. Thank you again for your help!

I need help with one more exercise... It seems that my loop function does not work and I do not understand why..
I get a result 2 and I should get this list of number:2 2 2 2 1 2 1 1 2 1.

My code:
[python]
import numpy as np
import math
def clusterAnalysis(reflectance):
ref=np.size(reflectance)
even1=np.any(ref %2==0)
uneven1=np.any(ref%2>0)
even2="2"
uneven2="1"
even3=(np.mean(even1))
uneven3=(np.mean(uneven1))
while ref==even1:
clusterAssigments="2"
if ref==uneven1:
clusterAssigments="1"
else:ref==even1 and ref==uneven3
clusterAssigments="2"
return clusterAssigments
print(clusterAnalysis(np.array([1.7, 1.6, 1.3, 1.3, 2.8, 1.4,
2.8, 2.6, 1.6, 2.7])))
[python]
The assigment:
The clustering algorithm
Given a set of N measurements, (r1, r2, . . . , rN ), we will initially assign the odd-numbered measurements to
class 1 and the even numbered measurements to class 2. Then the following two steps are repeated:
• Update step: Compute the mean value (average) of the measurements within each cluster.
• Assignment step: Assign each measurement to the cluster with the closest mean value. In case of a tie,
assign the measurement to cluster 1.
Repeat the above steps until the cluster assignments do not change. It can not be determined in advance how
many steps will be needed before the clustering assignment stabilizes.
Problem definition
Create a function that takes as an input a vector of reflectance measurements and returns a vector of cluster
assignments computed using the algorithm described above.

Thank you in advance for your help!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020