33
2
As code-golfers, we're not used to releasing (surely). We're gonna need a few tools to help us do that.
Of course, to help marketing a new release, we need a nice and shiny Release Version. Who doesn't get excited when they hear about version 3.0.0?
Task
Your task will be to write a program/routine/... to increment a version number.
You need to increment the version number and reset the "less important" ones (i.e. patch version).
You get two arguments: the current version (ex "1.0.3") as a string, and an index to know which one to update (0 or 1-indexed).
Example, 0-indexed:
next-version("1.0.3", 0) # 2.0.0
next-version("1.2.3.4.5", 2) # 1.2.4.0.0
next-version("10.0", 0) # 11.0
next-version("3", 0) # 4
next-version("1", 7) # ERROR
next-version("01", 0) # ERROR
The version is a string, each part is a number, separated with a dot. There can be no leading, no trailing or no consecutive dots (and nothing outside of numbers/dots). There is no limit to the size of the version string.
^[1-9]\d*(\.[1-9]\d*)*$
The error case (last two examples) is undefined behavior. What happens in case of wrong inputs is of no relevance to this challenge.
As usual, standard loopholes are forbidden. You're allowed to print or return the string.
1Can we ask to receive as input first the index and then the version number? – Leo – 2016-12-08T18:10:21.667
@Leo yes, the order is not an issue. – Ven – 2016-12-08T18:17:27.980
I might add a test case for incrementing the final number in the string, or an example or something to test for. – nmjcman101 – 2016-12-08T18:44:48.237
@nmjcman101 how is it a special case? – Ven – 2016-12-08T18:52:03.017
@ven It's not a special case, there just aren't any cases incrementing the last one, and I thought it could help someone else. – nmjcman101 – 2016-12-08T18:54:52.013
3I wish I could make the same challenge with the win condition as "most readable" so someone would write these for me to use in real work. =) – jpmc26 – 2016-12-08T21:25:20.513
@jpmc26 I have xyz.pm6 on github, the module I extracted this idea from! I actually use it. – Ven – 2016-12-08T21:35:44.133
You are reminding me of an argument over whether the semantic versioning standard required decimal numbers, or allowed other bases. Thanks for providing a regex to make your spec unambiguous. – Sparr – 2016-12-09T04:04:15.033
Well, mostly unambiguous. Your regex allows 1.0.009 and most of the solutions would not "correctly" increment that to 1.0.010 – Sparr – 2016-12-09T04:04:56.490
so close! you still allow 000.1.2.3 :) – Sparr – 2016-12-09T21:04:09.420
Awww. Okay, fixed again. :P – Ven – 2016-12-09T23:46:30.883