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

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):
Image of page 1
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

  • Spring '14
  • BERRY,ME

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors