Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
Asked
Viewed 307 times
-4
\$\begingroup\$

Choose a language and write a program in the language that:

  • Take a valid program of the language as input,
  • Output a string(bytearray, etc.). It needn't be a valid program.
  • Only finite valid inputs result in longer/same-length(in bytes) output.
  • Different valid input, even if behave same, lead to different output.

Let length of your code as \$L\$ and number of inputs with longer/equal-length output as \$n\$. To win, you should be lowest on at least one of:

  • \$L\$,
  • \$N\$,
  • \$L+N\$,
  • \$L+\log_2(N+1)\$.

Notes

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Please make the objective and rules of the challenge clearer. \$\endgroup\$
    Caelus
    –  Caelus
    2022-01-19 16:56:52 +00:00
    Commented Jan 19, 2022 at 16:56
  • 1
    \$\begingroup\$ @Binary198 Please make your request clearer :) \$\endgroup\$
    l4m2
    –  l4m2
    2022-01-19 17:01:31 +00:00
    Commented Jan 19, 2022 at 17:01
  • \$\begingroup\$ Well, what is the program intended to do exactly, and how does the scoring system work? \$\endgroup\$
    Caelus
    –  Caelus
    2022-01-19 17:04:22 +00:00
    Commented Jan 19, 2022 at 17:04
  • \$\begingroup\$ @Binary198 To compress all programs but finite ones. Some users would make four questions for four scoring way but it likely make a mess \$\endgroup\$
    l4m2
    –  l4m2
    2022-01-19 17:07:02 +00:00
    Commented Jan 19, 2022 at 17:07
  • \$\begingroup\$ So, you need to take a program as input and output a unique string that will be longer than or equal to in length than the input? Still, the scoring system is weird. What is \$N\$, and why are there multiple scoring ways? I would usually just go with one. \$\endgroup\$
    Caelus
    –  Caelus
    2022-01-19 17:10:59 +00:00
    Commented Jan 19, 2022 at 17:10

1 Answer 1

3
\$\begingroup\$

Python 3.8 (pre-release), \$L=79\$, \$N\approx255^{1500}\$

-3 bytes thanks to @Jonathan Allan

f=lambda b,s=0,l=0:b and f(b[1:],255*s+b[0],l+1)or s.to_bytes(l-(l>1500),"big")

Try it online!

Python source code can't contain null bytes. This converts the input (bytestring) to base 255 and then back to bytes. After 1500 removes a redundant byte.

\$\endgroup\$
7
  • \$\begingroup\$ Do you save one byte iff length>1500? \$\endgroup\$
    l4m2
    –  l4m2
    2022-01-19 17:21:39 +00:00
    Commented Jan 19, 2022 at 17:21
  • \$\begingroup\$ @l4m2 Yes, exactly \$\endgroup\$
    AnttiP
    –  AnttiP
    2022-01-19 17:22:52 +00:00
    Commented Jan 19, 2022 at 17:22
  • \$\begingroup\$ You don't need the f= (I guess you were going to go with a recursive function which would need it, and left it in). \$\endgroup\$
    Jonathan Allan
    –  Jonathan Allan
    2022-01-19 19:02:55 +00:00
    Commented Jan 19, 2022 at 19:02
  • 1
    \$\begingroup\$ Using recursion (which I think we may assume to have no limit set for code-golf purposes), you could do f=lambda b,s=0,l=0:b and f(b[1:],255*s+b[0],l+(l!=1500))or s.to_bytes(l,"big") for \$L=78\$ TIO \$\endgroup\$
    Jonathan Allan
    –  Jonathan Allan
    2022-01-19 19:09:38 +00:00
    Commented Jan 19, 2022 at 19:09
  • \$\begingroup\$ Hmm, maybe not - I don't understand this error \$\endgroup\$
    Jonathan Allan
    –  Jonathan Allan
    2022-01-19 19:26:30 +00:00
    Commented Jan 19, 2022 at 19:26

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.