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

Evaluation order of an APL n-train

+7
−0

Description

APL trains are a series of functions, that get applied to an argument in this way:

(f g) x = f g x
(f g h) x = (f x) g (h x)
(a b c d e f) x = (a (b c (d e f))) x = a (b x) c (d x) e (f x)

Trains evaluate from the right to the left, so in the last example, (f x) is evaluated, then (d x), then (d x) e (f x), then (b x), etc.

The final evaluation order there is FDEBCA, or using numbers instead, 6 4 5 2 3 1.

Challenge

Given a number n, output the evaluation order of a train with n functions. Your result can be 0 indexed or 1 indexed.

Examples

Here are the first 10 outputs starting from n=1 (1 indexed)

1 (0 if 0 indexed)
2 1 (1 0 if 0 indexed)
3 1 2
4 2 3 1
5 3 4 1 2
6 4 5 2 3 1
7 5 6 3 4 1 2 
8 6 7 4 5 2 3 1
9 7 8 5 6 3 4 1 2
10 8 9 6 7 4 5 2 3 1
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (2 comments)

7 answers

+3
−0

BQN, 8 bytesSBCS

⍒↕+2|⊢+↕

Run online!

The solution itself is an 8-train, so its evaluation order is given by the sequence 7 5 6 3 4 1 2 0: first the rightmost , then the , then the + between them, and so on—it's fully expanded below. Although it's organized as a train, the computation itself doesn't take advantage of BQN's support for trains.

⍒↕+2|⊢+↕  # Tacit function    (suppose 𝕩=3)
       ↕  # Range of 𝕩         0‿1‿2
     ⊢    # 𝕩, unchanged       3
      +   # Add                3‿4‿5
   2      #               2
    |     # Remainder mod ^    1‿0‿1
 ↕        # Range again        0‿1‿2
  +       # Add                1‿1‿3
⍒         # Descending grade   2‿0‿1

The idea is to start with ascending indices, then group them into mostly-twos by adding one to the first part of each pair. We do this by taking indices modulo 2. However, this means the first pair starts on element index 1—in english, the second element—which is correct for even lengths but wrong for odd lengths. So we flip the odd lengths by adding 𝕩 to the range. Subtracting would also work, in either order: perhaps a more intuitive version is ⊢-↕, which is the distance from each position to one after the end.

After doing this we have a number for each function, and the ordering goes from high to low numbers but left to right for functions with the same number. Descending Grade does exactly this.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Jelly, 8 bytes

ṖUs2UṭµF

Try it online!

-1 byte thanks to caird coinheringaahing

(Also posted on Stack Exchange by myself)

ṖUs2UṭµF   Main Link
Ṗ          pop (remove last element; for numbers, comes with an implicit range)
 U         reverse
  s2       slice into chunks of length 2
    U      reverse each chunk
     ṭ     tack; prepend the input
      µ    (new chain)
       F   flatten the list
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Haskell, 52 48 45 42 bytes

Caught mistake thanks to rak1507

Saved 3 bytes thanks to Hakerh400

f n=n:g[n-1,n-2..1]
g(b:c:t)=c:b:g t
g t=t

Try it online!

g takes the rest of the trains.

-- This is a fork, so append c (monad) and b (dyad)
-- and continue with the rest of the train
g(b:c:t)#=[c,b]++g t
-- t is either empty or a single monad, so finish it off
g t=t

f simply starts it off with the last function n and the other trains 1..n-1 (in reverse).

f n=n:g[n-1,n-2..1]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (3 comments)
+2
−0

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
               -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/(\n[^\n]+)(\n[^\n]+)/\2\1/g'

Old 52 bytes, outputting spaces:

seq -s' ' $1 -1 1|sed -r 's/( [^ ]+)( [^ ]+)/\2\1/g'
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Jelly, 5 bytes

ḶHĊUỤ

Try it online!

Uses xash's method over on CGCC

How it works

 ḶHĊUỤ - Main link. Takes n on the left
 Ḷ     - Range from 0 to n-1
  H    - Halve
   Ċ   - Ceiling
    U  - Reverse
     Ụ - Grade; Sort the indices of n by its elements
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt, 10 bytes

o ÅÔò cÔiU

Try it

o ÅÔò cÔiU     :Implicit input of integer U
o              :Range [0,U)
  Å            :Slice off the first element
   Ô           :Reverse
    ò          :Partitions of length 2
      c        :Map then flatten
       Ô       :  Reverse
        iU     :Prepend U
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Husk, 8 bytes

ηÖ↔mo⌈½ŀ

Try it online!

Same idea as xash's answer from CGCC.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »