# CompSciHW2

Homework 2 -- types, functions, and substitution Due Monday, Feb. 8

Use the following code as reference for problem 1: <source lang="python"> from numpy.random import uniform from math import cos, sin, acos, pi

def atom_str(x, y, z, at, res, type=" C ", resname="UNK "):

return "ATOM %5d %3s %4s%5s %8.3f%8.3f%8.3f 1.00"%(at,type, \ resname,res,x,y,z)

def polar2cart(r, th, phi):

z = r*cos(phi) # z-axis part x = r*sin(phi) # x/y plane part y = x*sin(th) x = x*cos(th) return x, y, z

def gen_sph_surfpt():

th = uniform(0.0, 2.0*pi) z = uniform(-1.0, 1.0) phi = acos(z) return th, phi

R = 1000 out = open("test.pdb", 'w') for i in range(1000):

u, v = gen_sph_surfpt() x, y, z = polar2cart(R, u, v) out.write(atom_str(x, y, z, i+1, 1)+'\n')

out.close() </source>

- In this code, name the type of each of the following:
- u
- gen_sph_surfpt
- polar2cart
- Make sure you use one of the following types:
- basic type: int / float / string / ()
- derived type: [a], (a, b, c, ...) / a -> ... -> b -> c

- Write code that computes each of the following patterns, but replaces the variable part with 'x' (and 'y', 'z', etc.).
- Be sure to write this as a function with "def f(x, y, etc.):" and use "return" to return the pattern as output.
- Also, write the type of the function you have produced.

a. A list of all positive integers smaller than n

ex: [0, 1, 2, 3, 4] [0, 1] [0, 1, 2, 3, 4, 5, 6, 7]

b. The following sentence template:

"The quick brown dog jumped over 5 foxes." "The terrible brown dog attacked 0 foxes." "The leaning brown dog replaced 99 foxes." "The absent brown dog confused 4 foxes."

c. The following very similar lists:

[8, 7, 4, 2, 2, 4, 7, 8] [8, 7, 9, 0, 0, 9, 7, 8] [8, 7, 6, 5, 5, 6, 7, 8] [8, 7, 2, 3, 3, 2, 7, 8] [8, 7, 6, 1, 1, 6, 7, 8]

d. The outputs (total work and finished flag) of the following flow-chart:

(> 4 days left) ---> (procrastinate) ^ / (work >= 10) ---> (work is done) (<= 4 days left) ---> (add "5 - (#days left)" to work)

Your function should produce the following output for these (left,work) inputs :

input output 6, 0 -> 0, False 5, 0 -> 0, False 4, 0 -> 1, False 3, 1 -> 3, False 2, 3 -> 7, False 1, 6 -> 10, True

3. Write for-loop to do each of the following tasks:

a. Build a list of 4 lines input by the user. Hint: use raw_input

b. Count the number of times the letter "e" appears in a string named 'x'.

c. Return the rot13 cipher of a lowercase string, "x.lower()"

- Explanation: rot13 changes each letter to the letter that appears 13 characters later alphabetically, for example

abc

becomes

nop

while

and ketchup

becomes

naq xrgpuhc

- Hint: use (ord, chr, and the mod function), dont forget to subtract ord('a')