Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

7 of 8
edited body
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Dyalog APL, (削除) 24 (削除ここまで) (削除) 23 (削除ここまで) (削除) 21 (削除ここまで) (削除) 20 (削除ここまで) 19 bytes

×ばつ(M←+/÷≢)

This defines an unnamed, monadic function train, which is equivalent to the following function.

{.5*⍨M(×ばつ⍨⍵)×ばつ(M←{(+/⍵)÷≢⍵})⍵}

Try them online on TryAPL.

How it works

The code consists of several trains.

M←+/÷≢

This defines a monadic 3-train (fork) M that executes +/ (sum of all elements) and (length) for the right argument, then applies ÷ (division) to the results, returning the arithmetic mean of the input.

×ばつM

This is another fork that applies M to the right argument, repeats this a second time, and applies ×ばつ (product) to the results, returning μ2.

×ばつ⍨-(×ばつM)

This is yet another fork that calculates the square of the arithmetic mean as explained before, applies ×ばつ (product with itself) to the right argument, and finally applies - (difference) to the results.

For input (x1, ..., xN), this function returns (x1 - μ2, ..., xN - μ2).

*∘.5∘M

This composed function is applies M to its right argument, then *∘.5. The latter uses right argument currying to apply map input a to a*0.5 (square root of a).

(*∘.5∘M)(×ばつ⍨-(×ばつM))

Finally, we have this monadic 2-train (atop), which applies the right function first, then the left to its result, calculating the standard deviation as follows.

formula

Dennis
  • 211.7k
  • 41
  • 380
  • 830

AltStyle によって変換されたページ (->オリジナル) /