Python 10 liner


#1
class Solution:
	# @param A : string
	# @param B : string
	# @return an integer
	def compareVersion(self, A, B):
	    vera = list(map(int, A.split('.')))
	    verb = list(map(int, B.split('.')))
	    
	    for ind in range(max(len(vera), len(verb))):
	        
	        anum = vera[ind] if ind < len(vera) else 0
	        bnum = verb[ind] if ind < len(verb) else 0
	        
	        if anum < bnum:
	            return -1
	        elif anum > bnum:
	            return 1
	   
	    return 0

I have splitted the numbers along the dots and then kept comparing the corresponding digits in the version numbers. If the corres number doesn’t exist (in case the length of one version is more than other) I assume the missing one to be zero.


#2

can you please explain the question and why aren’t u considering number after dots


#3

I never said that I am not considering number after dots.
The ques simply asks you to compare version numbers, i.e.
12.5.63.78 > 12.5.9.100

11.8.69.475 > 11.8.69.1 and so on

Taking the first example, I have splitted the string around dot to get list of numbers
list1 = [12, 5, 63, 78]
list2 = [12, 5, 9, 100]

Now I will be comparing the corresponding elements of this list till I find an inequality. When I find and inequality, I can deduce which number of both is greater. Here that will happen when 63 and 9 get compared.

If these lists had different lengths, like
12.5.63.78.11 and 12.5.63.78
then
list1 = [12, 5, 63, 78, 11]
list2 = [12, 5, 63, 78]

Then when I’ll reach the 4th index, I will see that the element in list2 doesn’t existat this index, in which case I will assume that missing element’s value to be 0, because anyways you can write these version numbers as
12.5.63.78.11 and
12.5.63.78.00

Hope that helped.


#4

good work, after running my test case, I got an error of “1.0” and “1”. So I was thinking how can I append 0 in the shorter string. Your solution was clean. Thank you