0
\$\begingroup\$

The problem is to create a string of the same character repeated 100 times.

One potential solution in JavaScript is:

var i = 100,
    s = '';

while(i--) s += "Z";

Shortest code wins.

\$\endgroup\$
  • 14
    \$\begingroup\$ This site is intended for puzzles only, not for practical problems. This creates three issues: 1. The task is too trivial to be interesting as a puzzle. 2. The really short answers aren't in JavaScript, so they won't help you. 3. In code golf, O(n) algorithms will have O(n!) implementations, undefined behavior will be invoked and pretty much anything else that should never, ever be present in code that is actually going to be used will be done, if it saves just a single byte. \$\endgroup\$ – Dennis May 16 '14 at 18:02
  • \$\begingroup\$ I edited the question to be on topic but still allow for the existing answers. \$\endgroup\$ – mbomb007 Apr 6 '16 at 17:23
  • \$\begingroup\$ Some answers took “create a string” literally and only generate the value leaving it lost in the RAM, while other answers stick with the default of outputting/returning the result. For trivial tasks like this, outputting/returning the result in some languages doubles the code length. I am not sure if this question is ready to be reopened. \$\endgroup\$ – manatwork Apr 6 '16 at 18:24
  • 2
    \$\begingroup\$ 3-byte Jelly answer: ⁶׳. This is surely too trivial. \$\endgroup\$ – Erik the Outgolfer Sep 25 '16 at 18:10

11 Answers 11

9
\$\begingroup\$

golflua: 109 15

w(S.t("a",100))

Which is more-or-less equivalent to Heiko's Lua answer

\$\endgroup\$
6
\$\begingroup\$

Python2.7 - 7 bytes

'a'*100

String manipulation using * pretty simple.

\$\endgroup\$
6
\$\begingroup\$

Lua, 24 bytes

This variant is based on Geobits' comment:

print(("").rep("a",100))

Older version, 38 bytes

a=""for i=0,99 do a=a.."a"end print(a)

Ungolfed:

a = ""              -- start with empty string
for i = 0, 99, 1 do -- 100 times
  a = a .. "a"      -- append "a" to variable a
end
print(a)            -- print result
\$\endgroup\$
  • \$\begingroup\$ print(string.rep("a",100)) ? \$\endgroup\$ – Geobits May 16 '14 at 20:04
  • \$\begingroup\$ @Geobits: Very good, much shorter (26 bytes). \$\endgroup\$ – Heiko Oberdiek May 16 '14 at 20:18
  • 1
    \$\begingroup\$ print(("a"):rep(100)), use the object implementation of string.rep, 21 Bytes ;) \$\endgroup\$ – Katenkyo Apr 4 '16 at 13:57
4
\$\begingroup\$

GolfScript, 4 characters:

100*

The input should be on the stack. This program assumes a single-character input.

Test online.

If the character needs to be hardcoded, it's 7 characters:

'a'100*

If the character is a newline and it needs to be hardcoded, 5 characters (thanks to Jan Dvorak):

