My solutions for google foobar
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1016B

  1. #I might over-comment those solutions since it might be
  2. #reviewed one day, so bear with me, reviewers!
  3. """
  4. Gets the biggest square below or equal to max_number
  5. I'm sure it can be optimized but as we never get over 1M
  6. it should be largely fast enough for most cpus:
  7. ➜ /tmp time python2 tmp.py
  8. [1000000]
  9. python2 tmp.py 0.01s user 0.00s system 96% cpu 0.009 total
  10. The code is also more readable this way
  11. """
  12. def get_biggest_square(max_number):
  13. n=1
  14. while(n*n < max_number+1):
  15. n=n+1
  16. return n-1
  17. #Check readme.txt for an explaination of what this does.
  18. # "Heavy" computation is done in get_biggest_square
  19. def answer(area):
  20. #check readme.txt
  21. if(area > 1000000 or area < 1):
  22. raise ValueError('Area is outside of bounds')
  23. array=[]
  24. #no need to sort or do fancy pant stuff, we go from biggest to smallest in
  25. #a natural fashion this way
  26. while(area != 0):
  27. res=get_biggest_square(area)
  28. array.append(res*res)
  29. area-=res*res
  30. return array