Rozwiązałem również jest pytanie za pomocą Python, chociaż moja wersja robi kilka dodatkowych rzeczy, oto mój kod:
def answer(l):
list1 = [] # this is the list for the nested strings
for x in l:
list1.append(x.split("."))
list2 = [] # this is the same list as list one except everything is an integer in order for proper sorting
for y in list1:
y = map(int, y)
list2.append(y)
list3 = sorted(list2) #this is the sorted list of of list 2
FinalList = [] # this is the list that converts everything back to the way it was
for a in list3:
a = '.'.join(str(z) for z in a)
FinalList.append(a)
return FinalList
Dla wersji istnieją trzy rzeczy; Major, Minor i rewizja. Dzieje się tak dlatego, że organizuje to tak, aby '1'
pojawiło się przed '1.0'
, co nastąpi przed '1.0.0'
. Kolejny plus, nie ma potrzeby importowania żadnych bibliotek, ale nie masz ich i działa ze starszymi wersjami Pythona, ten był przeznaczony specjalnie dla wersji 2.7.6. W każdym razie, oto kilka przykładów:
Inputs:
(string list) l = ["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"]
Output:
(string list) ["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]
Inputs:
(string list) l = ["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"]
Output:
(string list) ["0.1", "1.1.1", "1.2", "1.2.1", "1.11", "2", "2.0", "2.0.0"]
Jeśli masz jakieś pytania, po prostu skomentuj odpowiedź !!
Powiązane artykuły [Jak porównać ciągi znaków "styl wersji"] (http://stackoverflow.com/q/11887762) –