10-13 Basics

Python proficiency interlude.

Python proficiency questions.

  1. Suppose x is an object of class CC. What method is run by str(x)?

  2. Give an efficient way to remove the trailing newlines from an array of strings read from a file.

     ['the\n','mouse\n','went\n','up\n','the\n','clock\n']
    
  3. When you read the lines from a file, you get to following:

     ['\n','\n','\n','NVERT 2\n','0 1\n','\n']
    

    There are a bunch of blank lines that should be ignored. What do you recommend to do about this annoyance? Note: totally empty lines are almost as annoying as lines with newlines. Write code show how you plan to handle the problem, beginning with f = open("demo.txt","r").

  4. (fnsr) A file f consists of two numbers and a string. Write a function fnsr to determine if the sum of the two numbers equals the number of characters in the string. the input to fnstr is the already open file returned from open. The output is a boolean. Examples for True and False.

       5 8
       abcdefghijklmnopqrstuvwyzabcdefghinjlmno
    
       1 3
       four