{"id":25785,"date":"2021-12-16T09:17:25","date_gmt":"2021-12-16T03:47:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=25785"},"modified":"2021-12-16T09:17:25","modified_gmt":"2021-12-16T03:47:25","slug":"what-do-you-need-to-know-about-competitive-python-programming","status":"publish","type":"post","link":"https:\/\/python-programs.com\/what-do-you-need-to-know-about-competitive-python-programming\/","title":{"rendered":"What Do You Need to Know About Competitive Python Programming?"},"content":{"rendered":"

Hello there, programmer! I’m sure you’re aware of what Competitive Programming is. However, there are a few things to keep in mind when coding in Python. These minor changes can make a significant difference in your code.<\/p>\n

Python Competitive Programming:<\/strong><\/h2>\n
1. Using Generators in Python<\/strong><\/h5>\n

Python includes a generator that allows you to write your own iterator function. A generator is a type of function that returns an iterator object with a sequence of values rather than a single value. A yield statement, rather than a return statement, is used in a generator function.<\/p>\n

For Example:<\/strong><\/p>\n

def Python_generators():\r\n    yield 15\r\n    yield 20\r\n    yield 30\r\n\r\n\r\nfor itr in Python_generators():\r\n    print(itr)\r\n<\/pre>\n

It is also useful for returning multiple values sequentially at the same time.<\/p>\n

2. Using built-in Functions in Python<\/strong><\/h5>\n

Python includes a number of functions that are readily available for use. Using built-in functions and libraries is preferable to the traditional method.<\/p>\n

Some of the built-in Function Examples are:<\/p>\n

1.len():<\/strong> The length of an object is returned by this method.<\/p>\n

gvn_lst = [10, 20, 30, 40, 50]\r\nlst_len = len(gvn_lst)\r\nprint(\"The length of the given list = \", lst_len)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The length of the given list =  5<\/pre>\n

2. min():<\/strong> Returns the minimum element in the given list.<\/p>\n

gvn_lst = [90, 20, 30, 40, 50]\r\nminmum_ele = min(gvn_lst)\r\nprint(\"The minimum element in the given list = \", minmum_ele)<\/pre>\n

Output:<\/strong><\/p>\n

The minimum element in the given list =  20<\/pre>\n

3. max():<\/strong> Returns the maximum element in the given list.<\/p>\n

gvn_lst = [90, 20, 30, 40, 50]\r\nmaxmum_ele = max(gvn_lst)\r\nprint(\"The maximum element in the given list = \", maxmum_ele)<\/pre>\n

Output:<\/strong><\/p>\n

The maximum element in the given list = 90<\/pre>\n

4. lower():<\/strong> Converts the given string into lowercase.<\/p>\n

gvn_str = \"HELLO BTECHgeeks\"\r\nlwr_str = gvn_str.lower()\r\nprint(\"The given string after converting into lowercase = \", lwr_str)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The given string after converting into lowercase =  hello btechgeeks<\/pre>\n

5. upper():<\/strong> Converts the given string into uppercase.<\/p>\n

gvn_str = \"welcome all\"\r\nupr_str = gvn_str.upper()\r\nprint(\"The given string after converting into uppercase = \", upr_str)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The given string after converting into uppercase =  WELCOME ALL<\/pre>\n

Similarly, we have many built-in functions which make code easier.<\/p>\n

3.itertools in Python:<\/strong><\/h5>\n

The itertools module can be extremely useful in solving some complex problems.<\/p>\n

itertools.permutations() method:<\/strong> This method returns all the possible permutations of a given list.<\/p>\n

for example:<\/strong><\/p>\n

import itertools\r\ngvn_lst = ['a', 'b', 'c']\r\nrslt = itertools.permutations(gvn_lst)\r\nrslt_lst = list(rslt)\r\nprint(\"The all permutations of the given list are :\")\r\nprint(rslt_lst)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

The all permutations of the given list are :\r\n[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]<\/pre>\n

The same thing can be done by writing your own logic and functions, but it will be far more complex and time-consuming.<\/p>\n

4.map() Function in Python:<\/h5>\n

When we need to take input from all the elements of an integer array in a single line separated by white spaces, the map function is our best.<\/p>\n

Using the map function reduces the complexity of dealing with multiple values entered on a single line.<\/p>\n

For example:<\/strong><\/p>\n

# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngivn_lst = list(map(int, input(\r\n   'Enter some random List Elements separated by spaces = ').split()))\r\nprint(\"The given list = \",givn_lst)\r\n\r\n<\/pre>\n

Output:<\/strong><\/p>\n

Enter some random List Elements separated by spaces = 20 46 72 35\r\nThe given list = [20, 46, 72, 35]<\/pre>\n
5. Python String Concatenation:<\/h5>\n

In Python, To concatenate multiple strings, we have two options:<\/p>\n