Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys, time
- def count_string(line):
- for i in xrange(len(line[0])):
- letters = [0] * 26
- # print "i = ", i
- for word in line:
- # print "word = ", word
- if i > len(word)-1:
- break
- letters[ord(word[i]) - 97] += 1
- print "%5d - %-3s" % (i, chr(letters.index(max(letters)) + 97))
- yield chr(letters.index(max(letters)) + 97)
- def max_chars(line):
- line = line.split()
- line.sort()
- line.sort(key=len, reverse=True)
- return [val for val in count_string(line)]
- import unittest
- class TestStringCount(unittest.TestCase):
- def test_count(self):
- self.assertEqual(max_chars("foo foo foo bar foobar baz foobaz alta beta"),\
- ['f','o','o','a','a','r'])
- self.assertEqual(max_chars("a man a plan a canal panama"),\
- ['a','a','n','a','l','a'])
- # More unit tests
- if __name__ == "__main__":
- suite = unittest.TestLoader().loadTestsFromTestCase(TestStringCount)
- unittest.TextTestRunner(verbosity=2).run(suite)
- # Uncomment to get input from stdin
- # for line in sys.stdin.readlines():
- # # could sort here first by length of words
- # print "line = ", line
- # count_string(line)
Add Comment
Please, Sign In to add comment