math-exercises 3.0.71 → 3.0.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"median.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/probaStat/stats1var/median.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,QAAQ,EAWT,MAAM,sBAAsB,CAAC;AAG9B,KAAK,WAAW,GAAG;IACjB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,gBAAgB,EAAE,MAAM,EAAE,CAAC;CAC5B,CAAC;AA8FF,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,WAAW,CAYjD,CAAC"}
1
+ {"version":3,"file":"median.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/probaStat/stats1var/median.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,QAAQ,EAWT,MAAM,sBAAsB,CAAC;AAG9B,KAAK,WAAW,GAAG;IACjB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,gBAAgB,EAAE,MAAM,EAAE,CAAC;CAC5B,CAAC;AAmFF,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,WAAW,CAYjD,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { randint } from "../../../../math/utils/random/randint.js";
2
+ import { median } from "../../../../math/utils/stats/median.js";
2
3
  import { shuffle } from "../../../../utils/alea/shuffle.js";
3
4
  import { dollarize } from "../../../../utils/latex/dollarize.js";
4
5
  import { mdTable } from "../../../../utils/markdown/mdTable.js";
@@ -16,17 +17,7 @@ Calculer la médiane de cette série de valeurs.`;
16
17
  };
17
18
  const getAnswer = (identifiers) => {
18
19
  const { randomEffectives, randomValues } = identifiers;
19
- const n = randomEffectives.reduce((sum, value) => sum + value, 0);
20
- const middleIndex = Math.floor(n / 2);
21
- let median;
22
- if (n % 2 === 0) {
23
- median = (randomValues[middleIndex - 1] + randomValues[middleIndex]) / 2;
24
- }
25
- else {
26
- median = randomValues[middleIndex];
27
- }
28
- const answer = (median + "").replace(".", ",");
29
- return answer;
20
+ return median(randomValues, randomEffectives).frenchify();
30
21
  };
31
22
  const getMedianWithTable = () => {
32
23
  const getRandomUniqueValues = (count, min, max) => {
@@ -1 +1 @@
1
- {"version":3,"file":"medianList.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/probaStat/stats1var/medianList.ts"],"names":[],"mappings":"AACA,OAAO,EACL,QAAQ,EAYT,MAAM,sBAAsB,CAAC;AAE9B,KAAK,WAAW,GAAG;IACjB,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB,CAAC;AAuEF,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,WAAW,CAYhD,CAAC"}
1
+ {"version":3,"file":"medianList.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/probaStat/stats1var/medianList.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,QAAQ,EAYT,MAAM,sBAAsB,CAAC;AAE9B,KAAK,WAAW,GAAG;IACjB,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB,CAAC;AA+DF,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,WAAW,CAYhD,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { randint } from "../../../../math/utils/random/randint.js";
2
+ import { median } from "../../../../math/utils/stats/median.js";
2
3
  import { addValidProp, shuffleProps, tryToAddWrongProp, } from "../../../exercise.js";
3
4
  import { getDistinctQuestions } from "../../../utils/getDistinctQuestions.js";
4
5
  const getInstruction = (identifiers) => {
@@ -13,16 +14,7 @@ Calculer la médiane de cette liste de valeurs.`;
13
14
  };
14
15
  const getAnswer = (identifiers) => {
15
16
  const { sortedValues } = identifiers;
16
- const middleIndex = Math.floor(sortedValues.length / 2);
17
- let median;
18
- if (sortedValues.length % 2 === 0) {
19
- median = (sortedValues[middleIndex - 1] + sortedValues[middleIndex]) / 2;
20
- }
21
- else {
22
- median = sortedValues[middleIndex];
23
- }
24
- const answer = (median + "").replace(".", ",");
25
- return answer;
17
+ return median(sortedValues).frenchify();
26
18
  };
