Advertisement
Atheuz

fair_and_square.py

Apr 13th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. def is_palindrome(n):
  4.     p = str(n)
  5.     if p == p[::-1]:
  6.         return True
  7.     else:
  8.         return False
  9.  
  10. def run(case, A, B):
  11.     print B
  12.     palindromes = filter(is_palindrome, range(1, 1000))
  13.     squares = map(lambda x: x*x, palindromes)
  14.     fair_and_square = filter(is_palindrome, squares)
  15.     fair_and_square = len(filter(lambda x: x in range(A,B+1), fair_and_square))
  16.     return "Case #%d: %d\n" % (case, fair_and_square)
  17.  
  18. def main():
  19.     with open("C-small-practice.in", "r") as f:
  20.         lines = f.readlines()[1:]
  21.         with open("out1.txt", "a") as w:
  22.             for line in enumerate(lines, start=1):
  23.                 nums = map(int, line[1].split())
  24.                 run(line[0], nums[0], nums[1])
  25.                 w.write(run(line[0], nums[0], nums[1]))
  26.  
  27. if __name__ == '__main__':
  28.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement