OILS / benchmarks / compute / palindrome.py View on Github | oilshell.org

49 lines, 31 significant
1#!/usr/bin/env python2
2"""
3unicode.py
4"""
5from __future__ import print_function
6
7import re
8import sys
9
10
11def main(argv):
12 decode = False
13 if argv[1] == 'unicode':
14 print('palindrome.py: unicode', file=sys.stderr)
15 decode = True
16
17 for line in sys.stdin:
18 line = line.rstrip() # remove newlines and spaces
19
20 if len(line) == 0: # skip blank lines
21 continue
22
23 if decode:
24 seq = line.decode('utf-8')
25 else:
26 seq = line
27
28 n = len(seq)
29
30 h = n // 2 # floor division
31
32 #print('n = %d, h = %d' % (n, h))
33
34 palindrome = True
35 for i in xrange(h):
36 if seq[i] != seq[n-1-i]:
37 palindrome = False
38 break
39
40 if palindrome:
41 print(line)
42
43
44if __name__ == '__main__':
45 try:
46 main(sys.argv)
47 except RuntimeError as e:
48 print('FATAL: %s' % e, file=sys.stderr)
49 sys.exit(1)