Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. def count_lines(lst):
  2. """ (list of str) -> int
  3.  
  4. Precondition: each str in lst[:-1] ends in \n.
  5.  
  6. Return the number of non-blank, non-empty strings in lst.
  7.  
  8. >>> count_lines(['The first line leads off,\n', '\n', ' \n',
  9. ... With a gap before the next.\n', 'Then the poem ends.\n'])
  10. 3
  11. """
  12.  
  13. total_lines = 0
  14. blank_lines = 0
  15. nonblank_lines = 0
  16.  
  17. #goes through each element of the list
  18. for line in lst:
  19. total_lines += 1
  20. if line.isalnum() or line.isspace():
  21. blank_lines += 1
  22. else:
  23. nonblank_lines += 1
  24. print(total_lines, blank_lines, nonblank_lines)
  25. return nonblank_lines
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement