hw01.py - Homework 1 Control Q1 from operator import add sub def a_plus_abs_b(a b"Return a abs(b but without calling abs > a_plus_abs_b(2 3 5 >

# hw01.py - Homework 1 Control Q1 from operator import add...

• Homework Help
• 3

This preview shows page 1 - 3 out of 3 pages.

""" Homework 1: Control """ # Q1 from operator import add, sub def a_plus_abs_b(a, b): """Return a+abs(b), but without calling abs. >>> a_plus_abs_b(2, 3) 5 >>> a_plus_abs_b(2, -3) 5 """ if b < 0: f = sub else: f = add return f(a, b) # Q2 def two_of_three(a, b, c): """Return x*x + y*y, where x and y are the two largest members of the positive numbers a, b, and c. >>> two_of_three(1, 2, 3) 13 >>> two_of_three(5, 3, 1) 34 >>> two_of_three(10, 2, 8) 164 >>> two_of_three(5, 5, 5) 50 """ return max(a,b,c)*max(a,b,c) +max(min(a,b),min(b,c),min(a,c))*max(min(a,b),min(b,c),min(a,c)) # Q3 def largest_factor(n): """Return the largest factor of n that is smaller than n. >>> largest_factor(15) # factors are 1, 3, 5 5 >>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40 40 >>> largest_factor(13) # factor is 1 since 13 is prime 1 """ i = n while i > 1: i = i - 1 if n % i == 0: return i # Q4 def if_function(condition, true_result, false_result):

#### You've reached the end of your free preview.

Want to read all 3 pages?

• Spring '14
• BERRY,ME

### 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