Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
periodic-table.test.ts don't iterate over full table, do random subse…
…t for speed
- Loading branch information
Showing
2 changed files
with
31 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function random_sample<T>(input_list: T[], n_samples: number): T[] { | ||
// If the subset size is greater than the list size, return the original list | ||
if (n_samples >= input_list.length) { | ||
return input_list | ||
} | ||
|
||
// Generate a random subset | ||
const rand_sample = [] | ||
const used_indices = new Set() | ||
|
||
while (rand_sample.length < n_samples) { | ||
const rand_idx = Math.floor(Math.random() * input_list.length) | ||
if (!used_indices.has(rand_idx)) { | ||
rand_sample.push(input_list[rand_idx]) | ||
used_indices.add(rand_idx) | ||
} | ||
} | ||
|
||
return rand_sample | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters