57
\$\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.

msh210
3,50123 silver badges36 bronze badges
asked Dec 16, 2016 at 21:30
\$\endgroup\$
2
  • \$\begingroup\$ What characters can appear in the input? \$\endgroup\$ Commented Dec 16, 2016 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\$ Commented Dec 16, 2016 at 22:42

51 Answers 51

1
2
19
\$\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

answered Dec 16, 2016 at 22:51
\$\endgroup\$
2
  • \$\begingroup\$ Wow, JS is much terser than Python for this. \$\endgroup\$ Commented Dec 16, 2016 at 22:57
  • \$\begingroup\$ @Arnauld I cannot get my head around this. Do you mind explaining how this code works? \$\endgroup\$ Commented Dec 18, 2016 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

answered Dec 17, 2016 at 4:31
\$\endgroup\$
1
  • \$\begingroup\$ Whoa, I don't think I've ever seen a latex answer on ppcg before. \$\endgroup\$ Commented Dec 17, 2016 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>

answered Dec 16, 2016 at 21:40
\$\endgroup\$
4
  • \$\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\$ Commented Dec 17, 2016 at 8:42
  • \$\begingroup\$ @Prasanna Works for me in Firefox; which browser are you using? \$\endgroup\$ Commented Dec 17, 2016 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\$ Commented Dec 17, 2016 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\$ Commented Dec 19, 2016 at 8:38
5
\$\begingroup\$

Lua 5.2, 32 Bytes

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

Where the variable a is the input string.

answered Dec 16, 2016 at 21:51
\$\endgroup\$
0
5
\$\begingroup\$

C, (削除) 67 (削除ここまで) (削除) 65 (削除ここまで) 61 bytes

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

Wandbox

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

Pyth - 7 bytes

+Qfql+Q

Try it online here.

answered Dec 17, 2016 at 0:10
\$\endgroup\$
4
\$\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.

answered Dec 17, 2016 at 6:57
\$\endgroup\$
4
\$\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
}
answered Dec 17, 2016 at 22:32
\$\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)

answered Dec 16, 2016 at 21:44
\$\endgroup\$
2
  • \$\begingroup\$ It always confuses me how buried the actual output is among warnings or other messages :-) \$\endgroup\$ Commented Dec 16, 2016 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\$ Commented Dec 16, 2016 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

answered Dec 16, 2016 at 22:36
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can do (s+`n`)[n:] for n<len(s+`n`). \$\endgroup\$ Commented Dec 16, 2016 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.

answered Dec 17, 2016 at 0:54
\$\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
answered Dec 17, 2016 at 8:58
\$\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.

answered Dec 17, 2016 at 16:01
\$\endgroup\$
6
  • 1
    \$\begingroup\$ This isn't valid then, it must pass all test cases \$\endgroup\$ Commented Dec 17, 2016 at 16:05
  • \$\begingroup\$ So supporting a length up to 999 would be enough? \$\endgroup\$ Commented Dec 17, 2016 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\$ Commented Dec 17, 2016 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\$ Commented Dec 17, 2016 at 16:17
  • \$\begingroup\$ Thank you Dennis, I will modify my submission accordingly \$\endgroup\$ Commented Dec 17, 2016 at 23:11
3
\$\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.

answered Dec 18, 2016 at 9:30
\$\endgroup\$
1
  • \$\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\$ Commented Jan 19, 2017 at 21:53
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*$
$._
answered Dec 16, 2016 at 23:01
\$\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.

answered Dec 17, 2016 at 0:06
\$\endgroup\$
1
  • \$\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\$ Commented Dec 17, 2016 at 1:59
2
\$\begingroup\$

05AB1E, 11 bytes