27
19
  const getMedianList = () => {
28
20
  let randomValues = [];
@@ -1 +1 @@
1
- {"version":3,"file":"quartilesList.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/probaStat/stats1var/quartilesList.ts"],"names":[],"mappings":"AACA,OAAO,EACL,QAAQ,EAYT,MAAM,sBAAsB,CAAC;AAG9B,KAAK,WAAW,GAAG;IACjB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAiFF,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,WAAW,CAY/C,CAAC"}
1
+ {"version":3,"file":"quartilesList.d.ts","sourceRoot":"","sources":["../../../../../src/exercises/math/probaStat/stats1var/quartilesList.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,QAAQ,EAYT,MAAM,sBAAsB,CAAC;AAG9B,KAAK,WAAW,GAAG;IACjB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAuEF,eAAO,MAAM,aAAa,EAAE,QAAQ,CAAC,WAAW,CAY/C,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { randint } from "../../../../math/utils/random/randint.js";
2
+ import { firstQuartile, thirdQuartile, } from "../../../../math/utils/stats/quartiles.js";
2
3
  import { addValidProp, shuffleProps, tryToAddWrongProp, } from "../../../exercise.js";
3
4
  import { getDistinctQuestions } from "../../../utils/getDistinctQuestions.js";
4
5
  const getInstruction = ({ randomQuartile, randomValues, }) => {
@@ -16,17 +17,10 @@ Calculer le ${quartileToString} de cette série de valeurs.`;
16
17
  };
17
18
  const getAnswer = (identifiers) => {
18
19
  const { randomValues, randomQuartile } = identifiers;
19
- const firstQuartileIndex = Math.round(randomValues.length / 4 + 0.49);
20
- const thirdQuartileIndex = Math.round((3 * randomValues.length) / 4 + 0.49);
21
- const firstQuartile = randomValues[firstQuartileIndex - 1];
22
- const thirdQuartile = randomValues[thirdQuartileIndex - 1];
23
- let choosenQuartile;
24
20
  if (randomQuartile === 0)
25
- choosenQuartile = firstQuartile;
21
+ return firstQuartile(randomValues).frenchify();
26
22
  else
27
- choosenQuartile = thirdQuartile;
28
- const answer = choosenQuartile + "";
29
- return answer;
23
+ return thirdQuartile(randomValues).frenchify();
30
24
  };
31
25
  const getQuartiles = () => {
32
26
  let randomValues = [];
@@ -0,0 +1,2 @@
1
+ export declare const median: (sortedValues: number[], effectifs?: number[]) => number;
2
+ //# sourceMappingURL=median.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"median.d.ts","sourceRoot":"","sources":["../../../../src/math/utils/stats/median.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,MAAM,iBAAkB,MAAM,EAAE,cAAc,MAAM,EAAE,WAgClE,CAAC"}
@@ -0,0 +1,39 @@
1
+ //! values must be sorted
2
+ export const median = (sortedValues, effectifs) => {
3
+ if (!sortedValues.length)
4
+ throw Error("Malformed data in median");
5
+ if (!effectifs?.length) {
6
+ const N = sortedValues.length;
7
+ if (N % 2 === 0) {
8
+ return (sortedValues[N / 2 - 1] + sortedValues[N / 2]) / 2;
9
+ }
10
+ else {
11
+ return sortedValues[Math.floor(N / 2)];
12
+ }
13
+ }
14
+ else {
15
+ const N = effectifs.reduce((acc, curr) => acc + curr, 0);
16
+ if (N % 2 === 0) {
17
+ let s = 0;
18
+ for (let i = 0; i < effectifs.length; i++) {
19
+ s += effectifs[i];
20
+ if (N / 2 < s) {
21
+ return sortedValues[i];
22
+ }
23
+ if (N / 2 === s)
24
+ return (sortedValues[i] + sortedValues[i + 1]) / 2;
25
+ }
26
+ return (sortedValues[N / 2 - 1] + sortedValues[N / 2]) / 2;
27
+ }
28
+ else {
29
+ let s = 0;
30
+ for (let i = 0; i < effectifs.length; i++) {
31
+ s += effectifs[i];
32
+ if (N / 2 <= s) {
33
+ return sortedValues[i];
34
+ }
35
+ }
36
+ }
37
+ }
38
+ return 0;
39
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"playground.d.ts","sourceRoot":"","sources":["../src/playground.ts"],"names":[],"mappings":"AAkCA,eAAO,MAAM,UAAU,YAMtB,CAAC"}
1
+ {"version":3,"file":"playground.d.ts","sourceRoot":"","sources":["../src/playground.ts"],"names":[],"mappings":"AAmCA,eAAO,MAAM,UAAU,YAMtB,CAAC"}
package/lib/playground.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Decimal } from "./math/numbers/decimals/decimal.js";
2
+ import { median } from "./math/utils/stats/median.js";
2
3
  export const playground = () => {
3
4
  const dec = new Decimal(0.0001);
4
5
  const answer = dec
5
6
  .toScientificNotation()
6
7
  .toTex({ forceNoSimplification: true });
7
- console.log(answer);
8
+ console.log(median([1, 2, 3, 4], [1, 2, 10, 2]));
8
9
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "math-exercises",
3
3
  "type": "module",
4
- "version": "3.0.71",
4
+ "version": "3.0.72",
5
5
  "description": "Math exercises generator for middle school and high school",
6
6
  "main": "lib/index.js",
7
7
  "files": [