Python Tricks and Tips for Competitive Coding
Tricks to become unique competitors
Python is one of the programming languages which makes everything easier and straight forward. Anyone who has dabbled in python for competitive coding gets somewhat addicted to its beautiful features. Here is a list of some cool features of Python that I found useful in a competitive coding environment.
1. The most _common function of the Counter Package:
This is probably the most useful function I’ve ever used and it's always at the back of my mind while writing any python code. This function analyses a list and helps to return the top n entities in the list according to their number of occurrences in descending order where n is a number that is specified by the programmer. The individual entities are returned along with their number of occurrences in a tuple which can easily be printed as and when required.
from collections import counter
my_list = [1, 3, 4, 1, 2, 1, 1, 3, 4, 3, 5, 1, 2, 5, 3, 4, 5]
counter = Counter(my_list)
top_three_elements = counter.most_common(3)
print(top_three_elements)
Output:
[(1,5), (3,4), (4,3)]
Look at the second element of each tuple. The value 1 occurs for 5 times which is the most common element. So that the value and count pair of 1 is the first element. The Counter() takes number of most common elements we need to print. It is a quick way to access the problems such as second most common element in a list.
2. The n-largest/n-smallest function of the heapq Package:
The functions nlargest() and nsmallest() are used to get the certain number of largest and smallest values from a list.
import heapq
grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90]
print(heapq.nlargest(3, grades))
print(heapq.nsmallest(4, grades))
Output:
[110, 95, 90]
[20, 25, 33, 38]
The first line of output gives 3 of the largest numbers present in the list grades. Similarly the second line of output prints out 4 of the smallest element present in the list grades. Another specialty of this function is that it does not overlook repetitions. So in place of n if we were to place the length of the array we would end up with the entire sorted array itself.
3. Dictionary and concept of zipping Dictionaries:
Dictionary in python is a fascinating data structure that has many advantages. They are stored as a key and value pair in the form of a list like structure. Each value can be accessed with its corresponding key. The zip function is used to join two lists together or we can even join the key and value pairs in a dictionary together as a single list. The application of this concept will be made clear in the following code snippet.
import heapq
stocks = {'Goog' : 520.54,
'FB' : 76.45,
'YHOO' : 39.28,
'AMZN' : 306.21,
'APPL' : 99.76}
zip_1 = zip(stocks.values(), stocks.keys())
print(sorted(zip_1))
zip_2 = zip(stocks.keys(), stocks.values())
print(sorted(zip_2))
Output:
[(39.28, 'YHOO'), (76.45, 'FB'), (99.76, 'APPL'), (306.21, 'AMZN'), (520.54, 'Goog')]
[('AMZN', 306.21), ('APPL', 99.76), ('FB', 76.45), ('Goog', 520.54), ('YHOO', 39.28)]
4. The Map function:
This function is a sneaky little shortcut that allows us to implement a simple function on a list of values in a very Unconventional Manner. The following example will give a simple application of this functionality. The function takes as parameters the function name and the name of the list the function needs to be applied upon.
income = [10, 30, 75]
def double_money(x):
return x*2
updated_income = list(map(double_money, income))
print(updated_income)
Output:
[20, 60, 150]
Here, we just implemented a simple function which multiplies each list value by two and returns it's as a new list.
5. Concatenation of list of strings:
Suppose we have been given a list of strings and we have to give the output by concatenating the list Let’s look at the previous code what we were doing:
string = ' '
my_list = ['I', 'Love', 'Python']
for x in my_list:
string = string + x
print(string)
This method of joining a list of strings is definitely not the best method because every time a new string will be created
my_list = ['I', 'Love', 'Python']
string = ' '.join(my_list)
print(string)
Using the join() function is memory efficient as well as handy to write which definitely proves to be the advantages over the previous code.
Closing Thoughts:
Individually these functions might look innocent but will definitely come in handy in a Time-limited coding environment in the sense that they offer large functionality in a very short amount of code. The functionalities discussed have very specific applications and act like a Shortcut or a cheat-sheet in competitive coding. Having these useful tricks up your sleeve might just give someone the competitive edge that they were looking for. Thanks for reading this article, you can follow me on Vocal.media.
Happy Learning & Happy Coding!!!!!!!!!!




Comments
There are no comments for this story
Be the first to respond and start the conversation.