-4

Elevator Maintenance

You've been assigned the onerous task of elevator maintenance - ugh! It wouldn't be so bad, except that all the elevator documentation has been lying in a disorganized pile at the bottom of a filing cabinet for years, and you don't even know what elevator version numbers you'll be working on.

Elevator versions are represented by a series of numbers, divided up into major, minor and revision integers. New versions of an elevator increase the major number, e.g. 1, 2, 3, and so on. When new features are added to an elevator without being a complete new version, a second number named "minor" can be used to represent those new additions, e.g. 1.0, 1.1, 1.2, etc. Small fixes or maintenance work can be represented by a third number named "revision", e.g. 1.1.1, 1.1.2, 1.2.0, and so on. The number zero can be used as a major for pre-release versions of elevators, e.g. 0.1, 0.5, 0.9.2, etc (Commander Lambda is careful to always beta test her new technology, with her loyal henchmen as subjects!).

Given a list of elevator versions represented as strings, write a function solution(l) that returns the same list sorted in ascending order by major, minor, and revision number so that you can identify the current elevator version. The versions in list l will always contain major numbers, but minor and revision numbers are optional. If the version contains a revision number, then it will also have a minor number.

For example, given the list l as ["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"], the function solution(l) would return the list ["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]. If two or more versions are equivalent but one version contains more numbers than the others, then these versions must be sorted ascending based on how many numbers they have, e.g ["1","1.0", "1.0.0"]. The number of elements in the list l will be at least 1 and will not exceed 100.

i know this question is not that easy as is shown,i don't know what i forgetting in this question

my program :

def solution(l):
    l.sort()
    print(l)

my error when verify :

  • test case 1 failed.
  • test case 2 failed.
  • test case 3 failed (hidden).
  • test case 4 failed (hidden).
  • test case 5 failed (hidden).
  • List item
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
  • you need to provide a specific key function in order to sort according to the condition, also look up https://docs.python.org/3/library/functions.html#sorted – Devesh Kumar Singh Jun 14 '19 at 06:00
  • you are write but my output are same when i use test case input – vinod karadiya Jun 14 '19 at 06:03
  • 2
    if your code was correct, the test cases wouldn't have failed also your code gives `['1.0', '1.0.12', '1.0.2', '1.1.2', '1.3.3']` as output instead of `["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]` – Devesh Kumar Singh Jun 14 '19 at 06:06
  • Duplicate: [sorting-a-list-of-dot-separated-numbers-like-software-versions](https://stackoverflow.com/questions/2574080/sorting-a-list-of-dot-separated-numbers-like-software-versions) – Patrick Artner Jun 14 '19 at 07:37

1 Answers1

2
# user input
x=["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"]
"""
sorting orignal input according to user requirement, so magic is happening in 
key section, what is happening here is first i am spliting the each elemnt to
a list of integer, ie "1.1.2"-->[1,1,2] , and then i am giving the order in
which sorting has to be done ie inner level sorting, like first sort on first
element, values whose first element are same then sort on the second element
and so on.
"""
result = sorted(x, key=lambda x:[int(i) for i in x.split('.')])
print(result)

output

['1.0', '1.0.2', '1.0.12', '1.1.2', '1.3.3']
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • thanks prashant for your help , your code is giving me write output but its failed in verifying process in foo bar – vinod karadiya Jun 14 '19 at 06:24
  • 1
    @PatrickArtner explanation is done. – sahasrara62 Jun 14 '19 at 06:27
  • @vinodkaradiya it's better if you provide the inputs for which the code fails – Devesh Kumar Singh Jun 14 '19 at 06:34
  • i have only this two test case 2/5 Input: solution.solution(["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"]) Output: 0.1,1.1.1,1.2,1.2.1,1.11,2,2.0,2.0.0 Input: solution.solution(["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"]) Output: 1.0,1.0.2,1.0.12,1.1.2,1.3.3 – vinod karadiya Jun 14 '19 at 06:38
  • @vinodkaradiya may be this is time complexity issue is there, that is why it is getting wrong – sahasrara62 Jun 14 '19 at 07:16
  • @vino this is a challenge for you - so you get invited to a google cs interview. if you can not solve it - do you think you will pass the interview? You need to read the instructions carefully - you are NOT supposed to print the solution - you need to return the sorted list of strings - if you do additional stuff (like printing) the test-validator might invalidate your try... – Patrick Artner Jun 14 '19 at 07:35