fork download
  1. import sys
  2.  
  3. def count_letters_loop(my_neat_thing: str) -> int:
  4. size: int = 0
  5. for letter in my_neat_thing:
  6. size = size + 1
  7.  
  8. return size
  9.  
  10. def count_words_loop(sentence: str) -> int:
  11. words: int = 0
  12. # Use strip() to get rid of extraneous spaces
  13. for letter in sentence.strip():
  14. if letter == ' ':
  15. words += 1
  16. # Ensure to count the final word in the sentence.
  17. words += 1
  18.  
  19. return words
  20.  
  21. def count_words_easy(sentence: str) -> int:
  22. wordlist: list[str] = sentence.strip().split(' ')
  23. length: int = len(wordlist)
  24. return length
  25.  
  26. def main() -> None:
  27. inputstring = sys.stdin.read()
  28. print(inputstring)
  29. length: int = count_letters_loop(inputstring)
  30. print(f'length: {length}')
  31.  
  32. python_length = len(inputstring)
  33. print(f'Python thinks it is: {python_length}')
  34.  
  35. word_count: int = count_words_loop(inputstring)
  36. print(f'Word count: {word_count}')
  37.  
  38. builtin_count: int = count_words_easy(inputstring)
  39. print(f'Word count with builtin: {builtin_count}')
  40.  
  41. with_underscores: str = inputstring.replace(' ', '_')
  42. print(f'Spaces replaced: {with_underscores}')
  43.  
  44. if __name__ == '__main__':
  45. main()
  46.  
Success #stdin #stdout 0.04s 9688KB
stdin
this is a test
stdout
this is a test
length: 14
Python thinks it is: 14
Word count: 4
Word count with builtin: 4
Spaces replaced: this_is_a_test