Monday, 21 May 2012

EPIC abs(num)



http://codingbat.com/prob/p124676

This post refers to "near_hundred" problem in the Warmup-1 section of
Codingbat /python

My solution was this:


def near_hundred(n):
    if n<100:
       if 100-n<=10:
           return True
       else:
           return False
    
    if n>=100 and n<=200:
       if n-100<=10:
           return True
       if n-100>10:
            if 200-n<=10:
               return True
            else: 
                return False   
       
    
    if n>=200:
       if n-200<=10:
          return True
       else:
         return False
                             
Codebat's Solution


def near_hundred(n):
  return ((abs(100 - n) <= 10) or (abs(200 - n) <= 10))




this function makes the other way of doing it sound so lame.
one cool and handy function this is.  
Its just like taking modulus in mathematics.
thats how we take mod in python.




Post a Comment