Section numbers are strings of dot-separated integers. The highest level sections (chapters) are numbered 1, 2, 3, etc. Second level sections are numbered 1.1, 1.2, 1.3, 2.1, 2.2, 2.3, etc. Next level sections are numbered 1.1.1, 1.1.2, 1.1.2, 1.2.1, 1.2.2, erc. There is no bound on the number of sections a document may have, nor is there any bound on the number of levels. 

A section of a certain level may appear directly inside a section several levels higher without the levels between. For example, section 1.0.1 may appear directly under section 1, without there being any level 2 section. Section 1.1 comes after section 1.0.1. Sections with trailing ".0" are considered to be the same as the section with the trailing ".0" truncated. Thus, section 1.0 is the same as section 1, and section 1.2.0.0 is the same as section 1.2. 

```if:python
Write a function `compare(section1, section2)` that returns `-1`, `0`, or `1` depending on whether `section1` is before, same as, or after `section2` respectively.
```
```if:javascript
Write a function `cmp(section1, section2)` that returns `-1`, `0`, or `1` depending on whether `section1` is before, same as, or after `section2` respectively.
```
```if:haskell
Write a function `cmp section1 section2` that returns `LT`, `EQ` or `GT` depending on whether `section1` is before, same as, or after `section2` respectively.
```

Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code:
def compare(s1, s2):
	

Solutions:
def compare(s1, s2):
	(v1, v2) = (version(s1), version(s2))
	return -1 if v1 < v2 else 1 if v1 > v2 else 0

def version(s):
	v = [int(n) for n in s.split('.')]
	while v[-1] == 0:
		v = v[0:-1]
	return v


def compare(s1, s2):
	(s1, s2) = (s1.split('.'), s2.split('.'))
	for i in range(abs(len(s1) - len(s2))):
		min(s1, s2, key=len).append('0')
	for (num1, num2) in zip(s1, s2):
		(num1, num2) = (int(num1), int(num2))
		if num1 > num2:
			return 1
		elif num1 < num2:
			return -1
	return 0


from itertools import zip_longest as z
compare = lambda s1, s2: next(([1, -1][int(i) < int(j)] for (i, j) in z(s1.split('.'), s2.split('.'), fillvalue='0') if int(i) != int(j)), 0)


import re
from distutils.version import LooseVersion
TRAILING_ZEROS = re.compile('(\\.0+)+$')

def cmp(a, b):
	return (a > b) - (a < b)

def compare(s1, s2):
	return cmp(LooseVersion(TRAILING_ZEROS.sub('', s1)), LooseVersion(TRAILING_ZEROS.sub('', s2)))


def compare(s1, s2):
	(s1, s2) = (s1.split('.'), s2.split('.'))
	if len(s1) < len(s2):
		s1 += ['0'] * (len(s2) - len(s1))
	elif len(s1) > len(s2):
		s2 += ['0'] * (len(s1) - len(s2))
	for i in range(len(s2)):
		if int(s1[i]) > int(s2[i]):
			return 1
		elif int(s1[i]) < int(s2[i]):
			return -1
	return 0


def compare(s1, s2):
	s1 = list(map(int, s1.split('.')))
	s2 = list(map(int, s2.split('.')))
	dif = abs(len(s1) - len(s2))
	s1.extend([0] * dif) if len(s1) < len(s2) else s2.extend([0] * dif)
	for (val_1, val_2) in zip(s1, s2):
		if val_1 != val_2:
			return 1 if val_1 > val_2 else -1
	return 0


def compare(s1, s2):
	(n1, n2) = ([int(i) for i in s1.split('.')], [int(i) for i in s2.split('.')])
	l = min(len(n1), len(n2))
	for i in range(l):
		if n1[i] == n2[i]:
			continue
		return 1 if n1[i] > n2[i] else -1
	if any(n1[l:] + n2[l:]):
		return 1 if len(n1) > len(n2) else -1
	return 0