hw2s.pdf

# hw2s.pdf - COMPUTER SCIENCE 50100 Computing for Science and...

• Homework Help
• 5

This preview shows pages 1–3. Sign up to view the full content.

COMPUTER SCIENCE 50100 Computing for Science and Engineering FALL 2015 ASSIGNMENT # 2 Solutions (35 points) October 6 This assignment covers Section 2.3–2.6 of the class notes. 1. (8 points) The problem is to write a module funcf.py implementing two versions of a function o that takes as it arguments two functions f and g , each of which maps a float to a float . The function o is to return the composition f g . (a) (5 points) The first implementation, call it o1 , should make no use of the lambda oper- ator. (b) (3 points) The second implementation, call it o2 , should make no use the def command— none at all. Here is a template with a unit test: #!/usr/bin/env python # _funcf.py def o1(f, g): ... o2 = ... if __name__ == ’__main__’: def f(x): return x*x + 1 def g(y): return y*y - 2 h = lambda z: z*z +3*z for o in [o1, o2]: fog = o(f, g) for x in [2, 3, 5]: print(fog(x) == f(g(x))) hogof = o(h, o(g, f)) for x in [-1, -2]: print(hogof(x) == h(g(f(x)))) Solution: #!/usr/bin/env python # funcf.py def o1(f, g): # call them o1 and o2 def fog(x): return f(g(x)) return fog o2 = lambda f, g: lambda x: f(g(x)) if __name__ == ’__main__’: def f(x): return x*x + 1 def g(y): return y*y - 2 h = lambda z: z*z +3*z for o in [o1, o2]: fog = o(f, g) for x in [2, 3, 5]: 1

This preview has intentionally blurred sections. Sign up to view the full version.

print(fog(x) == f(g(x))) hogof = o(h, o(g, f)) for x in [-1, -2]: print(hogof(x) == h(g(f(x)))) 2. (10 points) sec 2.5 Write a script that, from the current working directory and every nested subdirectory, removes every file whose name ends with “ ~ ”, but
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern