Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Evaluation order of an APL n-train

posix SH + GNU sed, 43 bytes seq $1|tac|sed -zE 's/(\n.+)(\n.+)/\2\1/mg' seq $1 # 1..the argument (inclusive) |tac| # reverse sed # replace...

posted 3y ago by Wezl‭  ·  edited 3y ago by Wezl‭

Answer
#5: Post edited by user avatar Wezl‭ · 2021-06-07T22:37:37Z (over 3 years ago)
golf 7 bytes
  • # posix SH, 50 bytes
  • ```
  • seq $1|tac|sed -zE 's/(
  • [^\n]+)(
  • [^\n]+)/\2\1/g'
  • ```
  • ```bash
  • seq $1 # 1..the argument (inclusive)
  • |tac| # reverse
  • sed # replace
  • -z # null terminated. basically this means that \n is
  • # treated as a normal character, needed because
  • # seq and tr operate on lines
  • E # extended regex so we can use () instead of \(\)
  • ' do this replacement '
  • s/(
  • [^
  • ]+)(
  • [^
  • ]+)/\2\1/g
  • s/ / /g # replace all occurences of
  • \n[^\n]+ # a newline followed by non-newlines
  • ( )(........) # twice
  • / # with
  • \2\1 # swap their places
  • ```
  • Old 52 bytes, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
  • # posix SH + [GNU sed], 43 bytes
  • ```
  • seq $1|tac|sed -zE 's/(
  • .+)(
  • .+)/\2\1/mg'
  • ```
  • ```bash
  • seq $1 # 1..the argument (inclusive)
  • |tac| # reverse
  • sed # replace
  • -z # null terminated. basically this means that \n is
  • # treated as a normal character, needed because
  • # seq and tr operate on lines
  • E # extended regex so we can use () instead of \(\)
  • ' do this replacement '
  • s/(\n.+)(\n.+)/\2\1/mg
  • s/ / /mg # replace all occurences of
  • \n.+ # a newline followed by non-newlines
  • # the (GNU extension) m modifier makes
  • # . not match a newline
  • ( )(....) # twice
  • / # with
  • \2\1 # swap their places
  • ```
  • # posix SH, 50 bytes
  • ```
  • seq $1|tac|sed -zE 's/(
  • [^
  • ]+)(
  • [^
  • ]+)/\2\1/g'
  • ```
  • Old 52 bytes, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
  • [GNU sed]: https://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command "using the 'm' regex modifier"
#4: Post edited by user avatar Wezl‭ · 2021-06-07T21:39:19Z (over 3 years ago)
explain
  • # bash, 50 bytes
  • ```
  • seq $1|tac|sed -zr 's/(
  • [^
  • ]+)(
  • [^
  • ]+)/\2\1/g'
  • ```
  • Old 52 bytes, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
  • # posix SH, 50 bytes
  • ```
  • seq $1|tac|sed -zE 's/(
  • [^
  • ]+)(
  • [^
  • ]+)/\2\1/g'
  • ```
  • ```bash
  • seq $1 # 1..the argument (inclusive)
  • |tac| # reverse
  • sed # replace
  • -z # null terminated. basically this means that \n is
  • # treated as a normal character, needed because
  • # seq and tr operate on lines
  • E # extended regex so we can use () instead of \(\)
  • ' do this replacement '
  • s/(\n[^\n]+)(\n[^\n]+)/\2\1/g
  • s/ / /g # replace all occurences of
  • \n[^\n]+ # a newline followed by non-newlines
  • ( )(........) # twice
  • / # with
  • \2\1 # swap their places
  • ```
  • Old 52 bytes, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
#3: Post edited by user avatar Wezl‭ · 2021-06-07T21:29:10Z (over 3 years ago)
golf -1
  • # bash, 51 bytes
  • ```
  • seq $1 -1 1|sed -zr 's/(
  • [^
  • ]+)(
  • [^
  • ]+)/\2\1/g'
  • ```
  • Old, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
  • # bash, 50 bytes
  • ```
  • seq $1|tac|sed -zr 's/(
  • [^
  • ]+)(
  • [^
  • ]+)/\2\1/g'
  • ```
  • Old 52 bytes, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
#2: Post edited by user avatar Wezl‭ · 2021-06-07T21:26:34Z (over 3 years ago)
golf 1 byte
  • # bash, 52 bytes
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
  • # bash, 51 bytes
  • ```
  • seq $1 -1 1|sed -zr 's/(\n[^\n]+)(\n[^\n]+)/\2\1/g'
  • ```
  • Old, outputting spaces:
  • ```
  • seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
  • ```
#1: Initial revision by user avatar Wezl‭ · 2021-06-07T21:20:48Z (over 3 years ago)
# bash, 52 bytes

```
seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
```