Python Tricks for Competitive Coding
Python is one such programming language that makes everything easier and straight forward. Anyone who has dabbled in python for Competitive Coding gets somewhat addicted to its many features. Here is a list of some of its cool features that I’ve found most useful in a competitive coding environment.
- The most_common function of the Counter Package.
This is probably the most useful function I’ve ever used and its always at the back of my mind while writing any python code. This function analyses a list/string and helps to return the top n entities in the list/string 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 referred/printed as and when required.
[(1, 5), (3, 4), (4, 3)]
- The output tuple clearly states that 1 has occurred 5 times, 3 has occurred 4 times, and 4 has occurred 3 times.
- The n-largest/n-smallest function of the heapq Package.
This function helps to return the top n smallest/largest elements in any lists and here again n is a number specified by the programmer.
[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 elements present in the list grades. Another speciality 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 then we would end up with the entire sorted array itself !!
- Dictionary and concept of zipping Dictionaries
Dictionaries in python are truly fascinating in terms of the unique functionality that they offer. They are stored as a Key and Value pair in the form of an array like structure. Each value can be accessed by 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.
[(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)]
- 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.
[20, 60, 150]
- Here, we just implemented a simple function which multiplies each list value by two and returns it as a new list.
- 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:
This method of joining a list of strings is definitely not the best method because a new string will be created every time the loop is run.
Using join() function is memory efficient as well as handy to write which definitely proves to be the advantages over the previous code.
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 !!
This article is contributed by Siddhant Bajaj. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.