Skip to main content
deleted 1 character in body
Source Link
FrownyFrog
  • 3.8k
  • 1
  • 15
  • 20

J, 20 bytes

{~[:{~^:_#\-2+_1".,.

Try it online

                  ,.  Each character on a separate row
              _1".    Convert to numbers, replacing non-numbers with -1
                         (it becomes one row again)
            2+        Add 2.
         #\           Prefix lengths (range 1..length)
           -          Subtract
  [:{~^:_             Index into itself as long as it changes the result
{~                    Index into the original string

Credit to ngn for the inspiration.

J, 22 bytes

22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

In both solutions the version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

J, 20 bytes

{~[:{~^:_#\-2+_1".,.

Try it online

                  ,.  Each character on a separate row
              _1".    Convert to numbers, replacing non-numbers with -1
                         (it becomes one row again)
            2+        Add 2.
         #\           Prefix lengths (range 1..length)
           -          Subtract
  [:{~^:_             Index into itself as long as it changes the result
{~                    Index into the original string

Credit to ngn for the inspiration.

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

In both solutions the version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

J, 20 bytes

{~[:{~^:_#\-2+_1".,.

Try it online

                  ,.  Each character on a separate row
              _1".    Convert to numbers, replacing non-numbers with -1
                         (it becomes one row again)
            2+        Add 2.
         #\           Prefix lengths (range 1..length)
           -          Subtract
  [:{~^:_             Index into itself as long as it changes the result
{~                    Index into the original string

Credit to ngn for the inspiration.

22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

In both solutions the version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

added 924 characters in body
Source Link
FrownyFrog
  • 3.8k
  • 1
  • 15
  • 20

J, 20 bytes

{~[:{~^:_#\-2+_1".,.

Try it online

                  ,.  Each character on a separate row
              _1".    Convert to numbers, replacing non-numbers with -1
                         (it becomes one row again)
            2+        Add 2.
         #\           Prefix lengths (range 1..length)
           -          Subtract
  [:{~^:_             Index into itself as long as it changes the result
{~                    Index into the original string

Credit to ngn for the inspiration.

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

TheIn both solutions the version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

The version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

J, 20 bytes

{~[:{~^:_#\-2+_1".,.

Try it online

                  ,.  Each character on a separate row
              _1".    Convert to numbers, replacing non-numbers with -1
                         (it becomes one row again)
            2+        Add 2.
         #\           Prefix lengths (range 1..length)
           -          Subtract
  [:{~^:_             Index into itself as long as it changes the result
{~                    Index into the original string

Credit to ngn for the inspiration.

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

In both solutions the version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

added 31 characters in body
Source Link
FrownyFrog
  • 3.8k
  • 1
  • 15
  • 20

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |.    The string backwards, because reduce is right-to-left.
            _1".[         The next character as a number (d), -1 if it's not a number.,
         _1-                 and a space character produces an empty array.
         _1-           -1-d
      1{.                 Take 1. AIf spacewe ishave interpreteda asnothing
 an empty array,                       at this point, that makes it a 0.
   ,                      Prepend the next character to the result of the previous call.
    {~                    Select the character. 0 is the first, _2 is second to last.
 ],                       Append the result.

The version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |.    The string backwards, because reduce is right-to-left.
            _1".[         The next character as a number (d), -1 if it's not a number.
         _1-              -1-d
      1{.                 Take 1. A space is interpreted as an empty array, this makes it a 0.
   ,                      Prepend the next character to the result of the previous call.
    {~                    Select the character. 0 is the first, _2 is second to last.
 ],                       Append the result.

The version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

J, 22 bytes

(],,{~1{._1-_1".[)/@|.

This is a port of the Jelly answer.

                    |. The string backwards, because reduce is right-to-left.
            _1".[      The next character as a number (d), -1 if it's not a number,
                          and a space character produces an empty array.
         _1-           -1-d
      1{.              Take 1. If we have a nothing
                          at this point, that makes it a 0.
   ,                   Prepend the next character to the result of the previous call.
    {~                 Select the character. 0 is the first, _2 is second to last.
 ],                    Append the result.

The version that TIO uses interprets a single . as the number 0, so the last test fails. Older versions (≤7) seem to work correctly.

Try it online!

added 31 characters in body
Source Link
FrownyFrog
  • 3.8k
  • 1
  • 15
  • 20
Loading
Source Link
FrownyFrog
  • 3.8k
  • 1
  • 15
  • 20
Loading