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

4
\$\begingroup\$

Pyth - 7 bytes

+Qfql+Q

Try it online here.

\$\endgroup\$
18
\$\begingroup\$

JavaScript (ES6), 32 bytes

f=(s,n=0)=>(s+n)[n]?f(s,n+1):s+n

How it works

f = (s, n = 0) =>   // given a string 's' and starting with n = 0:
  (s + n)[n] ?      // if the Nth character of (s + n) exists:
    f(s, n + 1)     //   try again with n + 1
  :                 // else
    s + n           //   return s + n

Starting with N=0, we test the Nth character (0-based) of the string made of the concatenation of the original input string and the decimal representation of N. We increment N until this character doesn't exist anymore.

Example:

N =  0 : abcdefghi0
         ^
N =  1 : abcdefghi1
          ^
N =  2 : abcdefghi2
           ^
...
N =  8 : abcdefghi8
                 ^
N =  9 : abcdefghi9
                  ^
N = 10 : abcdefghi10
                   ^
N = 11 : abcdefghi11    -> success
                    ^

Test cases

f=(s,n=0)=>(s+n)[n]?f(s,n+1):s+n

console.log(f("aaa"));       // -> aaa4
console.log(f(""));          // -> 1
console.log(f("aaaaaaaa"));  // -> aaaaaaaa9
console.log(f("aaaaaaaaa")); // -> aaaaaaaaa11
console.log(f("a1"));        // -> a13

\$\endgroup\$
  • \$\begingroup\$ Wow, JS is much terser than Python for this. \$\endgroup\$ – mbomb007 Dec 16 '16 at 22:57
  • \$\begingroup\$ @Arnauld I cannot get my head around this. Do you mind explaining how this code works? \$\endgroup\$ – Gowtham Dec 18 '16 at 15:46
12
\$\begingroup\$

LaTeX, 108/171

\newcounter{c}\def\s#1#2]{\stepcounter{c}\def\t{#2}\ifx\empty\t\arabic{c}\else#1\s#2]\fi}\def\q[#1]{\s#10]}

\q[] //1

\$\endgroup\$
  • \$\begingroup\$ Whoa, I don't think I've ever seen a latex answer on ppcg before. \$\endgroup\$ – pajonk Dec 17 '16 at 16:02
5
\$\begingroup\$

JavaScript (ES6), 37 bytes

f=(s,t=s,u=s+t.length)=>t==u?t:f(s,u)
<input oninput=o.textContent=f(this.value)><pre id=o>

\$\endgroup\$
  • \$\begingroup\$ When I clicked on Run Code Snippet I get to see an error message. I have no knowledge of Javascript - I was just trying \$\endgroup\$ – Prasanna Dec 17 '16 at 8:42
  • \$\begingroup\$ @Prasanna Works for me in Firefox; which browser are you using? \$\endgroup\$ – Neil Dec 17 '16 at 10:54
  • \$\begingroup\$ @Prasanna Works on latest Google Chrome. Are you sure you aren't using IE11 or older, Opera or anything that doesn't support ES6? \$\endgroup\$ – Ismael Miguel Dec 17 '16 at 20:41
  • \$\begingroup\$ I'm using an old good chrome (Version 48.0.2564.97). I will try this with IE too. Can't update my chrome - office security issues \$\endgroup\$ – Prasanna Dec 19 '16 at 8:38
5
\$\begingroup\$

C, 67 65 61 bytes

x;f(*v){printf("%d",(int)log10(x=-~printf(v))*-~(x%10>8)+x);}

Wandbox

\$\endgroup\$
  • 1
    \$\begingroup\$ Ohh, yeah, I shoulda printf'd... Anyways, congrats on having the shorter C solution :D +1 \$\endgroup\$ – cat Dec 17 '16 at 16:05
4
\$\begingroup\$

Lua 5.2, 32 Bytes

a=arg[1]print(a..#a+#(''..#a+1))

Where the variable a is the input string.

\$\endgroup\$
3
\$\begingroup\$

Pyke, 8 bytes (old version)

.f+liq)+

Explanation:

.f    )  -  first where (i++)
  +      -    input + i
   l     -    len(^)
    iq   -   ^ == i
       + - input + ^

Try it here! (New version, 9 bytes)

\$\endgroup\$
  • \$\begingroup\$ It always confuses me how buried the actual output is among warnings or other messages :-) \$\endgroup\$ – Luis Mendo Dec 16 '16 at 23:28
  • 2
    \$\begingroup\$ I should really get around to fixing the web bug in the copy link that automatically disables the warnings switch \$\endgroup\$ – Blue Dec 16 '16 at 23:31
3
\$\begingroup\$

Python 2, 54 48 46 bytes

Simple solution. Recursion ended up being shorter.

f=lambda s,n=0:f(s,n+1)if(s+`n`)[n:]else s+`n`

Try it online

\$\endgroup\$
  • 1
    \$\begingroup\$ I think you can do (s+`n`)[n:] for n<len(s+`n`). \$\endgroup\$ – xnor Dec 16 '16 at 22:48
3
\$\begingroup\$

Haskell, 46 bytes

