Awal minggu ini, kami belajar tentang cara memformat bahasa esoterik untuk berkomentar. Hari ini, kita akan melakukan kebalikannya. Saya ingin Anda menulis sebuah program atau fungsi yang mem-parsing beberapa kode esoterik yang dikomentari dengan baik dan mem-parsing komentar, hanya mengembalikan kode. Dengan menggunakan beberapa contoh dari tantangan sebelumnya, di sini terlihat seperti apa kode yang dikomentari dengan baik:
a #Explanation of what 'a' does
bc #Bc
d #d
e #Explanation of e
fgh #foobar
ij #hello world
k #etc.
l #so on
mn #and
op #so forth
Inilah yang perlu Anda lakukan untuk mengekstrak kode. Pertama, hapus karakter komentar ( #
), spasi sebelum, dan semuanya setelah karakter komentar.
a
bc
d
e
fgh
ij
k
l
mn
op
Kemudian, pisahkan setiap baris ke atas menjadi satu baris. Misalnya, karena b
berada di kolom kedua di baris dua, setelah kami menutupnya, itu akan berada di kolom kedua di baris pertama . Demikian pula, c
akan diletakkan di kolom ketiga baris satu, dan d
akan diletakkan di keempat. Ulangi ini untuk setiap karakter, dan Anda mendapatkan ini:
abcdefghijklmnop
Catatan penting: Sepertinya solusi sepele adalah dengan hanya menghapus komentar, menghapus setiap spasi, dan bergabung dengan setiap baris. Ini bukan pendekatan yang valid! Karena kode asli mungkin memiliki spasi di dalamnya, ini akan dihapus dengan pendekatan ini. Misalnya, ini adalah input yang benar-benar valid:
hello #Line one
#Line two
world! #Line three
Dan output yang sesuai harus:
hello world!
Tantangan:
Tulis program atau fungsi yang mengambil kode komentar sebagai input, dan mengeluarkan atau mengembalikan kode dengan semua komentar diuraikan darinya. Anda harus mengeluarkan kode tanpa spasi tambahan, meskipun satu baris tambahan diizinkan. Karakter komentar akan selalu ada #
, dan akan selalu ada satu ruang ekstra sebelum komentar dimulai. tidak#
akan muncul di bagian komentar dari input. Agar tantangannya lebih sederhana, berikut adalah beberapa input yang tidak harus Anda tangani:
Anda dapat mengasumsikan bahwa kode tidak akan memiliki dua karakter di kolom yang sama. Misalnya, ini adalah input yang melanggar aturan ini:
a #A character in column one bc #Characters in columns one and two
Anda juga dapat mengasumsikan bahwa semua karakter komentar muncul di kolom yang sama. Misalnya, input ini:
short #this is a short line long #This is a long line
melanggar aturan ini. Ini juga berarti bahwa
#
tidak akan ada di bagian kode.Dan terakhir, Anda tidak harus menangani bagian kode dengan spasi awal atau akhir. Sebagai contoh,
Hello, # World! #
Anda juga dapat mengasumsikan bahwa input hanya berisi karakter ASCII yang dapat dicetak.
Contoh:
Input:
hello #Line one
#Line two
world! #Line three
Output:
hello world!
Input:
E #This comment intentionally left blank
ac #
h s #
ecti #
on is #
one c #
haracte #
r longer #
than the #
last! #
Output:
Each section is one character longer than the last!
Input:
4 #This number is 7
8 #
15 #That last comment is wrong.
16 #
23 #
42 #
Output:
4815162342
Input:
Hello #Comment 1
world #Comment 2
, #Comment 3
how #Comment 4
are #Comment 5
you? #Comment 6
Output:
Hello world, how are you?
Input:
Prepare #
for... #
extra spaces! #
Output:
Prepare for... extra spaces!
Anda dapat mengambil input dalam format beralasan apa pun yang Anda suka, misalnya, daftar string, string tunggal dengan baris baru, daftar karakter 2d, dll. Jawaban terpendek dalam byte menang!
hello world!
Anda tunjukkan)? Selain itu, Anda menyatakan: "#
tidak akan muncul di bagian komentar dari input. ", Tetapi bisakah itu terjadi di cuplikan-kode itu sendiri?do {stuff} while (condition);
dengan penjelasan dalam urutando while (condition); #Explainything
kemudian{stuff} #Explainything
.Jawaban:
Jelly ,
87 byteCobalah online!
Bagaimana itu bekerja
sumber
Python 2,
4843 bytesThanks to @xnor for golfing off 5 bytes!
Test it on Ideone.
sumber
map(max,*x)
becausemax
takes any number of arguments andNone
is small.map
can be used like that... Thanks!`...`[2::5]
trick work?`...`
is equivalent torepr(...)
, so for the list of singleton strings['a', 'b', 'c']
, you get the string"['a', 'b', 'c']"
. Finally,[2::5]
chops off the first two characters ("['"
) and takes every fifth character of the remaining string.JavaScript (ES6),
977560 bytesThanks to @Neil for helping golf off 22 bytes
Input is an array of lines.
a
is array inputp
is previous itemc
is current itemm
is match stringo
is offsetsumber
m
regexp flag is unnecessary (did you have a$
at one point?) as is the space in(p, c)
. Finally, I thinkreplace
will work out shorter than[...p].map().join
.length
and userscript, maybe you didn't count the newline, but only because I accidentally included the semicolon;
which isn't required (JavaScript has ASI).replace
would help so much, that's really neat!Perl,
353432 bytesIncludes +1 for
-p
Give input on STDIN
eso.pl
Notice that there is a space after the final
;
. The code works as shown, but replace\0
by the literal character to get the claimed score.sumber
$a|=...
is rather well done, it took me a while to figure out what you were doing! One question though :*_=a
seems to be roughly equivalent to$_=$a
, why is that?*_=a
is a very obscure glob assignment which aliases the_
globals and thea
globals. So it's not so much a copy from$a
to$_
but from that point on (global)$a
and$_
are actually the same variable. All to save 1 byte...Python 2, 187 bytes
I'm gonna golf this more tomorrow I have school ;)
sumber
1 for
can be reduced to1for
. Also, if the sum of the list (at line 5) can't be negative, you can just check for<1
instead of==0
. Happy school day! :D +1.Ruby, 63 bytes
Basically a port of Dennis' Jelly answer. Takes input as an array of strings.
See it on eval.in: https://eval.in/640757
sumber
CJam, 12 bytes
Thanks to Sp3000 for saving 2 bytes.
An unnamed block that takes a list of strings (one for each line) and replaces it with a single string.
Try it online!
Explanation
sumber
J, 30 bytes
Takes a list of strings as input. Basically uses the same approach as Dennis in his Jelly answer.
Commented and explained
Intermediate steps:
Test case
sumber
Javascript (ES6), 63 bytes
Takes input as an array of strings.
sumber
Retina, 32 bytes
Byte count assumes ISO 8859-1 encoding.
Try it online!
sumber
Pyke,
1510 bytesTry it here!
Port of the Jelly answer
sumber
C#
157122 BytesGolfed 35 bytes thanks to @milk -- though I swear I tried that earlier.
Takes input as a 2-d array of characters.
157 bytes:
sumber
Trim()
work instead ofTrimEnd()
? Even better, I think you can save a lot of bytes by using s[0] as the output var and usingreturn new string(s[0],0,i)
wherei
is the index of the last code character. That idea may require twofor
loops instead of theforeach
, I'll think about it more and try to write actual code later today.Trim()
will trim from the start as well, which I believe wouldn't be valid. I also was originally doing the loading into s[0] and I had inti;
outside of the loop (to reuse it in the return) which I believe ultimately added bytesPyth, 11 bytes
A program that takes input of a list of strings on STDIN and prints a string.
Try it online
How it works
sumber
sed, 126 bytes
Requires a newline at the end of the input.
I'm sure I can golf this a little more, but I'm just happy it works for now.
sumber
Perl 6, 39 bytes
Translation of the Python solution by Dennis.
Takes input as a list of strings, and returns a string.
(try it online)
sumber
Jelly, 27 bytes
Test it at TryItOnline
Uses the strictest spec - the extra space before the comment character is removed at the cost of a byte.
Input is a list of strings.
sumber
Ruby, 77 bytes
sumber
TSQL,
216175 bytesGolfed:
Ungolfed:
Fiddle
sumber
Javascript,
5634 bytes, non-competingAs @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ pointed out, I am not prepared for extra spaces
sumber
Dyalog APL, 22 bytes
Inspiration.
(
⎕UCS
character representation of¯2↓
all but the last two of⍳∘35↑
up until the position of the first 35 ("#"), in that which is outside the parenthesis, taken from⊢
that which is outside the parenthesis)
namely...⌈⌿
the columnar maximums∘
of⎕UCS
the Unicode valuesTryAPL online!
sumber