21
\$\begingroup\$

Your task is it to output a number as a unary string. You get the number of a non-negative integer input in the range of 0 - 2'000'000'000.

Rules

  • You can choose any character for the digit you like. But it has to be the same everywhere and for every input.
  • Output a unary string by outputting this character N times. N is the input.
  • Input range is 0 - 2'000'000'000 (inclusive)
  • A input of 0 result in a empty string except the optional starting and ending characters.
  • You may or may not add optional starting and ending characters at the start and/or end, for example a newline character or [ and ]. If you do, all outputs (including of the input 0) must have one. This characters are different from the the character for the digit you have chosen.
  • Input integer is given as decimal, hexadecimal, octal or binary number, whatever is convenient for you.
  • You are free to use a pattern of multiple characters for a single digit. But it has to be the same pattern for all digits and for every input.
  • Digit grouping is optional. If you use it, the group size has to be constant and the separation character has to be the same everywhere.
  • Your program should finish in less than 4h on a modern desktop CPU with 4 GB of free RAM, for any valid input.
  • Output is a string or stream of characters.

Examples / test cases:

Input  ->  Output

./yourProgram 1  -> 1
./yourProgram 12 -> 111111111111
./yourProgram 0  -> 
./yourProgram 2000000000 | tr -d -c '1' | wc -c -> 2000000000
./yourProgram 32 -> 11111111111111111111111111111111

Non-golf example program:

#!/usr/bin/env python3
import sys

for i in range( int(sys.argv[1]) ):
  print("1", end='')

This is code-golf, so the shortest code wins.

Related but they have significant differences:

\$\endgroup\$
14
  • \$\begingroup\$ Can I output a list of unary digits? \$\endgroup\$ Commented Jul 18, 2023 at 15:52
  • 2
    \$\begingroup\$ @TheThonnu I would prefer standalone programs/scripts. But gave up on that, because people don't like that for some reason. So it is ok. \$\endgroup\$ Commented Jul 18, 2023 at 16:23
  • 1
    \$\begingroup\$ @12431234123412341234123 Why are you arbitrarily overriding default output methods? generators lists definition of a list Also, if this is a clarification, it should be an edit, not buried in the comments. \$\endgroup\$ Commented Jul 18, 2023 at 23:33
  • 1
    \$\begingroup\$ In particular, this unfairly disadvantages languages like Haskell where the default string type is a lazy linked list. \$\endgroup\$ Commented Jul 18, 2023 at 23:55
  • 1
    \$\begingroup\$ I wonder which answers did actually test the runtime for the big number edge case. \$\endgroup\$
    – Philippos
    Commented Jul 19, 2023 at 8:40

43 Answers 43

1
2
2
\$\begingroup\$

Batch, 26 bytes

@for/l %i in (1,1,%1)do cd

The @ is needed to prevent unwanted echo of the for command, but echo of the cd command is per-iteration anyway so no @ is needed. The four spaces are needed so that the code will parse correctly.

\$\endgroup\$
2
  • \$\begingroup\$ You may or may not add optional starting and ending characters at the start and/or end Couldn't you remove the @? I can't test it. \$\endgroup\$ Commented Jul 20, 2023 at 12:47
  • 1
    \$\begingroup\$ @12431234123412341234123 It would repeat the cd an extra time, which would make the answer invalid. \$\endgroup\$
    – Neil
    Commented Jul 20, 2023 at 15:16
2
\$\begingroup\$

simply, 17 bytes

This creates an anonymous anonymous function that returns the expected result:

fn($x)=>&tb($x,1)

This is a full program that does the same thing, but outputs it (also 17 bytes):

out&tb($argv[0]1)

How does it work?

This runs the function &tb, which is an alias to &tobase.

It takes the number and the base.
It relies on JavaScript's Number.prototype.toString(radix) with a special case to handle unary (where it just outputs 1s).


Ungolfed

Ungolfing both versions at the same time.

All examples here are executable on their own, and take a JSON array as input, with the value you wish.
This isn't a limitation of the program at all - it's just how you pass values to the compiler to populate the $argv variable.

All examples will be written with &tobase for clarity.

Code-like

