r/dailyprogrammer • u/Elite6809 1 1 • Oct 06 '14
[10/06/2014] Challenge #183 [Easy] Semantic Version Sort
(Easy): Semantic Version Sort
Semantic Versioning, or Semver as it's known on the streets, is an attempt to standardise the way that software versions are incrementally changed. In the world there are many different pieces of software whose developers have conflicting ideas about how software should be developed. For example, Dwarf Fortress is currently at version 0.40.13, whereas Google Chrome (which has been around for 2 years less than Dwarf Fortress) is currently at version 37.0.2062.124. How can those version numbers even be compared? They both represent around the same progress of development but in totally different ways. Semantic versioning aims to solve this problem by splitting the version string into 3, 4 or 5 parts:
<major>.<minor>.<patch>-<label>+<metadata>- major: Increased when your program changes in a way that makes it incompatible with older versions (major changes) - like the Python 2 to Python 3 change which, in order to make progress, broke a lot of existing programs.
- minor: Increased when you add functionality but keep compatibility and don't change existing bits of the API (minor changes) - for example, adding a new section of a standard library to a programming language.
- patch: Increased when you make minor functionality changes or bug fixes, like adding a new keyboard shortcut.
- label: Used to indicate pre-release program status, such as beta, alpha or rc2 (release candidate 2.)
- metadata: Used to describe build metadata when a version is in the early development stages - this might include the build date of the program.
For the purpose of this challenge, you will be sorting a list of Semantic Versions into chronological order, and you will do it like so:
- First, compare the major version.
- If they are the same, compare the minor version.
- If they are the same, compare the patch version.
- If those are all the same, check if the version has a label - ignore the content of the label. A version with a label (prerelease) comes before one without a label (final release.)
- Ignore the build metadata.
If the semantic versions are still the same at this point, they can be considered the same version. For the purpose of this challenge we won't attempt to parse the label - but if you're feeling up to you can give it a try!
The full specification for Semantic Versioning can be found here.
Formal Inputs and Outputs
Input Description
You will first be given a number N. You will then be given N more lines, each one with a semantic version.
Output Description
You will print the versions in chronological order, as described by the rules above.
Sample Inputs and Outputs
Sample Input
7
2.0.11-alpha
0.1.7+amd64
0.10.7+20141005
2.0.12+i386
1.2.34
2.0.11+i386
20.1.1+i386Sample Output
0.1.7+amd64
0.10.7+20141005
1.2.34
2.0.11-alpha
2.0.11+i386
2.0.12+i386
20.1.1+i386Tip
If your chosen language supports it, create a SemanticVersion record/structure with the appropriate fields. If your language supports it, overload the comparison (<, >) operators to compare for you.
If your language does not support sorting of data structures by default, you could adjust your solution to the Quicksort challenge to work with this one.
12
u/2i2c 1 0 Oct 08 '14
BLEHHGHHH, I'm so rusty!! Wow.
I brushed off my old beat-up copy of Android Studio and over the course of ~8 excruciating hours yesterday and today, I wrote a sample app that does this, with a working input box, nav bar, etc.
Screenshot: http://imgur.com/6HOqFHv
PM if you want the source, I don't have a good place to upload it right now. My code is pretty sloppy, though, and it doesn't do anything impressive in CS terms
10
u/Rahazan Oct 06 '14
Haskell
My first haskell program.. ever
5
u/MuffinsLovesYou 0 1 Oct 06 '14 edited Oct 06 '14
C# has a ton of sorting options for generics (one of the best things about the language).
version 1.0.0-rough: Calculated input correctly, no error checking
version 1.0.1: removed int conversions based on jnazario's test input containing chars
version 1.1.0-regex: added regex check for input string to sink bad input. added back in int conversions for speed.
version 1.1.1+final: tersed up the syntax, removed a couple unnecessary variable declarations and type conversions.
version 1.1.2+finalforreali: terseness intensifies
3
u/jnazario 2 0 Oct 06 '14
a suggestion, or at least a challenge input. the current sample input is easily sorted using a built-in lexicographical sort. here's some F# to show you what i mean:
i would suggest some changes to it to really force a sorting function to be implemented. here is a suggested challenge input to test against:
4
u/dohaqatar7 1 1 Oct 06 '14 edited Oct 06 '14
There seems to not be a single java solution in all 51 comments. I'll fix that.
It's a simple solution that implements Java's Comparator interface so that the sorting is as easy as
Collections.sort(versionList,new SemanticVersionComparator());Java
4
u/OllieShadbolt 1 0 Oct 06 '14 edited Oct 06 '14
Python 3.2.5
I feel like I cheated by simply pushing all of the 3 different numerical sections into one large number, then basically auto-sorting that and running through them to get the original outputs. Regardless, still had a lot of fun working on this and producing my code! I've tried to explain each section to the best of my ability and make each part clear, as the code lines aren't exactly the easiest to understand instantly. Anyways, any advice is always appreciated as I'm still an early learner c:
With Comments;
Without Comments;
Edit: Incredibly stupid 2-line compaction because why not
4
u/SilentBunny Oct 06 '14
Quick Ruby solution:
1
u/frozensunshine 1 0 Oct 06 '14
I don't quite understand how to differentiate between a label and metadata. Is it that label will always be of the form
-alphaor-betaor-rcX? And that metadata will always be of the form+blah?1
u/marchelzo Oct 06 '14
Haskell. I was lazy so I decided to use regex to split the versions :)
3
u/Godspiral 3 3 Oct 06 '14 edited Oct 06 '14
in J,
3
u/skeeto -9 8 Oct 06 '14 edited Oct 06 '14
C99 with no input validation. If major, minor, and patch numbers are guaranteed to be relatively small, a radix sort would have worked really well here instead of
qsort().3
u/99AFCC Oct 07 '14
Python sort key.
Regular expression.
2.7: old NoneType comparison
3.4: new style unpacking
Output
2
u/HippieSpider Oct 06 '14
[Python]
Waaay too much work on formatting and stuff, but at least it gives a beautiful output:
I also didn't work too hard on the sorting algorithm (compareSemanticVersions), which I guess was technically the main point of this challenge but I was more focused on making it pretty haha.
2
u/Joris1225 Oct 06 '14
C#
2
u/IceDane 0 0 Oct 06 '14
Here's my version in Haskell, using Parsec to parse the version strings into records, and then manually defining the instance for Ord for SemanticVersion so that it can be sorted using the built-in sort function.
2
u/jeaton Oct 07 '14
C:
2
u/ddsnowboard Oct 08 '14
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
- Jamie Zawinski
I may have done this wrong, but I learned regular expressions just a couple of weeks ago, and now I'm all excited to use them all the time. So that's what I did. It did, as the quote suggests, give me some problems, but it eventually worked out OK. Anyway, here's Java. I'm not a Java guru by any means, but I need to get better at it, so any comments are appreciated.
2
u/Jberczel Oct 08 '14
Ruby solution. I was having trouble overloading comparison operator for the SemanticVersion data structure. Not sure if there's a better way to do it than using #sort_by method on array. Any feedback how to clean that up would be appreciated.
Output:
2
u/Steve132 0 1 Oct 10 '14
Python, one crazy line.
1
u/FatShack Oct 06 '14 edited Oct 06 '14
Python 2. Edited for modularity.
quicksort.py:
version.py:
semanticsort.py:
Output:
1
u/[deleted] Oct 06 '14
Enjoyed this puzzle. :)
C#, making use of IComparable(T) and a call to OrderBy(). Includes some fairly inefficient parsing for the version string, but I can't convince myself that I care. :P
1
u/rectal_smasher_2000 1 1 Oct 06 '14 edited Oct 06 '14
c++11 (msvc2012)
uses
<regex>to parse user input, packs said input into asemver_tobject, which is then passed to astd::setusing a custom comparatorsemver_cmp. the set then does the sorting. the regex could be somewhat better, but unfortunately standard c++11 regular expressions do not support lookbehind assertion and i couldn't be bothered setting up boost.1
u/cooper6581 Oct 06 '14
Erlang
Here is the output:
1
u/leonardo_m Oct 06 '14
D language:
Output:
1
u/spfy Oct 07 '14
Here's my Java solution. I did a bubble sort because it's easy. I feel like my regex matches/splits are kind of ugly, but they work!
1
u/[deleted] Oct 07 '14 edited Jan 02 '16
1
u/skitch920 Oct 07 '14 edited Oct 07 '14
JavaScript When the label is empty, I just use some crazy high unicode character to handle sorting correctly.
Usage:
Output:
1
u/Miills Oct 07 '14
Node js, expects an input.txt file in the same directory https://github.com/grantmiiller/daily_programmer/blob/master/challenge183/solution.js
1
u/mesotiran Oct 07 '14
Perl
1
u/KevinTheGray Oct 07 '14
Java
Not too elegant, but hopefully doing more of these will get me better at that!
1
u/Edward_H Oct 07 '14
A delightfully simple (for COBOL) program:
1
u/fvandepitte 0 0 Oct 07 '14
C#
1
u/jnazario 2 0 Oct 07 '14 edited Oct 07 '14
F# solution edited a bit
the edit i made now puts a final version (e.g. 1.2.3) after an RC or anything else (e.g. 1.2.3-rc1) as i would expect it to.
1
u/antesignanus Oct 07 '14
Python 2
Takes input from a file provided by command line. Ex:
python 183.py test.txt
1
u/[deleted] Oct 07 '14
Here goes: a C++ solution.
1
u/Teslatronic Oct 07 '14
Python
Using magic methods for custom sorting.
(I kinda cheated by removing the first line of the input manually, since it wasn't really necessary for Python and I couldn't be bothered to make the list comprehension skip the first line.)
1
u/AtlasMeh-ed Oct 08 '14 edited Oct 08 '14
I used a java to implement part of the challenge and a shell script for the rest. First time posting here so let me know what you think.
The shell script
and the Java part
1
u/frozensunshine 1 0 Oct 08 '14
Late to the party, but here's my solution in C. I'd love some feedback! Thank you!
1
u/spotyx Oct 08 '14
Python
1
u/Hero_764 Oct 09 '14
Used Python for the first time in over a year.
1
u/[deleted] Oct 10 '14 edited Oct 10 '14
New to python and its pretty messy. Any suggestions are appreciated. Is it bad form to include my personal notes? or should I streamline the comments more?
1
u/sevvy325 Oct 11 '14
So I think I may have overdone it a bit haha. I did a solution in C# complete with Windows form ala Visual Studio. I added the ability to parse and save to files as well as a textbox in the program to sort inside if you like
Here's the .exe: https://dl.dropboxusercontent.com/u/78916595/SemanticVersioningForm.exe
The Github: https://github.com/sevvy325/SemanticVersioningForm
the meat of the solution is in the structure I created:
and the parsing seen here:
I doubt it's the most stable thing in the world, but if you five it correct input(Each Version on a new line), you get correct output ;)
1
u/_flks Oct 15 '14
LiveScript
1
u/Qyaffer Oct 19 '14
C++
Output :
I would greatly appreciate some tips, in my eyes I'm fairly new to programming so feedback is something I need in order to improve.
1
u/fakir72 Oct 19 '14
Go
Here's my entry in Go. I'm sure there's a more optimal solution, but this was a fun exercise in learning Go.
1
u/[deleted] Oct 26 '14
In Haskell: http://www.xojoc.pw/dailyprogrammer/semver.html
1
u/Zarimax Oct 29 '14 edited Oct 29 '14
C++11
I went for the regex this time.
1
u/wizao 1 0 Nov 14 '14
Forgot to post my haskell solution:
-2
u/AlmondFlashv2 Oct 09 '14
Hi can someone give me the solution in binary please?x
0
u/[deleted] Oct 06 '14
[deleted] — view deleted comment