Competitive Programming with Python : Getting Started 1

This is for those who know Python and want to engage themselves in doing Competitive Programming with CodeChef.

So, what comes to your mind as you think of competitive programming, solving complex questions, designing tough algorithms and what not?

But, before diving into this, you need to know about basic input/output methods used in competitive programming.

In this article, we will talk about taking Inputs with Python.

So, First, we will learn about inputting a single Number and Text/String.
For string - we use input()
For Integers - we use int(input())
For floats - we use float(input())

Why didn’t we use str() in the first case? This is because, in Python, the input’s default datatype is str only.
I suppose that you already know these basics.

Now, what about an array (or List/Tuple) of Strings?
In competitive programming, lists are not given with square brackets [ ] or commas ‘,’, neither tuples with ‘( )’. There are space-separated elements given to form an array.

For Eg, if we have to input the following array -
Apple Banana Orange

We can input it using the input() function, but, that would give us ‘Apple Banana Orange’ as input, and not [‘Apple’, ‘Banana’, ‘Orange’].
Do you recall any function for that? If no, it’s the .split() function.

Now, to input the above List, we will use input().split()
That would give us [‘Apple’, ‘Banana’, ‘Orange’] as a list.

Notice that we never used str(), because the input is set to default as a string.

And why did it split the input at spaces (’ ‘)?
That is because space, ’ ’ is the default parameter of the .split() function.
To split the input at ‘#’ for example, use string.split(’#’).
For Eg - ‘A#B’.split(’#’) will give [‘A’, ‘B’]

Okay, So this will help you with solving some problems.
But what about problems involving a list of numbers?

Keep on reading the next article for that!
Python and Competitive Programming : Getting Started 2

Some Problems you can try now -

You can always refer to the editorials if you get stuck anywhere!

Do not hesitate to ask any doubts emerging in your mind!

Thanks for Reading !

Please Share if you like it.