Find the C-factor of a vote
R, 23 bytes
function(o,v)mean(o!=v)
Try it online!
The challenge boils down to computing the proportion of values in v
different from o
(i.e. mean(xor(o,v))
). We can therefore avoid using abs
.
Jelly, 3 bytes
ạÆm
Try it online!
Literally just "absolute difference to mean".
ạÆm Main link
ạ Absolute difference
Æm Arithmetic Mean
If you invert the arguments you can invert the atoms.
APL (Dyalog Unicode), 9 8 5 bytes
≠⌹⊢=⊢
Try it online!
Anonymous train. Thanks to @Adám for a byte saved, and thanks to @ngn for 3 bytes!
How:
≠⌹⊢=⊢ ⍝ Anonymous Train
⊢ ⍝ The right argument (⍵)
⊢= ⍝ Equals itself. Generates an array of 1s
≠ ⍝ XOR left (⍺) and right args; generates ⍵ or (not ⍵), depending on ⍺.
⌹ ⍝ Divide these matrices.