Skip to content
pramode edited this page Jul 26, 2011 · 5 revisions

Problems

  1. Write a program which will open a text file and print the last 5 lines

  2. Write a program which will print the length of the longest line in a text file

  3. Suppose you have a text file each line of which is a single word; for example:

       hello
       ant
       cat
       elephant
    

Write a Python program which will display the lines in sorted order. The output in the above case should be:

     ant
     cat
     elephant
     hello
  1. Write a Python program which will print all lines of a text file which contains the following pattern:

     An upper case A followed by any number of digits ending with an upper case B
    

For example, if the file contains lines like these:

    helloA01123Bpqr
    A9812BCqtmn

those lines should be printed on to the screen. Use regular expressions for solving this.

  1. Write a Python program which will find out the largest file in the current directory. (hint: use "listdir" function from "os" module to find out names of all files; use the "stat" function from "os" module to get size of each file)

  2. Read about the "binary search" algorithm on the net. Write a Python function to implement the binary search algorithm. The function should look like this:

          def binsearch(a, n):
               # Check whether number "n" is present in a sorted list "a"
               # If present, return index of the location where the number is present
               # Otherwise, return -1
    
Clone this wiki locally