Skip to main content
Commonmark migration
Source Link

#Pure Bash, 28

Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 47

Pure Bash, 47

eval printf '%s\\n' ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 47

eval printf '%s\\n' ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

Pure Bash, 47

eval printf '%s\\n' ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

added 23 characters in body
Source Link
Digital Trauma
  • 73.8k
  • 10
  • 117
  • 268

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 3747

eval echoprintf '%s\\n' ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it onlineTry it online.

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 37

eval echo ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 47

eval printf '%s\\n' ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

added 333 characters in body
Source Link
Digital Trauma
  • 73.8k
  • 10
  • 117
  • 268

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 37

eval echo ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

#Pure Bash, 28

eval echo [${1//:/{:,]:[\}}]

Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:

[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]

Try it online.

  • ${1//:/REPLACEMENT} replaces the colons in $1 with {:,]:[\}
  • This generates a brace expansion like [1{:,]:[}2{:,]:[}3{:,]:[}4]
  • The eval (and careful \ escapes) causes the brace expansion to happen last and give the desired result.

If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:

#Pure Bash, 37

eval echo ${1//, /{\\,\\ ,]\\,\\ [\}}

Try it online.

Source Link
Digital Trauma
  • 73.8k
  • 10
  • 117
  • 268
Loading