Saat menulis esai saya untuk Shakespeare, saya menyadari bahwa saya perlu mempersingkat referensi kutipan saya menjadi panjang yang lebih mudah dikelola. Saya sebelumnya telah menulis ini:
(Act 1, Scene 2, Lines 345-346)
Tetapi saya sekarang diminta untuk menulisnya seperti ini:
(I.ii.345-6)
Jelas, saya perlu kode golf untuk mengurangi referensi kutipan Shakespeare saya.
Tugas
Tulis sebuah program atau fungsi yang, masing-masing diberi input string setelah Template 1 atau 2, mencetak atau mengembalikan string setelah Template 3 atau 4. Anda hanya perlu mendukung Kisah 1 hingga 5 dan Adegan 1 hingga 9.
Templat
Templat 1
(Act x, Scene y, Lines a-b)
Anda dapat mengasumsikan bahwa x
tidak pernah melebihi 5, y
tidak pernah melebihi 9, a
dan b
selalu bilangan bulat positif tidak melebihi nilai bilangan bulat standar positif maksimum bahasa Anda, dan a
selalu eksklusif kurang darib
.
Templat 2
(Act x, Scene y, Line a)
Kondisi yang sama dengan Templat 1, tidak termasuk informasi tentang b
.
Templat 3
(x.y.a-b)
Di mana x
angka romawi kapital, angka romawi y
huruf kecil, a
dan b
jumlahnya, dan b
disingkat menjadi hanya digit kurang dari digit berbeda pertama yang sama pentingnya dari a
.
Templat 4
(x.y.a)
Kondisi yang sama seperti Templat 3, tidak termasuk informasi tentang b
.
Uji Kasus
Membiarkan f(s)
menjadi fungsi yang didefinisikan dalam Tugas. ""
menunjukkan nilai string.
>>> f("(Act 1, Scene 2, Lines 345-346)")
"(I.ii.345-6)"
>>> f("(Act 3, Scene 4, Lines 34-349)")
"(III.iv.34-349)"
>>> f("(Act 5, Scene 9, Lines 123-234)")
"(V.ix.123-234)"
>>> f("(Act 3, Scene 4, Line 72)")
"(III.iv.72)"
>>> f("(Act 2, Scene 3, Lines 123-133)")
"(II.iii.123-33)"
>>> f("(Act 4, Scene 8, Lines 124-133)")
"(IV.viii.124-33)"
Untuk keperluan tantangan ini, terjemahan angka arab ke roma berikut harus didukung:
1 i I
2 ii II
3 iii III
4 iv IV
5 v V
6 vi (you do not have to support past 5)
7 vii
8 viii
9 ix
(Act 1, Scene 2, Lines 345-3499)
Jawaban:
The Shakespeare Programming Language (non-competing)
I really liked this question, and as there was some interest in a Shakespeare-language answer, here is one.
(It's over 6000 bytes long.) There are some tricks in there but I have not tried to golf it much, because: (1) I already contributed my share of golfing on another answer, and (2) changing all characters to "Page" and "Puck", or all phrases to "big big big big big cat", seems to spoil the fun. Instead for the part that deals with Roman numerals I have used characters who are Romans, etc. I did reuse characters and instructions to save some typing. :-)
Program ini sebagian besar harus langsung tetapi satu kerutan yang layak disebutkan adalah bahwa ketika saya menulis ini saya berasumsi bahwa membaca bilangan bulat akan bekerja seperti
scanf
: (1) mengkonsumsi hanya karakter sebanyak dari input sesuai dengan integer, dan (2) dalam kasus kegagalan, biarkan variabel tidak berubah. (Saya menggunakan properti kedua ini untuk membedakan antara Templat 1 dan 2 dalam Babak II, dengan membaca hingga "Baris" dan berusaha membaca bilangan bulat.) Sayangnya ternyata ada (apa yang saya anggap) bug dalam implementasi asli dari bahasa di mana membaca integer menghabiskan semuanya hingga akhir baris dan melempar kesalahan jika gagal, sehingga perlua patch tolibspl.c
to makeint_input
behave more likescanf
.And with that, it works:
Kode pseudocode sedikit lebih tinggi yang saya kerjakan, untuk membantu siapa pun yang mencoba memahami:
Relating the above to the final code is left as an exercise. :-) Note that ShakespearePL has arithmetic and stacks and gotos but no pointers (only labels), so implementing "subroutines" like the conversion to Roman is a bit… interesting.
sumber
LaTeX,
513364259226215178159 BytesGood essays should always be written in LaTeX.
This uses the xstring package as there isn't exactly a lot of string handling built in. On the plus side the upper limit for the built-in
\Roman
formatting is larger than we'll ever need (even for the sonnets) at2^31-1
. I've included\documentclass{ecv}
in the count, but none of the test code:(If you were crazy enough to actually use this, you'd have to ungolf the macro names at least. Overwriting single character macros is bad practice)
Ungolfed and commented:
Note that in this version the comments are required otherwise the newline is interpreted as whitespace and expands to a space.
sumber
~
as your macro name instead of\s
. But actually you don't need\s
(\stripcomma
in the ungolfed version) at all: you can just\def\t#1 #2, #3 #4, #5 #6
and TeX will take care of stripping the commas. (So you could use the~
trick on\t
instead, saving 1 byte.)~
is a bit nasty but I like it here. It meant I had to change the documentclass (to one of the other 3-letter.cls
files I had installed)\@roman
and\@Roman
don't need braces around the argument.xstring
ideas were yours :-) It was fun golfing this together!JavaScript (ES6),
210183178177171 bytesSaved 27 bytes by unrolling rest parameters (thanks to ETHproductions)
Saved 5 bytes by not matching the opening paren and by tweaking the roman numeral generation
Saved 1 byte by adjusting the final ternary expression
Saved 6 bytes by combining two matching groups
Test Cases:
sumber
Act
and each\D*
with.*
?Jelly,
87 8685 bytesTry it online! or see a test suite
How?
sumber
œr
,Ṗ,Ç
,Ṗ€V
,ṪÇ$
,W
as the last link on a helper link, possibly others too, nice effort! This isn't your usual 80-something Jelly submission, this deserves special recognition among Jelly people.R,
94126112166 bytesAnd now it's wordier than before :(, back to trying to golf it further. Regex for reducing the page reference shamelessly
stolenborrowed from @FryAmTheEggman.Now I really need to do some work to get back the bytes, but it works for the second case now.
Try it online! - Note that
el
doesn't work on TIO and has been replaced withunlist
sumber
Retina,
8988 bytesTry it online!
Saved 3 bytes thanks to Neil.
Strips away the unnecessary characters before replacing the first two numbers with blocks of
i
characters. Then it matches chunks of thesei
s to form the appropriate roman numerals. Then we capitalise the first roman numeral. Finally we match as many numbers as we can before the hyphen and after the hyphen such that the number of digits in the number is the same. We then remove that prefix from the second number.sumber
iiiii
withv
,iiii
withiv
andviv
withix
seems to save a couple of bytes.345-356
- I was expecting345-56
.\b
at the end of the last replacement to avoid having to repeat the)
in the substitution?\d
but it does seem to work since there isn't another word boundary. Thanks!PHP>=7.1, 195 Bytes
Testcases
Expanded
sumber
preg_match_all("#\d+#",$argn,$m);[$a,$s,$b,$e]=$m[0];
saves two bytes.if($e){for(;$b[$i]==$e[$i];$i++);echo"-",substr($e,$i);}echo")";
should save 46.(you do not have to support past 5)
saves 15 bytes.".$r[$s].$b"
saves another 5 bytes; and[[$a,$s,$b,$e]]=$m;
another one. Unfortunately, array assignments don´t work by reference (yet).if($e&&$e-$b){for($x=str_pad($b,strlen($e),0,0);$x[$i]==$e[$i];$i++);echo"-",substr($e,$i);}
saves 10 bytes and might work.&&$e-$b
is unnecessary for the test cases; so it saves 17 bytes, not 10. Btw. you still don´t need roman 6 to 9. ;)for(;str_pad($b,strlen($e),0,0)[$i]==$e[$i];)$i++;
withfor(;$e&&~$c=$e[-++$k];)$c>$b[-$k]&&$i=-$k;
.Perl 5, 185 + 1 = 186 bytes
1 byte penalty for the required
-n
flag.May fail for some test cases where the scene has more than 10^11 lines, but AFAIK no Shakespeare scenes are quite that long ;)
In readable form:
sumber
Ruby, 204+1 = 205 bytes
Uses the
-p
flag.Try it online!
sumber
Python 2.7 298 bytes
sumber
Perl, 99 bytes
Run with
perl -pe
. 98 bytes (source) + 1 byte (p
flag) = 99.sumber
Python 2,
301259252221 bytesA whopping -31 bytes thanks to Chas Brown.
So, uh, this is... extremely long... I think I can golf this but I've been wracking my brain for a while.
Try it online!
Breakdown
sumber
b,c,d,e,f=s.groups()
instead ofa,b,c,d,e,f=[s.group(n) for n in range(6)]
[0]+'i,ii,iii,iv,v,vi,vii,viii,ix'.split(',')
instead of[s,'i','ii','iii','iv','v','vi','vii','viii','ix']
.[0]+'i ii iii iv v vi vii viii ix'.split()
instead of[s,'i','ii','iii','iv','v','vi','vii','viii','ix']
.0
inside the quotes. One last minor tweak I can see: you are using:s,r=XXX,YYY;b,c,d,e,f=s.groups();
you can save another 4 bytes by instead equivalently saying:b,c,d,e,f=XXX.groups();r=YYY;
. So you end up with 81 bytes less than my submission! :)q/kdb+,
200187 bytesSolution:
Examples:
Explanation: (slightly ungolfed)
Notes:
Technically it can be 2 bytes shorter (no need for the
f:
) but makes it easier to show examples this way.Edits:
string
with$:
,count
with#:
,til
with(!)
andfirst
with(*:)
, cast the indices of R to strings so we dont have to cast act/scene into intssumber