[13⁄4JDg3⁄4Q#1⁄4\

Pretty straightforward bruteforce:

 Implicit i = 0
[ while true
 13⁄4J Concatenate input and i -> str
 Dg3⁄4Q# Break if length(str) == i
 1⁄4\ Else, i += 1

Try it online!

answered Dec 17, 2016 at 22:47
\$\endgroup\$
2
\$\begingroup\$

PHP, 42 bytes

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

Run with -r. Test at OnlinePHPfunctions.

answered Dec 18, 2016 at 18:02
\$\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.

answered Dec 18, 2016 at 20:00
\$\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.

answered Dec 23, 2016 at 10:00
\$\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"
answered Dec 17, 2016 at 0:03
\$\endgroup\$
2
\$\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"
answered Jan 15, 2017 at 15:51
\$\endgroup\$
2
\$\begingroup\$

Vyxal, 6 bytes

LJL+LJ

Try it Online!

Append the length of the string, take the length of that, then append the new length to the original. This does work with digit overflows (9, 99, etc...).

answered Oct 6, 2022 at 17:50
\$\endgroup\$
5
  • \$\begingroup\$ There's probably something I can do with iteratively adding and appending lengths until they are the same.... \$\endgroup\$ Commented Oct 6, 2022 at 17:54
  • 6
    \$\begingroup\$ The code looks especially funny if you change the + to an J, which works fine: LJLJLJ \$\endgroup\$ Commented Oct 6, 2022 at 20:01
  • \$\begingroup\$ You actually don't need the flag. \$\endgroup\$ Commented Oct 6, 2022 at 20:01
  • \$\begingroup\$ 3(LJ for 4 bytes \$\endgroup\$ Commented Nov 2, 2022 at 13:25
  • \$\begingroup\$ oh..... but mine is funnier :p \$\endgroup\$ Commented Nov 2, 2022 at 13:27
2
\$\begingroup\$

Pip, 8 bytes

L3Ya.#yy

Try It Online!

Explanation

Inspired by Pacmanboss256's Vyxal solution.

 ;; y is empty string; a is first command-line argument (implicit)
L3 ;; Loop 3 times:
 a. ;; Concatenate a with
 #y ;; Length of y
 Y ;; Assign the result to y
 y ;; After the loop, output y

Some worked examples:

Input a "" "aaa" "aaaaaaaaa"
Original y "" "" ""
After loop 1 "0" "aaa0" "aaaaaaaaa0"
After loop 2 "1" "aaa4" "aaaaaaaaa10"
After loop 3 "1" "aaa4" "aaaaaaaaa11"
answered Oct 6, 2022 at 23:25
\$\endgroup\$
1
  • \$\begingroup\$ +1, neat loop setup \$\endgroup\$ Commented Oct 7, 2022 at 3:04
2
\$\begingroup\$

K (ngn/k), 31 bytes

{$[9>t:#x;x,1ドル+t;x,$#x,(1ドル+t)]}

Try it online!

+22 bytes thanks to Dominic van Essen for pointing out a not-working test case (that should have worked)

answered Oct 7, 2022 at 8:56
\$\endgroup\$
5
  • \$\begingroup\$ I think this fails for "aaaaaaaaa": it outputs "aaaaaaaaa10" which is 11 characters long... \$\endgroup\$ Commented Dec 5, 2022 at 16:45
  • \$\begingroup\$ @DominicvanEssen ohhhhhhhh... now I understand why. \$\endgroup\$ Commented Dec 8, 2022 at 8:35
  • \$\begingroup\$ fails for the longer test cases (i.e. you dont handle strings of length 99+) \$\endgroup\$ Commented Jul 14, 2023 at 10:21
  • \$\begingroup\$ {x,$#x,$#x,$#x} for 15 bytes i think? \$\endgroup\$ Commented Jul 14, 2023 at 10:25
  • \$\begingroup\$ {{y,$#x}/4#,x} for 14 bytes, maybe golfable further? \$\endgroup\$ Commented Jul 14, 2023 at 10:31
2
\$\begingroup\$

Vyxal, 26 bitsv2 , 3.25 bytes

3(LJ

Try it Online!

Bitstring:

01001000011111010110110111

Finally posting my golfing suggestion as a separate answer. 3 times, len top and append.

answered Jul 28, 2024 at 0:22
\$\endgroup\$
2
  • \$\begingroup\$ Nice, syntax is very similar to the Thunno 2 equivalent answer \$\endgroup\$ Commented Jul 28, 2024 at 1:05
  • \$\begingroup\$ @noodleperson which is also very similar to the 05AB1E equivalent answer :p \$\endgroup\$ Commented Jul 28, 2024 at 1:08
1
\$\begingroup\$

C#, 77 bytes

n=>{int a=n.Length;int c=(a+1).ToString().Length-1;return(n+(n.Length+1+c));}
answered Dec 16, 2016 at 22:24
\$\endgroup\$
3
  • 1
    \$\begingroup\$ I don't now C#, but couldn't you use return(n+(a+1+c)) as a=n.Length ? \$\endgroup\$ Commented Dec 16, 2016 at 23:44
  • \$\begingroup\$ And also drop the -1 from int c=(a+1).ToString().Length-1 and the +1 from the return? \$\endgroup\$ Commented Dec 16, 2016 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\$ Commented Dec 16, 2016 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
answered Dec 16, 2016 at 21:35
\$\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
answered Dec 17, 2016 at 1:19
\$\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 ]
answered Dec 17, 2016 at 12:35
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

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.