Googling for Beginners

From Grundy
Jump to navigation Jump to search

This is a guide to help you out with improving your googling skills, which would positively effect the level of your code. A problem statement has been chosen so as to match the level of a beginner. We would be relying on our googling skills to write a Python code for the given problem statement.

Problem Statement

Given a list of students along with the marks they scored in a test, in a text file, we have to find the student with highest marks

Understanding the problem

Let us first understand what the question wants out of us. Make a list of all the steps that you know, or you may need it while solving this problem. The list will somewhat look like this:

  • Reading input from a text file
  • Storing the data in a variable.
  • Sorting the obtained data in order of marks.

Step 1: Reading input from a text file

Now, our first task is to read input from a text file and store them in a variable. Let us do our first google search. Before any search, know what exactly you want out of the search. So, here we go:

Search number 1 : Read a file in python

Error creating thumbnail: File missing

The google result of the above query is displayed as a screen shot. The first result is from stack overflow, which is a very reliable source for your coding queries. So I opened this link and got a method of loading data from a text file:

 with open('input.txt','r') as f:
     content = f.readlines()


Task Find what this 'r' means

Write this in a python file. Now I want to see whether it served my purpose or not. I want to see what is stored in the variable content. So I add a print command.

Task : Goggle how to print a variable in python 2.x if you don't know and verify it in the next line.

   print content


Now save it in a .py file and execute it (you must be knowing how to run it in Ubuntu or Windows, if not google it!). Don't forget to add an input.txt file in the same directory as that of the python script.

You will get a result something like this:

Output 1.png


This doesn't look great. I can figure out the following discrepancies:

  • One line has been stored as a single element of the list. (Tip: Google and read about list data type in python)
  • There is a '\n' at the end of each line. What's that? Ans: Google it!!

Search number 2 : What is \n and how to remove it in python

Error creating thumbnail: File missing


Search number 3 : How to split a string using spaces in python

Error creating thumbnail: File missing

The solution of this problem is to use a function rstrip() which removes the '\n' from a string and to use the function split(' ') which splits a string based on the input character (here it is ' ').

But, since we need to do this for all the strings stored in the variable content, we used something called as for loop.

Search number 4 : For loop syntax in python

Try finding out the syntax on your own and compare with that given below.


   for i in range(0,len(content)):
       content[i]=content[i].rstrip()
       temp=content[i].split(' ')


temp is made to store the list having the name and marks.

Step 2: Storing the data in a variable

Now , we make a new variable which would store the temp variable created in each step. Name it marks_list. Initialise it with null list.

Add this before the start of loop:

   marks_list=[]

Search number 5 : How to add values to a list in each iteration python

You will come across a function called as append(). It adds variables at the end of a list. (Task: Find ways to add variables in the middle or at the beginning)((

Use append() to add a list (with first element as marks in integer type (not string) and second as name ) in the marks_list list.

   marks_list.append([float(temp[1]),temp[0]])

Step 3: Sorting the obtained data in order of marks

Now lastly, we want to sort marks_list list in descending order.

Search number 6 : Sort list in decreasing order in python

   marks_list.sort(reverse=True)

Checking for errors

My 'claimed' solution program is:

   with open('input.txt','r') as f:
       content=f.readlines()
   marks_list=[]
   for i in range(0,len(content)):
       content[i]=content[i].rstrip()
       temp=content[i].split(' ')
       marks_list.append([float(temp[1]),temp[0]])
   marks_list.sort(reverse=True)
   print marks_list[0][1]


But the above code produces the following error:

Output 2.png

What to do in this case?

Debugging

In case of errors, try printing each variable associated to the erroneous line and try to observe. We observe that in the above example, variable content has an extra last element, which is a null list. This is the reason for the error list index out of range One error is found, it's easy to debug.

Add a check statement before appending temp in marks_data. It is obvious that there must be atleast two words per line, hence add an if statement to check the length of variable 'temp'

   if len(temp)>1:
       marks_list.append([float(temp[1]),temp[0]])


Final code

With all our hard work and efforts, we arrive at the following code:


   with open('input.txt','r') as f:
       content=f.readlines()
   marks_list=[]
   for i in range(0,len(content)):
       content[i]=content[i].rstrip()
       temp=content[i].split(' ')
       if len(temp)>1:
           marks_list.append([float(temp[1]),temp[0]])
   marks_list.sort(reverse=True)
   print marks_list[0][1]


The above program when executed gives the correct result!!

Final output.png

Conclusion

Always frame the program before actually writing it, this would save a lot of time and will ensure you don't mess up the initial part.

Happy Coding!!

Problems for practice

  • Given a list of n names in a text file, create a new text file which have names of all the people whose name starts with 'A'.
  • Write a function to compute the factorial of an integer. (You'll learn how to implement a function.)
  • List the functions you used in this tutorial and learn the variations of it.