Tulis sebuah program yang memasukkan string yang terdiri dari karakter yang dapat dicetak (ASCII 20-7E) dan integer n
dalam [2,16] dan lakukan modifikasi pada string tersebut.
- Setiap karakter dalam string dikonversi ke kode ASCII-nya (contoh yang diberikan dalam heksadesimal, meskipun basis 10 juga dapat diterima).
- Kode ASCII dikonversi menjadi markas
n
dan digabung menjadi satu. - String baru dibagi setiap karakter lainnya. Jika ada jumlah karakter ganjil, maka karakter terakhir dihapus seluruhnya.
- Mencetak kode ASCII (pada basis 16) dikonversi kembali ke dalam karakter mereka, sedangkan kode ASCII yang tidak dicetak dihapus.
- String yang dihasilkan dicetak.
Kasus cobaan
Memasukkan
Hello, World!
6
Tangga
Hello, World!
48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21
2002453003003031125222330331030024453
20 02 45 30 03 00 30 31 12 52 22 33 03 31 03 00 24 45
Output dari program ini adalah E001R"31$E
.
Ini kode golf, jadi aturan standar berlaku. Kode terpendek dalam byte menang.
7
, stringJ
akan melalui langkahJ
->50
->101
->10
->(no output)
, seperti halnya stringK
atauL
.H
is ASCII 72 (decimal) or 48 (hex), but what I need is 200 (base 6). All the row 2 in the example is useless and confusing in my opinionJawaban:
Pyth - 22 bytes
Hope to golf a lot more, pretty straightforward.
Try it online here.
sumber
q3l`T
is wonderful.fgTd
be enough?CJam, 33 bytes
Takes input in the form
6 "Hello, World!"
. Test it here.See Dennis's answer for a similar but better solution with a nice explanation.
sumber
Bash + common linux utils, 118
sumber
printf %s "$1"
intoecho -n "$1"
to save 2 bytes-e
. Tryecho -n "-e"
CJam, 24 bytes
Note that there is a DEL character (0x7F) between
'
and,
. Try it online in the CJam interpreter.How it works
sumber
DEL
character from the output.JavaScript (ES6), 137
147Using the most verbose functions available in JavaScript
sumber
x=>x>=
[for(z of ...)if(...)...]
instead ofmap(...).filter(...)
x=>x>=
has goneJulia, 118 bytes
Ungolfed:
sumber
Mathematica, 134 bytes
If a function is allowed:
Mathematica, 112 bytes
sumber
TeaScript, 23 bytes
TeaScript is JavaScript for golfing
Relatively straight-forward but delightfully short. I can probably golf down a few more characters with some more operators. A few other new features might also be able to be used to cut down some bytes.
Ungolfed && Explanation
sumber
Ruby 92
Online test here.
sumber
Python 2, 174 bytes
Try it here
Not really the best tool for the job. Since Python has no convert-to-arbitrary-base function, I had to implement my own. That was fun, at least--particularly finding a [marginally] shorter expression for the digits than
"0123456789ABCDEF"[n%b]
. For iterating over two characters at a time, I found awhile
loop was slightly shorter than a functional approach.181 bytes as a full program:
sumber
MATLAB, 103 bytes
I've written a function k that takes a string s and an integer n as input. e.g.:
gives
Most annoying thing I had to work around is leading zeros showing up when converting to base n. Getting these out of the array that was to be split after every 2nd character cost quite a lot of bytes. Not sure if it is possible to save any more bytes using this approach.
sumber
PHP - 286 bytes
Put the string in
$s
and the integer in$b
.Pass the value to
GET["s"]
.sumber