Basic Exercises


Exercise 1

Port number classification

Write a program that takes a port number as command line argument and print the range of that port number.
Write this program from scratch, use shebang, boilerplate syntax, a seperate function for checking port range.

Port numbers are assigned in various ways, based on three ranges:

  • System Ports - (0-1023)
  • User Ports - (1024-49151)
  • Dynamic Ports - (49152-65535)
    [source: RFC6335]

Example:

verax@humla $ python port_type.py 443
443 is a system port.

Hints:

  • sys.argv for list of command line arguments.
  • Builtin function int() converts strings to integers.
  • Refer to conditionals notes for info on available conditional and logical operators in Python.

Exercise 2

Find string1.py located in the “basic” directory.
Fill in the missing logic.

string1.py – complete the string functions in string1.py, based on the material covered on Python Strings.

Extra mile: Additional exercises available in string2.py but not mandatory.

Exercise 3

Find list1.py located in the “basic” directory.
Fill in the missing logic.

list1.py – complete the list functions in list1.py, based on the material covered on Python Lists and Python Sorting.

Extra mile: If you are quick, additional exercises available in list2.py but not mandatory.

Exercise 4

cat functionality

Write a program cat.py which mimics cat unix utility i.e. for a given file, print the contents of the file line by line.

TASK I - Write a program that takes a filename as command line argument, prints the file name followed by contents of the file, line by line.

TASK II - Improve the above cat program to take multiple filenames as input and print their contents, one file at a time.

Extra mile: Think about how you can implement unix tac functionality. (tac prints contents of a file in reverse, last lines first).

Exercise 5

grep functionality

Write a program grep.py which mimics grep unix utility i.e. for a given file, search the file for a keyword, for lines containing the keyword print the matching lines.

TASK I - Write a program that takes a filename and a keyword as command line arguments. Search the file for the keyword, for every line that has the keyword print the line.

TASK II - Improve the above grep program to take multiple filenames and a keyword as input. Print the filename followed by al the lines that has the keyword in that file.

Extra mile:

  • Think about how you can implement unix head functionality. (output the first n number of lines of a file).
  • Think about how you can implement unix tail functionality. (output the last n number of lines of a file).

Exercise 6

counts.py

Write a program counts.py which mimics wc unix utility that is for a given file, depending on the commandline options, print the number of characters/words/lines in that file.

Example -

verax@humla ~ $ python counts.py -l poem.txt    # prints number of lines
6
verax@humla ~ $ python counts.py -w poem.txt    # prints number of words
36
verax@humla ~ $ python counts.py -c poem.txt    # prints number of characters
124

TASK I - count lines function
Given a filename, print the number of lines in the file.

TASK II - count words function
Given a filename, print the number of words in the file.

TASK III - count characters function
Given a filename, print the number of characters in the file.

HINTS -

  • read method reads the whole file into a single string.
  • readlines reads lines of a file into list.
  • split method splits strings into words
  • len() is an builtin function to find length of sequential data types(strings, lists etc)

Exercise 7

Word frequency

Write a program that takes filename as command line argument and prints all words in the file, with their count(number of times they appear) in descending order of their count.

TASK I - word list function
Extract all the list of words from the file.

TASK II - word count function
For all the words count the number of times they appear.

TASK III - word frequency function
Sort the words by their count and print them in descending order of their count.

HINTS -

  • dictionary is a data structure that works the best for key-value pairs(word-count in this case)
  • dict.get(key, 0) retrives the value for a key, if the key doesn’t exist it returns the int specified as second argument. Thnik about how you can use it to maintain count of words.
  • dict.items() retrives keys and values, think of how you can use this to sort dictionary by value(custom sorting).
  • dict.get returns value for a key, think of how you can use it for sorting dictionary by value.

Exercise 8

Anagrams

  • Given a file and word – find all the anagrams of that word in the file
  • Make this program search work for mutiple files as input.
    (Two words are called anagrams if one word can be formed by rearranging letters of another. For example ‘eat’, ‘ate’ and ‘tea’ are anagrams.)

HINTS -

  • builtin function list() can convert strings into list of characters.
  • lists can be compared using == operators. But the catch is, Items and also their order has to be same.