Guest User

Untitled

a guest
Dec 12th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys, time
  4.  
  5. def count_string(line):
  6. for i in xrange(len(line[0])):
  7. letters = [0] * 26
  8. # print "i = ", i
  9. for word in line:
  10. # print "word = ", word
  11. if i > len(word)-1:
  12. break
  13. letters[ord(word[i]) - 97] += 1
  14. print "%5d - %-3s" % (i, chr(letters.index(max(letters)) + 97))
  15. yield chr(letters.index(max(letters)) + 97)
  16.  
  17. def max_chars(line):
  18. line = line.split()
  19. line.sort()
  20. line.sort(key=len, reverse=True)
  21. return [val for val in count_string(line)]
  22.  
  23. import unittest
  24.  
  25. class TestStringCount(unittest.TestCase):
  26.  
  27. def test_count(self):
  28. self.assertEqual(max_chars("foo foo foo bar foobar baz foobaz alta beta"),\
  29. ['f','o','o','a','a','r'])
  30. self.assertEqual(max_chars("a man a plan a canal panama"),\
  31. ['a','a','n','a','l','a'])
  32. # More unit tests
  33.  
  34. if __name__ == "__main__":
  35. suite = unittest.TestLoader().loadTestsFromTestCase(TestStringCount)
  36. unittest.TextTestRunner(verbosity=2).run(suite)
  37.  
  38. # Uncomment to get input from stdin
  39. # for line in sys.stdin.readlines():
  40. # # could sort here first by length of words
  41. # print "line = ", line
  42. # count_string(line)
Add Comment
Please, Sign In to add comment