Python Musings #1: Reading raw input from Hackkerank Challenges
Want to share your content on python-bloggers? click here.
As some of you may or may not know, Hackkerank is a website that offers a variety practice questions to work on your coding skills in an interactive online environment. You can work on a variety of languages like, Java, C, C++, Python and more! There are a lot of high quality questions that can really challenge your present coding and problem solving skills and help you build on them.
When I started out, I found that reading raw data was more challenging than writing the rest of the solution to the problem; This blog post is to show how to read raw data as lists, arrays and matrices and hopefully shed some light on how to do this in other problems.
I’m sure there are other more effective ways to be reading raw input from Hackerrank, but this has worked for me and I hope it will be helpful for others as well; Sorry in advance if my code appears to be juvenile.
To solve these problems, I will be working with Python 3.
Step 0: Reading Raw input
To read a line of raw input. Simply use the input()
function.
While this is great for reading data. It gives it to us in the raw form, which results in the data being received as a string- which isn’t any good if we want to do calculations.
Now that we know how to read raw input. Lets now get the data to be readable and in the form that we want for solving Hackerrank challenges.
Step 1: Reading Lists
Lets look at a problem that we can to read raw input as a list to solve.
This problem is called “Shape and Reshape“. This involves reading raw, space-separated data and turning it into a matrix.
Turning the data into a matrix can be done by using the numpy package. But to do this, the data first needs to be made into a list. I do this by first reading the raw data with input().strip().split(' ')
.
Lets explain what each part of this code does.
input()
takes in the raw input.
The .strip()
method clears all leading and trailing spaces. While not necessary for our example, it is good practice to do this so as not to have to encounter this problem in other cases.
The .split(' ')
method splits the raw data into individual elements. Because the data is space separated we define the the split to be a space. We could technically write split()
without anything in the middle (the default is white-space) but for this example we are defining the separator as an individual space character.
The problem now is that the data needs to be converted to a proper form. Remember, input()
reads all raw data as a string. For our problem, our data needs to be in integer form. This can be done by iterating the int()
function across the elements in our list.
The code we use is thus:
n = input("Write input: ").strip().split(' ') data=[int(i) for i in n] # Print the data to see our result (Not required for the solution) print(data)
Looking at the for-loop may not seem intuitive when first looking at it if you are used to writing for-loops traditionally. But once you learn how to write for loops this way, it definitely will be more preferred. As someone who has come to learn Python after initially spending a lot of time with R. I would describe this method of writing a for loop analogous to R’s sapply()
or lapply()
functions.
And there you have it! With 2 lines of code our data is ready to solve the problem!
(For actually solving the problem, you will have to figure it out yourself or you can check out my code on my Python Musings Github Repository (Its still a work in progress, messy code and all)
Step 2: Reading Arrays
After looking in the discussions for this problem the raw data can be read directly as an array using the numpy package.
The code is:
import numpy as np data = np.array(input("Write input: ").strip().split(' '),dtype= int) # Print the data to see our result (Not required for the solution) print(data)
Essentially we put our raw input that we put in as a list before directly into numpy’s .array()
function. To coerce the data into integers we define the data type as an integer. Therefore we set dtype=int
in the .array()
function…. and there you have it! An array of integers!
Step 3: Reading Matrices
For reading matrices, lets look at the problem titled “Transpose and Flatten“. This requires reading a matrix of data and transposing it and flattening it. While doing those operations are pretty straight forward. Reading the data might be a challenge.
Lets first look at how the input format is.
We want to have to read data in a way that will let us know the number of rows and columns of the data followed by the elements to read.
To do this, the code is:
import numpy as np n,m =map(int,input().strip().split()) array= np.array([input().strip().split() for i in range(0,n)],dtype=int) print(array)
Lets now break down the code:
Python has a very cool feature that you can assign multiple variables in a single line by separating them by a comma. So we can immediately assign our rows and columns from our input in a single line. To assign differing values, we use the map()
function on the split input. To assure that the values are in integer form we apply the int
function to both variables.
To get the rest of the data, we can read it directly into numpy’s .array()
function, iterating across the number of inputs we know we will be having (i.e. the number of rows). With this, we get the matrix we want for our input and can now work on the problem!
Conclusion
Doing challenges on Hackerrank is a good way to build skills in knowing how to write code and problem solve in Python. I personally found it initially challenging to read data in a form I wanted it. I hope this article shed some light on solving these problems!
Be sure to check out my Python Musing’s Github repository to see where I am in my adventures!
Want to share your content on python-bloggers? click here.