HackerRank : Python #1
Well I was thinking of starting to introduce myself with Python and I thought let's start Python with all of you.
What I meant by this is, I will be practising Python on the famous website i.e., HackerRank, where I will be solving problems, and sharing it with all of you, well if you also start with me, we can compete with each other.
So today I signed up and had solved 2-3 problems of array under the Data Structure. But my main focus was of Python, so inside Python, I was given my first problem as to use If_Else condition to print something. In python or in any other language the main thing to do or to understand is the looping and conditional statements. In my college we had been taking special lectures on Data Structures where we got to learn that Looping and Conditions are main things to understand in any coding language.
When you understand these you can understand what the code is all about and what you have to do. Well in my problem, it was stated like,
Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive range of to , print Not Weird
If is even and in the inclusive range of to , print Weird
If is even and greater than , print Not Weird
So I think for a while(i got idea), then I just typed this,
if(n%2==0):
if(n>=2 and n<=5):
print("Not Weird")
elif (n>=6 and n<=20):
print("Weird")
else:
print("Not Weird")
else:
print("Weird")
Here is little explanation, the first if will check if the number user entered is even or not, then it will allow to enter only if the number is even , otherwise it will be reverted to else part, where it will directly print that the number is weird, according to the problem statement.
If the number is even, it will enter and according to the range where the number fits it will print accordingly. For range 2 to 5, Print not weird. For range 6 to 20, Print Weird. And for above 20, Print not weird.
So this is how we will solve our problem, and learn something about, If_Else Statement in Python.
Next time we will be solving next problem, till then if you wanna solve more, go for it, just make sure you left aome problems for me. 😉
Till next time, #Coders😎
Comentários