CompSciHW4

From Predictive Chemistry
Revision as of 14:54, 19 February 2016 by David M. Rogers (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Homework 4 - Due Friday, February 26, 2016.

1. Write down the types for the following standard python functions.
     Hint: Consult the text, use help() and try out example function calls.
 a) str.count
 b) list.reverse
 c) str.strip
 d) str.split
 e) len

2. For each of the following, define any function that has that type.
   You must use all the arguments when creating the return value.
     Example: str -> int -> str  Answer: def f(x, i) return x + str(i)
 a) int -> float
 b) str -> [str]
 c) (int,int) -> (float,float)
 d) int -> int -> int
 e) str -> str -> str

3. Replace the constants and the variables in the following patterns.
   Create a single function returning the pattern.
   Define the constants outside the function.
     Example:                     Answer:
      open("test1.dat", 'r')      mode = 'r'
      open("test2.dat", 'r')      def f(name):
      open("test3.dat", 'r')          return open(name, mode)
       
 a) pretty-printed data
   "Atom %d coords %f %f %f"%(1, 1.1, 3.3. 0.0)
   "Atom %d coords %f %f %f"%(2,-1.2, 7.9. 0.0)
   "Atom %d coords %f %f %f"%(3, 2.3,-1.4. 0.0)


 b) tagged data
   ("A1",1.1,  [(1,2), (1,3)])
   ("A2",-1.2, [(1,2), (1,3)])
   ("B1",7.6,  [(1,2), (1,3)])
   ("B2",-1.4, [(1,2), (1,3)])

 c) scaled arange
   x = []
   for i in range(17):
     x.append(i*(4.0 - 2.3)/(17-1.0) + 2.3)

   x = []
   for i in range(22):
     x.append(i*(4.0 - 2.3)/(22-1.0) + 2.3)

   x = []
   for i in range(50):
     x.append(i*(4.0 - 2.3)/(50-1.0) + 2.3)

 d) status report
   print "Converged in %d iterations, residual = %e."%(100,1.7e-5)
   print "Converged in %d iterations, residual = %e."%(553,4.9e-10)
   print "Converged in %d iterations, residual = %e."%(201,8.3e-4)

 e) logistic map simulation
   x = 0.5
   vals = []
   for i in range(100 + 50):
      vals.append(x)
      x = 3.7*x*(1.0-x)
   return vals[50:]

   x = 0.5
   vals = []
   for i in range(100 + 50):
      vals.append(x)
      x = 3.1*x*(1.0-x)
   return vals[50:]

   x = 0.5
   vals = []
   for i in range(100 + 50):
      vals.append(x)
      x = 2.8*x*(1.0-x)
   return vals[50:]

 f) scaled and shifted random numbers (from numpy import random)
   random.standard_normal(10)*20.0 - 5.0
   random.standard_normal(10)*10.0 - 25.0
   random.standard_normal(10)*2.0 + 5.0

 g) The count
   " ".join(map(str, range(4)))
   " ".join(map(str, range(10, 0, -1)))
   " ".join(map(str, range(9, 22)))

 h) Summing random numbers (from numpy import random)
   x = sum(random.random()*2.0 for i in range(4))
   x = sum(random.random()*2.0 for i in range(10))
   x = sum(random.random()*2.0 for i in range(20))

 i) trying combinations in a sequence
   for x in range(3):
     for y in range(4, 7):
       if x^y == 7:
         break
     if x^y == 7:
       break

   for x in range(3):
     for y in range(4, 7):
       if x^y == 4:
         break
     if x^y == 4:
       break

   for x in range(3):
     for y in range(4, 7):
       if x^y == 9:
         break
     if x^y == 9:
       break

 j) writing a list to a file (the entire list is the variable)
   out = open("list.dat", 'w')
   out.write("\n".join(map(str, [1,2,3,3,4])) + "\n")
   out.close()

   out = open("list.dat", 'w')
   out.write("\n".join(map(str, [-3,3,1,2])) + "\n")
   out.close()

   out = open("list.dat", 'w')
   out.write("\n".join(map(str, [7,7,1])) + "\n")
   out.close()