Minimal Lines of Code



So, it's 11:20 p.m. and I just came across one of the finest problems in HackerRank to revise and also to test the concepts of Any and All in Python, they might look like they are of not much use but this problem showed me how to utilize them in the best possible manner. 
We are getting 2 inputs, first is the size of the array and the second one is the array elements in one line(space separated), although I think the number of elements was not needed at all and this costed me 20 mins later in the problem. 
Basically, we need to check 2 conditions:
1) All the elements of the array must be positive(greater than 0)
2) At least 1 elem in the array should be a Palindromic Integer
If either of these are False then the output should be False and True otherwise.
The key part is the use of maps along with lambda functions which is the overall soul of this problem,
Firstly I am using a map to check if elem is positive and getting a list of boolean values from this... and then using all function in this list, so this all function should return True, that is, every value in the list should be True, Secondly, using another lambda func to check for palindromic integers, using the world-famous list slicing of Python, converting an int to string and then reversing the whole string and then checking if it is equal to another string which itself has been converted from an int and sliced.... I mean what the hell, this would have taken around 5 lines of code in Cpp. And finally combining this whole checking into a one liner perfect Ternary Operation in Python. I could have also increased the number of checkings in the statement but then it would be so unreadable, so I dropped that idea.
It's around 11:30 now, I will be doing some duo then daily lessons and finally off to bed.
Peace!

N=int(input())
ls=list(map(int,input().split()))
print("False") if all(list(map(lambda x:x>=0,ls))) is False or 
any(list(map(lambda x:str(x)==str(x)[::-1],ls))) is False else print("True") 

 

Comments

Popular Posts