Socks Merchant | HackerRank Interview Warm-up Challenges | Interview Preparation Kit | Python Coding

Опубликовано: 03 Сентябрь 2020
на канале: Shaheer Shukur
1,316
20

Solving the Socks Merchant problem from the Warm-up challenges section of #HackerRank #InterviewPreparationKit, using #Python language. The coding is done from very basic easy to understand perspective to single line advanced coding.

HackerRank Page: https://www.hackerrank.com/challenges...

Closed Captioning:
Starting with this tutorial series, we will solve the HackerRank challenges, which are coming under the Interview Preparation Kit.
Let's solve the Warm Up challenges.
We will start with the first challenge 'Sock Merchant'.
As per the problem statement, we need to find the number of matching colored pairs of socks, from a large pile of socks.
In the function definition, we will be provided with two inputs.
n, the number of socks in the pile.
And, ar, an array containing the colors of the socks.
We need to print the number of colored pair of socks.
For example, in this case, if the input array is this,
the same colored socks can be paired like this.
which gives us 3 pairs.
Let's start coding.
We will be coding in Python 3.
We need to write the logic in this function.

First, let's see the contents of the array, using the print statement.
When we run the code, we can see it in the Debug output.
From this array or list, we need to find the total count of every colored socks.
In this case, there are four 10, three 20, one 30 and one 50.
In python we have Counter function for this.
It is defined in the collections package.
So we need to import it from there.
Let's call the Counter function on the array, and store it in a variable count.
Let's check it's content using print statement.
Here we got the counts of colored socks, in the form of a dictionary data.
We will extract only the counts from the dictionary, using the values function.
We got the counts of colored socks in an array.
From this array, we need to find the total number of pairs possible.
For that, first we will initialize a variable pairs as 0.
Using a 'for' loop, we will iterate through each element of the socks count array.
For each colored socks count, we will do a floor division, and add it to the pairs variable.
In python, double / does the floor division, which gives an integer value.
For example, 3/2 gives the answer 1.5, whereas 3//2 gives the answer 1.
After the iteration, lets's print the number of pairs.
We got the correct answer 3.
From the code, it is understandable that the function is required to return the value.
Instead of printing the result in function, we will return it.
Let's test our code.
The sample test cases passed successfully.
Let's submit the code and test against all test cases.
It's working fine.

Now, let's simplify our code.
In Python, we have something called 'List Comprehension' for creating list, which can be used instead of multi line 'for' loops.
It's syntax is, expression, iterable, and condition.
For example, if we have an iterable list containing some numbers, we can write an expression here to be applied to each element of array.
In this case, each element x will be multiplied by 2.
Also, if required, we can add a condition at the end.
We will get this output.
Let's use List Comprehension in our code, instead of the 'for' loop.
Instead of initializing the variable pairs with 0, we will write list comprehension here.
First, we will iterate through each element.
And for each element, we will do floor division.
Finally, we will use the built in function 'sum' to add up the elements.
Our code is working as expected.
Here, instead of using the assigned variable, we can directly write the function call.
Similarly, this part can be replaced with the original function call.
Also, we can return the single statement directly without assigning it to a variable.
Let's run the code and see.
Perfect!