Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
    Tweeted twitter.com/StackCodeGolf/status/719947330024247297
14 assert.stringEqual is ===, see https://github.com/substack/tape
source | link

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length number is equal or greater than the length of the string. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) >=== "left-padded string"

left_pad("foo", 5) >=== "  foo"
left_pad("foobar", 6) >=== "foobar"
left_pad("1", 2, "0") >=== "01"
left_pad("1", 2, "-") >=== "-1"

This is , so the submission with the smallest number of bytes wins!

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length number is equal or greater than the length of the string. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) > "left-padded string"

left_pad("foo", 5) > "  foo"
left_pad("foobar", 6) > "foobar"
left_pad("1", 2, "0") > "01"
left_pad("1", 2, "-") > "-1"

This is , so the submission with the smallest number of bytes wins!

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length number is equal or greater than the length of the string. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) === "left-padded string"

left_pad("foo", 5) === "  foo"
left_pad("foobar", 6) === "foobar"
left_pad("1", 2, "0") === "01"
left_pad("1", 2, "-") === "-1"

This is , so the submission with the smallest number of bytes wins!

    Post Reopened by Dennis
13 added 4 characters in body
source | link

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length of the stringnumber is greater or equal toor greater than the length of the string. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) > "left-padded string"

left_pad("foo", 5) > "  foo"
left_pad("foobar", 6) > "foobar"
left_pad(1"1", 2, "0") > "01"
left_pad(1"1", 2, "-") > "-1"

This is , so the submission with the smallest number of bytes wins!

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length of the string is greater or equal to the length. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) > "left-padded string"

left_pad("foo", 5) > "  foo"
left_pad("foobar", 6) > "foobar"
left_pad(1, 2, "0") > "01"
left_pad(1, 2, "-") > "-1"

This is , so the submission with the smallest number of bytes wins!

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length number is equal or greater than the length of the string. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) > "left-padded string"

left_pad("foo", 5) > "  foo"
left_pad("foobar", 6) > "foobar"
left_pad("1", 2, "0") > "01"
left_pad("1", 2, "-") > "-1"

This is , so the submission with the smallest number of bytes wins!

12 added 865 characters in body; edited title
source | link

javascript Javascript left-pad golf

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

Your code should take 3 inputs:The stringleft-pad function consists of 2 default arguments and 1 additional argument, in the form padlengthstring, and padstringlength, (padchar). If padstring doesn't existthe padchar is not given, it defaultsthis is standardized to a spacespace character. The taskLet's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad string onthis string in the left with enough copiesuntil the length of padstring that the full string reacheshas reached the length given in the function. In this case 6. Since the padchar is padlengthnot given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and then remove any excess chars from the left sideinitial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

Test casesWe just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the (first 4 fromstring, length, and maybe the additional argument original left-pad test suite):padchar, output the left-padded string. You can assume that the length of the string is greater or equal to the length. The padchar will always consist of 1 character.

Test cases

fleft_pad('foo'"string", 5)length, ===("padchar")) '> "left-padded foo';string"
f
left_pad('foobar'"foo", 65) ===> 'foobar';"  foo"
fleft_pad(1, 2"foobar", 06) ===> '01';"foobar"
fleft_pad(1, 2, '-'"0") ===> '-1';"01"
fleft_pad('a'1, 42, 'ab'"-") ===> 'baba';"-1"

MayThis is , so the shortest answer winsubmission with the smallest number of bytes wins!

javascript left-pad golf

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

Your code should take 3 inputs: string, padlength, and padstring. If padstring doesn't exist, it defaults to a space. The task is to pad string on the left with enough copies of padstring that the string reaches length padlength, and then remove any excess chars from the left side.

Test cases (first 4 from the original left-pad test suite):

f('foo', 5) === '  foo';
f('foobar', 6) === 'foobar';
f(1, 2, 0) === '01';
f(1, 2, '-') === '-1';
f('a', 4, 'ab') === 'baba';

May the shortest answer win!

Javascript left-pad golf

Introduction

In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad.

The left-pad function consists of 2 default arguments and 1 additional argument, in the form string, length, (padchar). If the padchar is not given, this is standardized to a space character. Let's take an example with two arguments:

left_pad("abc", 6)

First, we observe the length of the string, which is 3. After this, we need to pad this string in the left until the length of the full string has reached the length given in the function. In this case 6. Since the padchar is not given, we need to pad this with spaces:

   abc

This is a string with 3 spaces and the initial string, resulting into a string with length 6. Here is an example with the padchar given:

left_pad("abc", 6, "-")

We just do the same as the example above, but replace the spaces with the padchar. In this case, the hyphen:

---abc

The Task

Given the string, length, and maybe the additional argument padchar, output the left-padded string. You can assume that the length of the string is greater or equal to the length. The padchar will always consist of 1 character.

Test cases

left_pad("string", length, ("padchar")) > "left-padded string"

left_pad("foo", 5) > "  foo"
left_pad("foobar", 6) > "foobar"
left_pad(1, 2, "0") > "01"
left_pad(1, 2, "-") > "-1"

This is , so the submission with the smallest number of bytes wins!

    Post Closed as "unclear what you're asking" by Dennis
11 added 89 characters in body
source | link
    Post Reopened by Qwertiy, CalculatorFeline, edc65, Adnan, jrich
10 new test case with multichar padding
source | link
9 added spec
source | link
8 typ0
source | link
    Post Closed as "unclear what you're asking" by Dennis
7 edited tags
| link
6 removed pointless tag
| link
5 edited title
| link
    Post Reopened by Doorknob
4 code-golf-ified
source | link
3 Added more objective passing criteria
source | link
    Post Closed as "too broad" by Denker, xnor, LegionMammal978, Doorknob
2 edited tags
| link
1
source | link