Skip to content
Snippets Groups Projects
looping.py 1.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • tuhe's avatar
    tuhe committed
    import numpy as np
    import itertools
    
    def bacteriaGrowth(n0, alpha, K, N): #!f
        """
        Calculate time until bacteria growth exceed N starting from a population of n0 bacteria.
        hints:
    
    tuhe's avatar
    tuhe committed
            * You need to update the number of bacteria n0 within a loop
    
    tuhe's avatar
    tuhe committed
        """
        if n0 > N:
            return 0
        for t in itertools.count():
            n0 = (1 + alpha * (1-n0 / K) ) * n0
            if n0 > N:
                break
    
    tuhe's avatar
    tuhe committed
        t += 1
        return t
    
    tuhe's avatar
    tuhe committed
    
    def clusterAnalysis(reflectance):
        reflectance = np.asarray(reflectance)
        I1 = np.arange(len(reflectance)) % 2 == 1
        while True:
            m = np.asarray( [np.mean( reflectance[~I1] ),  np.mean( reflectance[I1] ) ] )
            I1_ = np.argmin( np.abs( reflectance[:, np.newaxis] - m[np.newaxis, :] ), axis=1) == 1
            if all(I1_ == I1):
                break
            I1 = I1_
        return I1 + 1
    
    def fermentationRate(measuredRate, lowerBound, upperBound):
    
    tuhe's avatar
    tuhe committed
        """
        Compute and return the mean value of the rates in 'measuredRate'
        which falls within lowerBound and upperBound.
        """
        mean_value = np.mean( [r for r in measuredRate if lowerBound < r < upperBound] )
        return mean_value
    
    tuhe's avatar
    tuhe committed
    
    def removeIncomplete(id):
    
    tuhe's avatar
    tuhe committed
        """
        Hints:
            * Take a look at the example in the exercise.
    
    tuhe's avatar
    tuhe committed
        """
        id = np.asarray(id)
        id2 = []
        for i, v in enumerate(id):
            if len( [x for x in id if int(x) == int(v) ] ) == 3:
                id2.append(v)
        return np.asarray(id2)
    
    if __name__ == "__main__":
        # I = clusterAnalysis([1.7, 1.6, 1.3, 1.3, 2.8, 1.4, 2.8, 2.6, 1.6, 2.7])
        # print(I)
        print(fermentationRate(np.array([20.1, 19.3, 1.1, 18.2, 19.7, 121.1, 20.3, 20.0]), 15, 25))
        # print(removeIncomplete(np.array([1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1])))
        # Problem 1: Write a function which add two numbers
        # clusterAnalysis([2, 1, 2, 4, 5])