An interview question


/ Published in: Python
Save to your folder(s)

Find sum of 2 smallest number


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2. import logging
  3. import csv
  4. from inspect import currentframe, getframeinfo
  5.  
  6. logger = logging.getLogger('root')
  7. FORMAT = "[%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"
  8. logging.basicConfig(format=FORMAT)
  9. #logger.setLevel(logging.NOTSET)
  10. logger.setLevel (logging.DEBUG)
  11.  
  12.  
  13. def sum(array):
  14. logger.debug ("array function")
  15. if len(array) < 2:
  16. print "Error! Array small"
  17. return -1
  18.  
  19. array.sort()
  20. print "Sorted array : %s" %array
  21. print "%d %d" % (array[0],array[1])
  22. return array[0] + array[1]
  23.  
  24. if __name__ == "__main__":
  25. mylist = [10,3,4,1,7]
  26. x = sum(mylist)
  27. print "sum of 2 min numbers : %d" %x

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.