32
4
One of the lesser known programming paradigms which seems rather fitting for code golfing is Overlapping Oriented Programming (OOP) *. When writing partly identical code, many bytes can be saved by simply overlapping the identical parts and remembering in some way where the two original code lines start. Your task is to write two overlapping programs or functions compress
and decompress
with the following specification:
*Do not use in production code, probably.
compress
compress
takes two strings in any convenient format and overlaps them as much as possible. That is a string s
with minimal length is returned such that both input strings are substrings of s
. Additionally, some output which identifies the start and end indices of both string is returned.
Examples: (The exact IO-format is up to you)
compress("abcd", "deab") -> "deabcd" ((2,5),(0,3))
compress("abcd", "bc") -> "abcd" ((0,3),(1,2))
compress("abc", "def") -> "abcdef" ((0,2),(3,5)) or "defabc" ((3,5),(0,2))
decompress
decompress
computes the inverse function of compress
, that is given a string and two start and end indices (in the format in which they are returned by your compress
), return the two original strings. You need only handle valid inputs.
The following equality should hold for all strings s1
, s2
:
(s1, s2) == decompress (compress (s1, s2))
Examples: (inverses of compress
examples)
decompress "deabcd" ((2,5),(0,3)) -> "abcd" "deab"
decompress "abcd" ((0,3),(1,2)) -> "abcd" "bc"
decompress "abcdef" ((0,2),(3,5)) -> "abc" "def"
or (whichever version your "compress" generates)
decompress "defabc" ((3,5),(0,2)) -> "abc" "def"
Scoring
Your score is the size of the string returned by calling compress("<code of compress>", "<code of decompress>")
. As this is code-golf a lower score is better.
Example:
Assume the code for your function compress
is c=abcd
and the code for decompress
is d=efghi
. Then compress("c=abcd", "d=efghi")
yields "c=abcd=efghi"
(and the indices, but those don't affect scoring) so the score is length "c=abcd=efghi" = 12
.
Additional Rules
- In the spirit of this challenge, your
compress
anddecompress
must overlap in at least one character. You may achieve this trivially by adding a comment, but note that doing so will increase your score and there might be shorter solutions using inherently overlapping code. compress
anddecompress
should be able to handle strings containing any printable ASCII characters as well as all characters you used to definecompress
anddecompress
.- The indices can be zero- or one-indexed.
- Your programs or functions don't have to actually be named
compress
anddecompress
.
Can you use different command line arguments to run you compress and decompress code? – MildlyMilquetoast – 2017-01-25T17:01:32.767
Sure. You have to give two programs and the site policy allows command line arguments as long as they are counted, so you can give different command line arguments for each of your programs. – Laikoni – 2017-01-25T17:24:44.913