Skip to content

Compare changes

Choose two branches to see what one has that the other does not, then open a pull request for it.

base
compare
Open a pull request

4 commits

4 files+153−10/4 viewed
+50−0
1+# Checks Docker on g1t's hosted runners: a service container reached by
2+# name, a step run in an image, a build, and a job inside a container.
3+name: Docker
4+
5+on:
6+ workflow_dispatch:
7+
8+permissions:
9+ contents: read
10+
11+jobs:
12+ services:
13+ name: Services and docker run
14+ runs-on: ubuntu-latest
15+ timeout-minutes: 15
16+ services:
17+ redis:
18+ image: redis:7-alpine
19+ options: >-
20+ --health-cmd "redis-cli ping"
21+ --health-interval 2s
22+ --health-timeout 2s
23+ --health-retries 15
24+ ports:
25+ - 6379:6379
26+ steps:
27+ - uses: actions/checkout@v5
28+ - name: Docker is there
29+ run: docker version
30+ - name: Reach the service by name
31+ run: docker run --rm redis:7-alpine redis-cli -h redis ping
32+ - name: Build an image
33+ run: |
34+ printf 'FROM alpine:3.20\nRUN echo built > /built\nCMD cat /built\n' > Dockerfile.smoke
35+ docker build -t smoke:latest -f Dockerfile.smoke .
36+ docker run --rm smoke:latest
37+ - name: A step in an image
38+ uses: docker://alpine:3.20
39+ with:
40+ args: echo hello from a docker step
41+
42+ container:
43+ name: Job in a container
44+ runs-on: ubuntu-latest
45+ timeout-minutes: 15
46+ container:
47+ image: node:24-bookworm-slim
48+ steps:
49+ - uses: actions/checkout@v5
50+ - run: node --version && cat /etc/os-release | head -2
+36−0
11 # automation lab
2+
3+## Functions
4+
5+All functions are exported from `math.js`.
6+
7+```js
8+const { add, multiply, power, mean, median, standard_deviation } = require("./math");
9+```
10+
11+- `add(a, b)`: returns the sum of `a` and `b`.
12+ ```js
13+ add(2, 3); // 5
14+ ```
15+- `multiply(a, b)`: returns the product of `a` and `b`.
16+ ```js
17+ multiply(4, 5); // 20
18+ ```
19+- `power(base, exponent)`: returns `base` raised to a whole-number `exponent`; throws a `RangeError` if `exponent` is negative.
20+ ```js
21+ power(2, 3); // 8
22+ ```
23+- `mean(numbers)`: returns the average of an array of numbers; throws a `TypeError` if the array is empty.
24+ ```js
25+ mean([1, 2, 3, 4]); // 2.5
26+ ```
27+- `median(numbers)`: returns the middle value (the average of the two middle values when the length is even); throws a `TypeError` if the array is empty.
28+ ```js
29+ median([3, 1, 2]); // 2
30+ median([1, 2, 3, 4]); // 2.5
31+ ```
32+- `standard_deviation(numbers)`: returns the population standard deviation; throws a `TypeError` if the array is empty.
33+ ```js
34+ standard_deviation([2, 4, 4, 4, 5, 5, 7, 9]); // 2
35+ ```
36+
37+Agent runs here have a filtered network: package registries and g1t only.
+51−1
99 return a * b;
1010 }
1111
12−module.exports = { add, multiply };
12+/** Returns a minus b. */
13+function subtract(a, b) {
14+ return a - b;
15+}
16+
17+/** Returns a divided by b; throws a RangeError when b is 0. */
18+function divide(a, b) {
19+ if (b === 0) {
20+ throw new RangeError("Division by zero");
21+ }
22+ return a / b;
23+}
24+
25+/** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */
26+function power(base, exponent) {
27+ if (exponent < 0) {
28+ throw new RangeError("exponent must not be negative");
29+ }
30+ let result = 1;
31+ for (let i = 0; i < exponent; i++) {
32+ result *= base;
33+ }
34+ return result;
35+}
36+
37+/** Returns the average of numbers; throws a TypeError if the array is empty. */
38+function mean(numbers) {
39+ if (numbers.length === 0) {
40+ throw new TypeError("numbers must not be empty");
41+ }
42+ return numbers.reduce((sum, n) => sum + n, 0) / numbers.length;
43+}
44+
45+/** Returns the middle value of numbers (the average of the two middle values when the length is even); throws a TypeError if the array is empty. */
46+function median(numbers) {
47+ if (numbers.length === 0) {
48+ throw new TypeError("numbers must not be empty");
49+ }
50+ const sorted = [...numbers].sort((a, b) => a - b);
51+ const mid = Math.floor(sorted.length / 2);
52+ return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
53+}
54+
55+/** Returns the population standard deviation of numbers; throws a TypeError if the array is empty. */
56+function standard_deviation(numbers) {
57+ const avg = mean(numbers);
58+ const variance = numbers.reduce((sum, n) => sum + (n - avg) ** 2, 0) / numbers.length;
59+ return Math.sqrt(variance);
60+}
61+
62+module.exports = { add, multiply, subtract, divide, power, mean, median, standard_deviation };
+16−0
11 const assert = require("node:assert");
22 const math = require("./math");
3+assert.strictEqual(math.subtract(5, 3), 2);
34
45 assert.strictEqual(math.add(2, 3), 5);
56 assert.strictEqual(math.multiply(4, 5), 20);
7+assert.strictEqual(math.divide(10, 4), 2.5);
8+assert.throws(() => math.divide(1, 0), RangeError);
9+assert.strictEqual(math.power(2, 10), 1024);
10+assert.strictEqual(math.power(5, 0), 1);
11+assert.throws(() => math.power(2, -1), RangeError);
12+assert.strictEqual(math.mean([1, 2, 3]), 2);
13+assert.strictEqual(math.mean([1, 2, 3, 4]), 2.5);
14+assert.strictEqual(math.median([1, 2, 3]), 2);
15+assert.strictEqual(math.median([3, 1, 2]), 2);
16+assert.strictEqual(math.median([4, 1, 3, 2]), 2.5);
17+assert.strictEqual(math.standard_deviation([1, 2, 3]), Math.sqrt(2 / 3));
18+assert.strictEqual(math.standard_deviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
19+for (const fn of [math.mean, math.median, math.standard_deviation]) {
20+ assert.throws(() => fn([]), TypeError);
21+}
622 console.log("all tests passed");