51
\$\begingroup\$

Challenge:

Given a string s on the characters a-z, A-Z, 0-9, append the length of s to itself, counting the additional character(s) in the length as part of the total length of s.

Input:

Just a string of arbitrary length (can be empty).

Output:

The same string, but with its length appended to the end. The characters that represent the length should also be counted as part of the length. In cases where there are multiple valid lengths to append, choose the smallest one possible (see test cases for examples).

Test Cases:

INPUT     -> OUTPUT       // Comment
aaa       -> aaa4
          -> 1            // Empty string
aaaaaaaa  -> aaaaaaaa9    // aaaaaaaa10 would also normally be valid, but violates using the smallest number rule mentioned above
aaaaaaaaa -> aaaaaaaaa11
a1        -> a13          // Input can contain numbers at the end of the string, you do not have to handle the fact that it looks like 13 rather than 3.

Longer test case(s):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102

Rules:

This is , so shortest code in bytes wins. Standard loopholes are forbidden. Submissions may be an entire program or a function, and you may either print the result to stdout or return it as a variable from a function.

\$\endgroup\$
  • \$\begingroup\$ What characters can appear in the input? \$\endgroup\$ – Martin Ender Dec 16 '16 at 21:48
  • \$\begingroup\$ @MartinEnder Alphanumerical characters only, 0-9 and A-Z/a-z. So yes, you can have strings with numbers at the end. I'll add a test case for one. \$\endgroup\$ – Yodle Dec 16 '16 at 22:42

40 Answers 40

1
2
1
\$\begingroup\$

awk, 37 bytes

{FS="";r=$0;$0=r NF;$0=r NF;$0=r NF}1

That will work in any awk that splits lines into characters when FS is NULL e.g. GNU awk and most others. WIth other awks you'd have to use the length() function instead of NF to calculate the number of characters in the record, e.g. awk '{r=$0;$0=r length;$0=r length;$0=r length}1' file.

Breakdown:

FS=""    # split the record at every character so NF is a count of characters in the record
r=$0     # save the original record
$0=r NF  # append the original length to the original record and cause awk to recalculate NF
$0=r NF  # append the new length to the original record and cause awk to recalculate NF again
$0=r NF  # append the final length to the original record for output
1        # a true condition invoking awks default action of printing the current record

Example:

$ awk '{FS="";r=$0;$0=r NF;$0=r NF;$0=r NF}1' file
aaa4
1
aaaaaaaa9
aaaaaaaaa11
a13
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Jelly, 12 bytes

Many thanks to Dennis for his help in writing this answer. Golfing suggestions welcome. Try it online!

D;³L=
LÇ1#;@

Explanation

LÇ1#;@  Main link. Argument: s
          Runs the helper link until a suitable x is found.
L       Yields the length of s.
 Ç1#    Calls the helper link until one match is found.
    ;@  Appends the matching x to s.

D;³L=  Helper link. Argument: x
         Appends x to s and checks if its length is equal to x.
D      Convert x from integer to decimal.
 ;³    Append to our original string.
   L=  Check if the length of this new string is equal to x.
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl, 31 bytes

Just going through all the numbers until one fits:

perl -lpe '1while++$n<length$_.$n;$_.=$n'
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 43 36 bytes

Anonymous function, abusing the $. global.

->s{$.+=1while(t=s+"#$.").size>$.;t}

Call it like this:

f = ->s{$.+=1while(t=s+"#$.").size>$.;t}
f[""]          # => "1"
f["a"]         # => "a2"
f["aaaaaaa"]   # => "aaaaaaa8"
f["aaaaaaaa"]  # => "aaaaaaaa9"
f["aaaaaaaaa"] # => "aaaaaaaaa11"
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

C, 75 bytes

Yes, there's another C answer but this (the same method as my Factor answer) is shorter:

i,z;f(char*s){i=strlen(s);z=1+log10(i);sprintf(realloc(s,i+z)+i,"%d",i+z);}

Ungolfed:

i, z;

g (char* s){
  i = strlen(s);
  z = 1 + log10(i);
  s = realloc(s, i + z);
  sprintf(s + i, "%d", i + z);
}

ceil(log10(n) is the number of digits in base 10 n, but 1+ is shorter than ceil().

Then, resize the string so we don't get a segfault, and format the number after the string part.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I don't believe this method works for 98 or 997 \$\endgroup\$ – Blue Dec 17 '16 at 16:37
0
\$\begingroup\$

Under review

Powershell V2.0, 64 bytes

$s=$args[0]
$s+[string]($s.length+([string]($s.length)).length)

Example

PS C:\Users\***\Downloads\golf> .\str-length.ps1 'This is my strin3333333333333333333333333333333333333333333333333333
33333333333333333333333333333333333333333g'
This is my strin333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333g113
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This outputs aaaaaaaaa10 when given input aaaaaaaaa, and not the correct aaaaaaaaa11. \$\endgroup\$ – AdmBorkBork Dec 19 '16 at 16:34
  • \$\begingroup\$ You're right, I missed that somehow. \$\endgroup\$ – Kieron Davies Dec 20 '16 at 8:34
0
\$\begingroup\$

Scala, 75 bytes

def a(b:String,c:Int=0):String=if((b+c).length>c)a(b,c+1)else b+c
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

awk, 27 bytes

$0=$0length($0length($0NF))

Test it:

$ awk -F '' '$0=$0length($0length($0NF))' file
7aaaaaa8
8aaaaaaa9
9aaaaaaaa11
97aaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa99
98aaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa10aaaaaaaa101
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can change length($0) to just length for brevity. \$\endgroup\$ – Ed Morton Dec 23 '16 at 16:40
  • \$\begingroup\$ @EdMorton Been experimenting with awk -F '' '$0=NF' \$\endgroup\$ – James Brown Dec 24 '16 at 13:46
0
\$\begingroup\$

C# / CS Script 99 87 79 bytes

Saved a few bytes switching to a lambda. And a few more moving the try/catch out of the for loop.

F=a=>{int i=0,l;try{for(;;)l=(a+(a+a.Length).Length)[++i];}catch{}return a+i;};

Try it here

Expanded:

F => a
{
    // 'i' is for traversing the string
    // 'l' is just a placeholder
    int i = 0, l;

    // wrap the loop in a try/catch
    try
    {
        // loop forever
        for (;;)
            // assign to 'l' the char at position ++i
            // throws an IndexOutOfRangeException when it falls off the end
            l = (a + (a + a.Length).Length)[++i];
    }
    // empty catch block
    catch {}

    // returns a string concat of the input a and index i
    return a + i;
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Java 7, 67 bytes

String y(String y){int i=0;for(;(y+i).length()!=i;i++);return y+i;}

Try it online!

| improve this answer | |
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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