PY-45

Measure Text and Stored Bytes

  • Easy–Medium
  • Text and Bytes
  • Python

Task

Write measure_text(text). Encode text as UTF-8 and return a dictionary with exactly these counts:

  • bytes: number of encoded bytes;
  • characters: number of Python characters;
  • lines: number of text lines;
  • whitespace: number of characters for which character.isspace() is true;
  • non_ascii: number of characters whose ord(character) is greater than 127.

For line counting, treat \n, \r, and \r\n as line endings, with \r\n counted once. Empty text has zero lines. Otherwise, each ending closes one line, and remaining text after the last ending forms one unterminated line. A final ending does not create another line.

Example

é is one Python character but two UTF-8 bytes. The two characters in CRLF are both whitespace, while together they close one line.

Your implementation

Edit solution.py and keep this function signature:

Return a new dictionary. Do not read a file, print, or ask for input.