Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

25
LINES

< > BotCompany Repo | #1035489 // digitalVariation

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (167L/2K).

static double digitalVariation(int[] array) {
  int n = l(array);
  if (n == 0) ret 0; // ? - undefined
  if (n == 1) ret 0;
  var value = array[0];
  int deviations = 0;
  for (int i = 1; i < n; i++)
    if (array[i] != value)
      ++deviations;
  ret doubleRatio(deviations, n);
}

static <A> double digitalVariation(Iterable<A> l) {
  var it = iterator(l);
  if (!it.hasNext()) ret 0; // ? - undefined
  int n = 1;
  var value = it.next();
  int deviations = 0;
  while (it.hasNext()) {
    ++n;
    if (!eq(value, it.hasNext()))
      ++deviations;
  }
  ret doubleRatio(deviations, n);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035489
Snippet name: digitalVariation
Eternal ID of this version: #1035489/3
Text MD5: 48b71e224a024f49a4bf50f7cc68fdf1
Transpilation MD5: f9f27a2387d40cfdba88d36af9cb0b57
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-27 00:36:48
Source code size: 617 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 104 / 164
Version history: 2 change(s)
Referenced in: [show references]