Sebagai pegolf kode, kami tidak terbiasa melepaskan ( pasti ). Kami akan membutuhkan beberapa alat untuk membantu kami melakukan itu.
Tentu saja, untuk membantu memasarkan rilis baru, kami membutuhkan Versi Rilis yang bagus dan berkilau. Siapa yang tidak senang ketika mereka mendengar tentang versi 3.0.0?
Tugas
Tugas Anda adalah menulis program / rutin / ... untuk menambah nomor versi.
Anda perlu menambah nomor versi dan mengatur ulang yang "kurang penting" (yaitu versi tambalan).
Anda mendapatkan dua argumen: versi saat ini (ex "1.0.3") sebagai string, dan indeks untuk mengetahui mana yang akan diperbarui (0 atau 1-diindeks).
Contoh, diindeks 0:
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
Versi ini adalah string, setiap bagian adalah angka, dipisahkan dengan titik. Tidak ada titik awal, tidak ada jejak atau tidak ada titik berurutan (dan tidak ada di luar angka / titik). Tidak ada batasan ukuran string versi.
^[1-9]\d*(\.[1-9]\d*)*$
Kasus kesalahan (dua contoh terakhir) adalah perilaku yang tidak terdefinisi. Apa yang terjadi jika input yang salah tidak ada hubungannya dengan tantangan ini.
Seperti biasa, lubang standar dilarang. Anda diizinkan mencetak atau mengembalikan string.
Jawaban:
Japt,
1611 byteUji secara online! Nomor input diindeks 1.
Berdasarkan jawaban JavaScript saya. Ini mengambil keuntungan dari salah satu fitur Japt yang paling membantu: memisahkan satu string pada yang lain sebelum memetakan setiap item, kemudian bergabung pada string itu lagi setelah memetakan.
Tanpa penjelasan dan penjelasan
sumber
Vim
2025 byteSayangnya saya menyadari bahwa itu tidak menangani kasus memperbarui nomor terakhir, jadi saya harus menambahkan byte. Ini 1-diindeks.
TryItOnline
Tidak dapat dicetak:
Ini mengambil argumen dalam urutan terbalik, sebagai baris terpisah:
Penjelasan:
sumber
DJ@"t.<C-a>qq2wcw0<esc>@qq@q
yang kembali ke dua puluhDJ@"f.@"<esc><C-a>qq2wcw0<esc>@qq@q
?JavaScript (ES6),
44424037 byteDisimpan 3 byte berkat @Neil
Nomor input diindeks 1.
Cuplikan tes
sumber
n=>i&&+n+!--i
V ,
13, 12 byteCobalah online!
Ini diindeks 0.
Ada
ctrl-a
(ASCII 0x01) di sana, jadi di sini adalah versi yang dapat dibaca:Penjelasan:
sumber
Perl,
403734 + 1 = 35 byte-2 byte terima kasih kepada @Dada. -3 byte berkat ide yang saya dapatkan dari membaca kode Japt @ ETHproductions.
Jalankan dengan
-p
bendera.Cobalah online!
Rincian kode
sumber
$&
bukannya$1
itu)Jelly ,
1917 byte1-diindeks.
TryItOnline!
Bagaimana?
sumber
V€
:).MATLAB, 85 byte
Satu upaya berbasis golf pertama!
sumber
string
type in action :-)C#
116104 BytesExplanation
Try it here
sumber
string
andint
in the anonymous function signaturePython 2, 84 Bytes
I feel like this could really be shorter.. Might need a way to have a non-enumerate option.
If we were able to take the version as a list of strings, there's a 75-byte solution:
Furthermore, if both the input and output were lists of numbers, there's a 64-byte solution:
sumber
V
1420 bytesAgain, I had to add code for the corner case of incrementing the final digit. (1-indexed)
TryItOnline
Unprintables:
This takes the arguments in reverse order, as separate lines:
sumber
@a
(or even shorter,À
) which should save you a bunch of bytes.Batch, 119 bytes
1-indexed.
sumber
Perl 6, 67 bytes, 0-indexed
Explanation:
sumber
PowerShell 3+,
7574 bytesUngolfed
Explanation
Parameters are accepted using the
$args
array..
, then for each element:$m
is set to be-not $b
. On first run,$b
will be undefined which will be coalesced to$false
, so$m
will start as$true
.$m
is intended to be a multiplier that's always0
or1
and it will be used later.$m
must be evaluated here because we want it to be based on the last iteration's$b
value.$b
is set to itself-or
the result of comparing an iterator$i
with$args[1]
(the index parameter). This means$b
will be set to$true
here once we're on the element that is to be incremented. Additionally, it will be$true
in every subsequent iteration because the conditional is-or
'd with its current value.$b
is converted to a number using unary+
($false
=>0
,$true
=>1
), then added to the current version element$_
which is a[string]
, but PowerShell always tries to coalesce the argument on the right to the type on the left, so arithmetic will be performed, not string concatenation. Then this value will be multiplied by$m
, which is still[bool]
but will be implicitly coalesced..
.So, the first iteration where
$b
becomes$true
,$b
would have been$false
when$m
was evaluated, making$m
equal$true
, which will keep the multiplier at1
.During that run
$b
becomes$true
and is added to the version element (as1
), thereby incrementing it, and since the multiplier is still1
, that will be the end result.So on the next iteration,
$b
will already be$true
, making$m
equal$false
, which will make the multiplier0
. Since$b
will forever be$true
now, the multiplier will always be0
, so every element returned will be0
too.sumber
R,
100959286 bytesUnusually for R, this uses 0-indexing. Anonymous function with two arguments (a string and an integer). Likely can be golfed down a tad.
sumber
05AB1E, 22 bytes
Try it online!
I don't know how to do if-else in 05AB1E, so this is longer than it should be.
sumber
1.0.0.0.3, 3
should produce1.0.0.1.0
not1.0.0.1.3
.Coffee-script:
7767 BytesWoot! Time for cake and coffee for the beta release.
Thanks to @ven and @Cyoce I shaved 10 Bytes!
sumber
.join '.'
or.split '.'
)+
instead ofparseInt
(use~~
if you need it to be cast to integer)Python 3,
8986 bytesvery naive way of getting things done
Edit: rewritten the conditional by referring to @kade
sumber
PHP, 81 bytes
awfully long. At least: the Elephpant still beats the Python.
loops through first argument split by dots:
"."[!$i]
is empty for the first and a dot for every other element;($i<=$n)
and($i==$n)
are implicitly cast to integer0
or1
for integer arithmetics.sumber
JavaScript (ES6),
5755 bytesExamples:
Not the best JS implementation but it's fairly simple and follows the logic you'd expect.
sumber
Pyth - 21 bytes
Test Suite
sumber
10.0
test case gives11.0.0
, that's one too many part!Powershell,
801009592 BytesSaved 5 bytes by using a const for the
-1..if
Saved 3 bytes by using
!$b
instead of$b-eq0
Explanation:
Test Cases:
sumber
r
instead ofrandom
random
, it's not an an alias! It's a result of PowerShell's command evaluation. As it looks to find a command in aliases, functions, cmdlets, native applications, etc., the very last thing it tries to do is prependGet-
to whatever it is. So you're actually callingGet-Random
, but technically not as an alias. You can see this working by runningservice
, orchilditem
, or for maximum irony,alias
.Objective-C 531 Bytes
compile:
usage:
sumber
0
instead ofNULL
and remove thereturn 0;
at the end of the main.NSString *s
can probably have the space removed.**argv
is 1 byte shorter than*argv[]
.@autoreleasepool{}
is probably unnecessary.Javascript ES6: 60 bytes
sumber
}
at the end. On golfing: one of the features of arrow functions is implicit return, so you can replace(n,r)=>{return r>i?n=0:n}
with(n,r)=>r>i?n=0:n
to save some bytes.R, 75 bytes
Indexing is 1-based. You can play with it online here.
sumber
APL (Dyalog), 31 bytes
Requires
⎕IO←0
(Index Origin 0), which is default on many systems. Full program body; prompts for text input (version) and then numeric input (index).Try it online!
⍞
prompt for text input'.'⎕VFI
Verify and Fix Input using period as field separator (fields' validities, fields' values)⌽
reverse (to put the values in front)⊃
pick the first (the values)⎕(
...)
apply the following tacit function to that, using evaluated input as left argument:To explain the non-tacit equivalents of each function application we will now use
⍺
to indicate the left argument (the index) and⍵
to indicate the right argument (the list of individual numbers of the originally inputted current version number).⊃
equivalent to(⍺⊃⍵)
use⍺
to pick an element from⍵
1+
add one to that↑,
equivalent to(⍺↑⍵),
prepend⍺
numbers taken from⍵
⊢∘≢↑
equivalent to(⍺⊢∘≢⍵)↑
equivalent to(≢⍵)↑
take as many numbers from that as there are elements in⍵
, padding with zeros if necessary⍕
format (stringify with one space between each number)' '⎕R'.'
PCRE Replace spaces with periodssumber
Java 8, 130 bytes
Explanation:
Try it here.
sumber
LiveScript,
5352 bytes-1 byte thanks to @ASCII-only!
Old Explanation:
Another self-answer... Not that anyone golfes in LiveScript anyway. :P
I was working on another version:
But
*
is too overloaded to be recognized in a splicing index, thus=0
will try to access0[0]
. So you need to write something like..[b to ..length- b]=[0]*(..length-1-b)
and it's longer in the end.sumber
f=(a,b)->(for e,i in a/\.<newline> if i<b then e else if i>b then 0 else+e+1)*\.
is way longer :(if i<b then e else if i>b then 0 else+e+1
in i.e.[+e+1;0;e;e][i>b+(2*i<b)]
or something along those lines, maybe even([+e;-1][i>b+(2*i<b)]||e-1)+1
(a,b)->(for e,i in a/\.<newline> [+e+1;0;e][(i>b)+2*(i<b)])*\.
, 54->(for e,i in it/\.<newline> [+e+1;0;e][(i>&1)+2*(i<&1)])*\.
for 52;
with space. also... looks like that's basically as far down as it'll go with this approachHaskell,
136129 bytesTry it online!
Original
sumber