Googling for Beginners
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.
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
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.
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:
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
Search number 3 : How to split a string using spaces in python
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:
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.
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
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),temp]) marks_list.sort(reverse=True) print marks_list
But the above code produces the following error:
What to do in this case?
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),temp])
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),temp]) marks_list.sort(reverse=True) print marks_list
The above program when executed gives the correct result!!
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.
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.