100n*
\$\endgroup\$
  • \$\begingroup\$ Umm... are you expecting a single-character input? Because 100 times nothing is still nothing. \$\endgroup\$ – John Dvorak May 16 '14 at 16:39
  • \$\begingroup\$ @JanDvorak: Yes, I am. The OP gave an example where he appended a single character 100 times, so I assumed that the input will always be a single character. \$\endgroup\$ – ProgramFOX May 16 '14 at 16:41
  • \$\begingroup\$ In his case, the character was hard-coded in the statement. I that as that you are required to hard-code your own as well. \$\endgroup\$ – John Dvorak May 16 '14 at 16:42
  • \$\begingroup\$ @JanDvorak: Ah, ok. I added an answer where the character is hardcoded, but I'll still keep the not-hardcoded part around if hardcoding the char is not necessary. \$\endgroup\$ – ProgramFOX May 16 '14 at 16:45
  • 2
    \$\begingroup\$ also, n`25* is an interesting solution ;-) \$\endgroup\$ – John Dvorak May 16 '14 at 16:52
4
\$\begingroup\$

QBasic : 22

PRINT STRING$(100,"A")

Why not?

\$\endgroup\$
4
\$\begingroup\$

JavaScript - 20 chars

At least, shorter than Bash ;)

Array(101).join("a")

An array of length 100 joined with letter a.

Perl - 12 chars

First Perl golf !

print"a"x100

Thanks to ace for shortening one character.

\$\endgroup\$
  • 1
    \$\begingroup\$ Array(100).join(a) - 18 characters. An array of length 100 joined with a letter from variable a! :D \$\endgroup\$ – avall May 16 '14 at 17:10
  • \$\begingroup\$ Is this a Ninja edit or did I not notice your Perl answer before I posted mine? And P.S. you can remove the space between print and "a"x100 \$\endgroup\$ – user12205 May 16 '14 at 17:12
  • 4
    \$\begingroup\$ The Javascript solution should be Array(101). Array(100).join("a").length == 99 \$\endgroup\$ – nderscore May 16 '14 at 20:33
  • \$\begingroup\$ @nderscore Thanks, fixing. \$\endgroup\$ – Gaurang Tandon May 17 '14 at 5:04
  • \$\begingroup\$ @ace I had just quickly applied my new Perl skills to my post, and noticed that we had posted the same answer at the same time. I was about to notify you, but the electricity went out. And thanks for the tip :) \$\endgroup\$ – Gaurang Tandon May 17 '14 at 5:06
3
\$\begingroup\$

J (7)

Very similar to the Python one

100$'a'

100$'ab' would output 50 times ab, for 100 characters total.

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

Bash+tr, 20 bytes

$ printf %100s|tr \  Z
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ$

Pure Bash, 31 bytes

$ printf -vs %100s;echo ${s// /Z}
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
$

Pretty standard stuff.


GNU dc, 14 bytes

2 800^255/90*P

Output:

$ dc -e '2 800^255/90*P'
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ$ 
\$\endgroup\$
2
\$\begingroup\$

Perl, 7 bytes

"a"x100

Almost completely the same as the Python one.

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

Brainfuck, 27 characters (or 26 25)

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

This program requires input, because hardcoding a character is not possible in Brainfuck.

If it is allowed to always output 100 times the NUL character, there is a 25 char solution (thanks to Quincunx):

++++++++++[>..........<-]
\$\endgroup\$
  • \$\begingroup\$ Well actually it is possible... if you remove , you will be outputting 100 times of the NUL character. \$\endgroup\$ – user12205 May 16 '14 at 17:18
  • \$\begingroup\$ @ace: Correct, but then you cannot specify the character. \$\endgroup\$ – ProgramFOX May 16 '14 at 17:18
  • \$\begingroup\$ The question never said the character has to be specified anyway... \$\endgroup\$ – user12205 May 16 '14 at 17:19
  • 1
    \$\begingroup\$ This is how I interpret it: "...to create a string of the same character repeated 100 times in as short a statement as possible. One potential solution is:" If you interpret it as having to output the Z characters for 100 times then I think it is still possible in BF: you need to add 90 + in front. \$\endgroup\$ – user12205 May 16 '14 at 17:23
  • 2
    \$\begingroup\$ Actually, if you could output the NUL character, you could simply do ++++++++++[>..........<-], saving 2 chars from your 27 \$\endgroup\$ – Justin May 16 '14 at 20:51
2
\$\begingroup\$

Haskell

replicate 100 'a'

GHCi output:

"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

Code for GHC:

main = print $ replicate 100 'a'
\$\endgroup\$
  • \$\begingroup\$ No need for the space between 100 and 'a'. \$\endgroup\$ – nimi Apr 6 '16 at 17:34

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