f s=[l|i<-[0..],l<-[s++show i],length l==i]!!0

Usage example: f "aaaaaaaa" -> "aaaaaaaa9".

Simply try all numbers starting with 0 and take the first that fits.

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 57 bytes

#<>ToString[(a=Length@#)+(i=IntegerLength)[a+i@a]~Max~1]&

Unnamed function taking an array of characters as input and returning a string. Uses the fact that if a is the length of the input, then the number to append to the input is a plus the number of digits in (a + the length of a), rather than just a plus the number of digits of a. Unfortunately it wouldn't give the right answer for the empty-string input without the ~Max~1 special case.

\$\endgroup\$
3
\$\begingroup\$

Brachylog, 13 bytes

l<L$@:?rc.lL,

Try it online!

Explanation

Basically a description of the problem. It will try every value of L bigger than the length of the input until it finds one for which, when concatenated to the input, is the length of that concatenation.

l<L              length(Input) < L
  L$@            Convert L to a string
     :?rc.       The Output is the concatenation of the Input with L as string
         .lL,    The length of the Output is L itself
\$\endgroup\$
3
\$\begingroup\$

Brainfuck, 258 bytes

,>+<----------[++++++++++>+[>+<-],----------]<[<]>[.>]>>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]

The input must be terminated by a linefeed (LF). Only works for inputs with a length lesser than 256 (including the LF).

Try it online!

Explanation

# read first char and add one to cell #1
# the cell after the input will contain the length
,>+<
# subtract 10 to check for LF
----------
# while the input is not 10 (LF)
[
# restore the input to its original value
++++++++++
# add one to the length
>+
# cut and paste the length to the next cell, then read the input
[>+<-],
# subtract 10 to check for LF
----------
]
# for input abc, the tape here would be: a b c *0* 4
# rewind to the beginning of the input
<[<]>
# print the input string
[.>]>
# convert the length to ascii chars and output them
>>++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-
<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++
<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]

Note: I used code from this SO answer to convert the length to ascii output; I hope this is acceptable on PPCG. This is my first Codegolf submission, and my second BF program. Feedback is welcomed.

\$\endgroup\$
  • 1
    \$\begingroup\$ This isn't valid then, it must pass all test cases \$\endgroup\$ – cat Dec 17 '16 at 16:05
  • \$\begingroup\$ So supporting a length up to 999 would be enough? \$\endgroup\$ – Forcent Vintier Dec 17 '16 at 16:10
  • \$\begingroup\$ The spec says "arbitrary length" which means "as long as your language is capable of handling or without running out of memory" \$\endgroup\$ – cat Dec 17 '16 at 16:11
  • \$\begingroup\$ The brainfuck interpreter you're using has 8-bit cells, so as long as your algorithm works for strings of arbitrary length, it should be fine if it fails for strings of length 256 or higher. The C and JavaScript submissions will also fail once the strings get too long. \$\endgroup\$ – Dennis Dec 17 '16 at 16:17
  • \$\begingroup\$ Thank you Dennis, I will modify my submission accordingly \$\endgroup\$ – Forcent Vintier Dec 17 '16 at 23:11
2
\$\begingroup\$

Retina, 22 bytes

\G`
.
x
+r`\d*$
$._
x

Try it online!

Ah well, if it wasn't for digits appearing in the input, this would be merely 11 bytes:

