-17
\$\begingroup\$

This question already has an answer here:

Given a number between 0 and 999999999 and output its length.

This is what I managed to work out (scoring 36):

s;main(){putchar(48+strlen(gets(&s)));}

Requirements:

  • Take ONE input string (with a trailing newline, LF) from STDIN.
  • Output a number (between 0 and 9, see notes below) to STDOUT.
    A trailing newline does not matter.

Bonuses:

  1. Takes input that is not numbers (e.g. abcdefg and output 7) but printable ASCII characters instead. The only newline LF is the last one. No CR. Beware of spaces and tabs. Reward: -3 bytes in score
  2. Takes input of a greater length. Reward: -3 bytes if 9 < max length < 232, -6 bytes if max length >= 232. Practical compiler/interpreter must exist.
  3. Complete both 1 and 2 for an extra -3 bytes.

Please note in your answer if you qualify for any bonuses.

\$\endgroup\$

marked as duplicate by Beta Decay, Timtech, Mnemonic, Mr. Xcoder code-golf Jul 20 '18 at 16:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Anyone comes up with a C program shorter than 39? \$\endgroup\$ – iBug Jul 4 '17 at 10:48
  • 4
    \$\begingroup\$ Bonuses are generally frowned upon in this community. \$\endgroup\$ – Okx Jul 4 '17 at 11:18
  • 1
    \$\begingroup\$ I would recommend that you don't include bonus 1. That makes it no different and no more difficult than 'calculate the length of a string'. However, it's a bit too late to do this now; you should use the sandbox in the future. \$\endgroup\$ – MD XF Jul 31 '17 at 18:28

16 Answers 16

4
\$\begingroup\$

Python 3, 3 - 12 = -9 bytes

Qualifies for all bonuses

len

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Mathematica, 12-12=0

StringLength
\$\endgroup\$
1
\$\begingroup\$

brainfuck, 9 - 3 = 6 bytes

,[>+<,]>.

Qualifies string bonus. Outputs as a character code.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Better if in readable number. \$\endgroup\$ – iBug Jul 4 '17 at 10:53
  • \$\begingroup\$ Brainfuck has upper limit 255. \$\endgroup\$ – Erik the Outgolfer Jul 4 '17 at 10:53
  • \$\begingroup\$ @EriktheOutgolfer Ah, I'm stupid :P \$\endgroup\$ – Okx Jul 4 '17 at 10:55
1
\$\begingroup\$

Braingolf, 2 - 6 = -4 bytes

dl

Try it online!

Only works if input is entirely numerical

Or:

Braingolf, 1 - 12 = -11 bytes

l

Try it online!

Only works if input contains at least one non-numerical character

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 1 - 12 = -11 bytes

g

Qualifies for the first bonus and the second, and then another -3 bytes.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Actually -12 bytes. \$\endgroup\$ – Erik the Outgolfer Jul 4 '17 at 10:41
  • \$\begingroup\$ @EriktheOutgolfer Actually -12 bytes (-3 bytes if you do both bonuses) \$\endgroup\$ – Okx Jul 4 '17 at 10:42
  • \$\begingroup\$ Erm, that's what I said isn't it? \$\endgroup\$ – Erik the Outgolfer Jul 4 '17 at 10:43
  • \$\begingroup\$ How is 1 - 3 - 6 - 3 = -12 and not -11? \$\endgroup\$ – Adám Jul 4 '17 at 10:45
  • \$\begingroup\$ @Adám I can do maths :P \$\endgroup\$ – Okx Jul 4 '17 at 10:45
0
\$\begingroup\$

Neim, 1 -6 = -5 bytes

𝐥

Builtin that gets the length. A trailing newline can be added to the input, and will not affect the output. -6 bytes because it can accept any length.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Retina, 1 - 9 = -8 bytes

Qualifies for the first bonus and for the first part of the second bonus

.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog) 64 bit, 1 - 12 = -11 bytes

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Windows batch, 53 - 3 - 3 - 3 = 44 bytes

@echo %1>f
@for %%G in (f)do @set/ar=%%G-2
@echo %r%

Redirect input to file, that get file length without CR\LF.

\$\endgroup\$
0
\$\begingroup\$

Japt, 1-6= -5 bytes

Qualifies for the first bonus and the first part of the second bonus. Theoretically it should qualify for the second part too as the maximum length of a string in JavaScript is 2**53-1 but I'm not aware of any browser that can handle anything longer than ~2**30.

l

Test it

\$\endgroup\$
  • \$\begingroup\$ if it meets the -6 length bonus AND the -3 non-numerical bonus, you get another -3, so it's 1 - 12 = -11 \$\endgroup\$ – Skidsdev Jul 4 '17 at 10:54
  • \$\begingroup\$ @Mayube, strings in JS can be up to 2**53-1 characters in length but I don't know if there's any browser that can handle strings longer than ~2**30 - I'm looking into it at the moment. \$\endgroup\$ – Shaggy Jul 4 '17 at 10:57
0
\$\begingroup\$

JavaScript (ES6), 11-6= 5 bytes

Qualifies for the first bonus and the first part of the second bonus. Theoretically it should qualify for the second part too as the maximum length of a string in JavaScript is 2**53-1 but I'm not aware of any browser that can handle anything longer than ~2**30.

s=>s.length
\$\endgroup\$
0
\$\begingroup\$

Pyth, 2 - 3 - 6 - 3 = -10 (?) bytes

I think it qualifies for all, because Pyth is python-based:

lw

My first Pyth submission. Hooray!

Try it online!

Explanation

lw     Full program: Get length of raw_input

l      Get length (string)
 w     Take raw input 
\$\endgroup\$
0
\$\begingroup\$

Lua, 10 - 3 - 3 - 3 = 1 bytes

string.len

I couldn't get lua to work with more than 2^31 length strings. If I manage to do that, I'll update here.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 5 bytes

s->#s

Try it online!

\$\endgroup\$
0
\$\begingroup\$

dc, 2 - 12 = -10 bytes

Zn

Takes length of TOS and prints it. Note that strings must be surrounded by [].

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 2-6= -4 bytes

log(Ans

Input must be numerical and less than 2^100

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.