// Anonymous function - assigned to $fn
$fn = fn($x) => &tobase($x, 1);
// Calls the function and outputs the result
echo call $fn(argv[0], 1);


// Full program
echo &tobase($argv[0], 1);

Plain-English-like

// Anonymous function - assigned to $fn
Set the variable $fn to an anonymous function with argument $x
Begin.
    Return the result of calling the function &tobase with the arguments $x, 1.
End.

// Calls the function and outputs the result
Display the result of calling the variable $fn with the arguments $argv[0], 1.


// Full program
Display the result of calling the function &tobase with the arguments $argv[0], 1.
\$\endgroup\$
2
\$\begingroup\$

D, 43 bytes

(int x){import std;return"1".replicate(x);}

not sure if I can make it much shorter. I can't just do a short lambda (x=>...) because it requires replicate from std.array. I tried doing some stuff with iota and map but that resulted in 4 bytes longer than this simpler solution.

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

ForWhile (C version), 6 bytes

{[9#)}

prints n tabs

Running the program with input 2000000000 takes a bit over 2 minutes and consumes about 150kBit of memory (when using the debug build of the interpreter) so the program is well withing the time and memory limits.

Interpreter

online interpreter Note that the online interpreter does not satisfy the efficiency requirements

Explanation

{    } anonymous procedure
 [  )  repeat n times
  9#   print tab

calling the procedure

{[9#)} 1$  store procedure at address 1
2000000000 1@? call procedure with argument `2000000000`

ForWhile, 26 bytes

99[_48-:0<!:[.'2*+1]).[9#)

Full program, reads binary integer from standard input

99[                 )       \ repeat up to 99 times
   _                        \ get a byte from standard input
    48-                     \ subtract char-code of 0
       :0<!:[      ]        \ if value is non-negative
             .    1         \ temporarily remove condition from stack
              '2*+          \ duplicate current value and add new bit
                    ).      \ break loop if last character was less than 48
                      [9#)  \ print tab n times
\$\endgroup\$
2
\$\begingroup\$

Piet + ascii-piet, 22 bytes (2×11=22 codels)

tabrjcdskcK  a?kqak kk

Try Piet online!


Explanation:

Prints 1 a total of n times.

Trace

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

Python 3, 63 bytes

import sys

n = int(sys.argv[1])
output = '1' * n
print(output)
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Welcome to CGCC! I've edited your answer to add code formatting and a header. This site's for Code Golf, where the goal is to have the shortest code, so you might want to remove the extra newline after the import and shorten output to o \$\endgroup\$ Commented Jul 18, 2023 at 17:14
  • 3
    \$\begingroup\$ print('1'*n) rather than output = '1' * n ; print(output) would be shorter. Remember: Code golf challenge is to make code as short as possible while still fulfilling the requirements while ignoring readability and other things. \$\endgroup\$ Commented Jul 18, 2023 at 17:20
1
\$\begingroup\$

Thue, 21 Bytes

0::=x
1::=x*
*x::=x**

Input is on the state and in binary, output is some number of xs (can be ignored) and the output number of asterisks on the state at the end.

Is this allowed?

\$\endgroup\$
2
  • \$\begingroup\$ @12431234123412341234123 do they have to be? If so, can I require a bracket to be at the beginning of input? \$\endgroup\$
    – Dadsdy
    Commented Jul 21, 2023 at 16:16
  • \$\begingroup\$ Thought about it: No and No. I didn't have this rule in the original question so you don't have to always output the same starting characters. Input is a number. \$\endgroup\$ Commented Jul 21, 2023 at 16:23
1
\$\begingroup\$

Dyalog APL, 3 bytes

⍴∘0

This reshapes 0 to have the same length as the argument to the function, effectively repeating it input times.

For the same length, there's also /∘0 and ⌿∘0.

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

Swift, 33 bytes

let u={for _ in 1...$0{print(0)}}

The "grouping separator" here is a newline after every digit.

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

YASEPL, 10 bytes

=i';"1",i<
\$\endgroup\$
0
\$\begingroup\$

Perl 5 (-p), 7 bytes

$_=1x$_

Try it online!

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

PowerShell, 6 bytes

'1'*$n

Try it online!

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

Scala 3, 5 bytes

"1"*_

Attempt This Online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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