-
-
Notifications
You must be signed in to change notification settings - Fork 639
Add Concept Exercise for 'randomness': Captain's Log #2683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Stub for new Concept exercise
Still need an uuid, no idea how to generate one
Added stub file's comments for task exercism#1
Added stub file's comments for task exercism#3
Hello. Thanks for opening a PR on Exercism 🙂 We ask that all changes to Exercism are discussed on our Community Forum before being opened on GitHub. To enforce this, we automatically close all PRs that are submitted. That doesn't mean your PR is rejected but that we want the initial discussion about it to happen on our forum where a wide range of key contributors across the Exercism ecosystem can weigh in. You can use this link to copy this into a new topic on the forum. If we decide the PR is appropriate, we'll reopen it and continue with it, so please don't delete your local branch. If you're interested in learning more about this auto-responder, please read this blog post. Note: If this PR has been pre-approved, please link back to this PR on the forum thread and a maintainer or staff member will reopen it. |
If you've used configlet, which is our tool for managing tracks and things like generating exercices, then all the needed files should be auto-generated and you shouldn't need to manually attach a UUID, but in case you do configlet can also do that. Also worth checking out our (slightly outdated) CONTRIBUTING guide. |
test('registry numbers are valid',() => { | ||
expect(randomShipRegistryNumber().test(/NCC-[1-9][0-9]{3}/)).toBe(true); | ||
expect(randomShipRegistryNumber().test(/NCC-[1-9][0-9]{3}/)).toBe(true); | ||
expect(randomShipRegistryNumber().test(/NCC-[1-9][0-9]{3}/)).toBe(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably want to use toMatch here.
test('stardates are valid',() => { | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have liked to see repeated testing like this in a for
loop or something similar, so you can avoid repetition and we can test an arbitrary amount of times, because randomness is tricky.
Additionally, this - (41000<=randomStardate()<42000)
is not valid JS syntax (technically it is but it doesn't work like in Python), because chained comparison isn't supported like that. You'll need something like (41000 <= stardate && stardate < 42000)
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will work:
function loadDie(...values) {
const originalRandom = Math.random()
Math.random = function loadedDie {
if (values.length === 0) {
return originalRandom();
}
return values.shift();
}
return () => {
Math.random = originalRandom;
}
}
describe('randomStardate',() => {
test('stardate is between 41000 and 42000',() => {
const restore = loadDie(
0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5
);
// 6 * 0, 6 * 1, 6 * 0.5 and everything else random
for (let i = 0; i < 10_000; i++) {
const starDate = randomStardate()
expect(starDate).toBeGreaterThanOrEqual(41_000);
expect(starDate).toBeLessThan(42_000);
}
restore();
}
}
This should run in ~ 0.1 ms
Errors in tests fixed, and for loops now used. Just a question - is there a toInclude method? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I wrote an entire thesis on how to calculate the expected number of rolls to get all the values in the planet class thing, but when I tried to submit it I pressed back accidentally and then lost it all. Will write it again soon...)
test('stardates are valid',() => { | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); | ||
expect(41000<=randomStardate()<42000).toBe(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will work:
function loadDie(...values) {
const originalRandom = Math.random()
Math.random = function loadedDie {
if (values.length === 0) {
return originalRandom();
}
return values.shift();
}
return () => {
Math.random = originalRandom;
}
}
describe('randomStardate',() => {
test('stardate is between 41000 and 42000',() => {
const restore = loadDie(
0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5
);
// 6 * 0, 6 * 1, 6 * 0.5 and everything else random
for (let i = 0; i < 10_000; i++) {
const starDate = randomStardate()
expect(starDate).toBeGreaterThanOrEqual(41_000);
expect(starDate).toBeLessThan(42_000);
}
restore();
}
}
This should run in ~ 0.1 ms
|
test('planet classes are valid', () => { | ||
const planetClasses = ['D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y']; | ||
for (let i=0;i<4; i++){ | ||
expect(planetClasses).toContain(randomPlanetClass()) | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. So I did this again.
In order to find out how many rolls we need to see each planet class in the following string:
'DHJKLMNRTY'
...we need to make a die that's fair with each side one class. There are 10 letters in the string. This math answer for a 6-sided die pointed me to terms to find this stats answer. We are looking for the 10 * harmonic(10)
which is just shy of 30.
However, that gets us the average and we need to find out a reasonable number to use as maximum or at least a high probability (>95%, preferably >99.5%).
So I made this simulation in R.
Then I ran it 5 times:
29.2
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.0 21.0 27.0 29.2 35.0 118.0
29.2544
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.00 21.00 27.00 29.25 35.00 131.00
29.528
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.00 21.00 27.00 29.53 35.00 118.00
29.0984
Min. 1st Qu. Median Mean 3rd Qu. Max.
11.0 21.0 27.0 29.1 34.0 100.0
29.53
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.00 21.00 27.00 29.53 35.00 93.00
This tells me that the average number is spot on, and that we can use 200 rolls to get all the values, but to make it completely improbable that a correct solution yields an incomplete set, we can do it 1_000
times.
describe('randomPlanetClass', () => {
test('planet classes are valid', () => {
const expected = 'DHJKLMNRTY'
const seen = {}
for (let i=0; i < 1_000; i++){
const actual = randomPlanetClass()
expect(expected).toContain(actual)
}
})
test('all planet classes can be returned', () => {
const expected = 'DHJKLMNRTY'
const seen = {}
for (let i=0; i < 1_000; i++){
const actual = randomPlanetClass()
seen[actual] = true
}
expected(Object.keys(seen).length).toBe(expected.length);
})
// remove the other test, it's no longer necessary
})
Tests updated and uuid added. |
Added captains-log to config.json
@quintuple-mallard You can run |
I am trying this, but it is saying 'missing file'. Should I change the directory? |
It's explained in more detail in the contributing guide that I linked earlier, but it would look something like this (when ran from root)
|
How do I get to the root directory? I am quite new to CLIs and configlet |
This PR adds
hints.md
,introduction.md
,instructions.md
,config.json
,design.md
,exemplar.js
,captains-log.js
, andcaptains-log.spec.js
to a new exercise on the JavaScript track.A couple of questions to a reviewer:
.gitignore, .npmrc, LICENSE, package.json, jest.config.js, global.d.ts, eslint.config.mjs, and babel.config.js. These are not mentioned in the docs though. Should I add these, or does that happen automatically somehow?