+r`\d*$
$._
\$\endgroup\$
2
\$\begingroup\$

Ruby, 62 58 56 bytes

s=gets.chomp;p s+"#{(s+"#{(s+"#{s.size}").size}").size}"

Tested in irb.

There's probably a better way to do this, but this was the first thing I came up with. Any help in golfing would be appreciated.

edit: I realized my use of parentheses was excessive.

\$\endgroup\$
  • \$\begingroup\$ You only use l in one place. If you inline that, you will save 3 bytes l=;. But your solution will still be longer than mine ;) \$\endgroup\$ – DepressedDaniel Dec 17 '16 at 1:59
2
\$\begingroup\$

Perl 6,  46  35 bytes

{$_~(.chars,*.chars+.chars...{$^a==$^b})[*-1]}
{$_~(.chars,*.chars+.chars...*)[2]}

Try it

Expanded:

{   # bare block lambda with implicit parameter 「$_」

  $_  # the input

  ~   # concatenated with

  (  # sequence generator

    .chars,  # the number of chars in 「$_」 (seed the generator)


    *\      # Whatever lambda input (represents previous value)
    .chars  # number of chars in that
    +       # plus
    .chars  # the number of chars in 「$_」


    ...     # keep doing that until

    *       # indefinitely

  )[2] # get the value at index 2 of the sequence
}
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 11 bytes

[¹¾JDg¾Q#¼\

Pretty straightforward bruteforce:

            Implicit i = 0
[           while true
 ¹¾J        Concatenate input and i -> str
    Dg¾Q#   Break if length(str) == i
         ¼\ Else, i += 1

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python, 39 bytes

lambda a:eval('a+str(len('*3+'a))))))')

Longer form:

lambda a:a+str(len(a+str(len(a+str(len(a))))))

Iteratively in Python 2 (41 bytes):

x=a=input();exec"x=a+`len(x)`;"*3;print x

Starting with x as the input string a, applies the transformation x -> a + str(len(x)) three times. I'm still not clear why three applications are needed to always reach the fixed point.

\$\endgroup\$
  • \$\begingroup\$ Why 3 times? First to append the length of text, second to adjust the length to include the number, third in case that adjustment added an extra digit. \$\endgroup\$ – Tom Viner Jan 19 '17 at 21:53
2
\$\begingroup\$

PHP, 42 bytes

while(++$n<strlen($a=$argv[1].$n));echo$a;

Run with -r. Test at OnlinePHPfunctions.

\$\endgroup\$
2
\$\begingroup\$

bash, 47 bytes

 for((n=-1;${#s} != $n;));{ s=$1$[++n];};echo $s

Save this as a script, and pass the input string as an argument.

It's a brute force implementation: try each number in turn until you find one which works.

\$\endgroup\$
2
\$\begingroup\$

><> (Fish) 35 bytes

i:1+?!v:o
ln;v9l<  >
*9+>:&)?!^1l&a

Takes input onto the stack, checks the length against values 9,99,999... and if the length is larger than add 1 to the stack length.

\$\endgroup\$
2
\$\begingroup\$

Haskell, 61 60 bytes

e=length
l%n|s<-show$l+1,n>e s=s|m<-n+1=(l+1)%m
c s=s++e s%2

Try it online!

Recursive solution. Usage:

Prelude> c "aaaaaaaaa"
"aaaaaaaaa11"
\$\endgroup\$
1
\$\begingroup\$

C#, 77 bytes

n=>{int a=n.Length;int c=(a+1).ToString().Length-1;return(n+(n.Length+1+c));}
\$\endgroup\$
  • 1
    \$\begingroup\$ I don't now C#, but couldn't you use return(n+(a+1+c)) as a=n.Length ? \$\endgroup\$ – Laikoni Dec 16 '16 at 23:44
  • \$\begingroup\$ And also drop the -1 from int c=(a+1).ToString().Length-1 and the +1 from the return? \$\endgroup\$ – Laikoni Dec 16 '16 at 23:46
  • 1
    \$\begingroup\$ Wait, does this handle the larger test cases correctly? It looks like it returns aa...a100 instead of aa...a101 for the 99a test case. \$\endgroup\$ – Laikoni Dec 16 '16 at 23:52
1
\$\begingroup\$

MATL, 11 bytes

`G@Vhtn@>]&

Try it online! Or verify all test cases.

`      % Do...while
  G    %   Push input
  @    %   Push iteration index (1-based)
  V    %   Convert number to string
  h    %   Concatenate horizontally
  t    %   Duplicate
  n    %   Get length of concatenated string
  @    %   Push iteration index
  >    %   True if length of concatenated string exceeds iteration index
]      % End. Run next iteration if top of stack is true; else exit loop
&      % Specifiy that next function (implicit display) takes only one input
       % Implicitly display top of the stack. This is the concatenated string
       % that had a length equal to the iteration index
\$\endgroup\$
1
\$\begingroup\$

Ruby, 51 bytes (program)

Ruby, 49 bytes (function)

Program (last newline is not necessary and thus unscored):

x=gets.strip
i=0
i+=1 until(y=x+i.to_s).size==i
p y

Function (last newline is scored):

def f x
i=0
i+=1 until(y=x+i.to_s).size==i
y
end
\$\endgroup\$
1
\$\begingroup\$

Factor, 55 bytes

It's a walk in the park! I came up with this in my head as soon as I read the question.

[ dup length dup log10 ⌈ + >integer 10 >base append ]
\$\endgroup\$
1
\$\begingroup\$

Clojure, 72 bytes

(defn f([s](f s 1))([s n](if(=(count(str s n))n)(str s n)(f s(inc n)))))
\$\endgroup\$
1
\$\begingroup\$

R, 49 bytes

cat(a<-scan(,""),(t<-nchar(a))+nchar(t+1),sep='')

Pretty straightforward solution.

\$\endgroup\$
  • \$\begingroup\$ This doesn't work for me: Read 1 item Error in nchar(x + 1) : object 'x' not found. I found that (t<-nchar(a))+... did work. \$\endgroup\$ – JAD Dec 18 '16 at 16:19
  • \$\begingroup\$ @JarkoDubbeldam : My bad ! \$\endgroup\$ – Frédéric Dec 18 '16 at 16:22
1
\$\begingroup\$

Wolfram, 56

#<>ToString@Nest[l+IntegerLength@#&,l=StringLength@#,2]&

Given l = StringLength[x] it appends l + IntegerLength[l + IntegerLength[l]] to x.

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

Labyrinth, 48 45 41 bytes

)"   10/{:@!
.,;: _ { _ ;
   })"}) 10-9!@

Try it online!

Saved 4 bytes thanked to @Martin Ender

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

ForceLang, 83 bytes

set s io.readln()
label 1
set n 1+n
set t s+n
if t.len=n
 io.write t
 exit()
goto 1
\$\endgroup\$

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.