Code:PyTesting

From Predictive Chemistry
Jump to: navigation, search

Use the following to get started with implementing your own dynamics programs. Focus on writing clean code, testing, and documentation with pseudocode.

<source lang="python"> def logistic(r, x):

   
   Execute a single step of the logistic map.
   input:  
   output: 
   >>> logistic(0.25, 0.5)
   0.0625
   >>> logistic(3.2, 0.7)
   0.672
   >>> round(logistic(3.6, 0.1), 6)
   0.324
   
   return r*x*(1.0-x)
  1. TODO: Create explanation and tests here!

def order(a, b):

 if a < b:
   return a, b
 else: # a >= b
   return b, a

def gcd(a, b):

   
   Implement Euclid's algorithm to find the greatest
   common divisor of a and b
   input:   integer a, b
   output:  integer gcd(a,b)
   >>> gcd(10, 2)
   2
   >>> gcd(30, 18)
   6
   
   b, a = order(b, a)
   while b != 0:
       #print "%d, %d"%(a,b)
       a = a % b # a < b
       a, b = b, a
   return a

def reduce(n, m):

   
   Reduce the quotient n/m into lowest terms.
   >>> reduce(10, 2)
   (5, 1)
   
   return (5, 1)
  1. More functions to implement here:
    1. Arnold's cat map
    2. Tinkerbell attractor
    3. Lorentz attractor

if __name__ == "__main__":

   import doctest
   doctest.testmod()

</source>