Skip to content
Snippets Groups Projects
Commit c98d751d authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 1073 additions and 0 deletions
# Disable line-ending conversions for this repository.
* -text
# dependencies
/node_modules
# testing
/coverage
# production
/build
# environments
.env.local
.env.development.local
.env.test.local
.env.production.local
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# misc
*~
.DS_Store
[submodule "eslint-config"]
path = eslint-config
url = git@git.unl.edu:csce-310/eslint-config.git
# Tests-Only Starter Code
A minimal project to be used as starter code for Homework 2 in the 250 section
of the CSCE 310 course at UNL.
# Quick Start
Recursively clone this repository and `cd` into the root folder:
```
$ git clone --recursive git@git.unl.edu:csce-310/2021-fall-homework-2.git
$ cd 2021-fall-homework-2
```
(If you forget `--recursive` when cloning, you can `cd` into your clone and run
`git submodule update --init --recursive` instead.)
Install dependencies:
```
$ npm install
```
# Instructions
See <https://canvas.unl.edu/courses/114253/assignments/1093847>.
Subproject commit 24df42fb655d234b83c93b0fb24d012e4d9ecb58
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.eol": "LF",
"files.exclude": {
"**/node_modules": true
},
"files.trimFinalNewlines": true,
"files.trimTrailingWhitespace": true
}
}
# dependencies
/node_modules
# testing
/coverage
# production
/build
# environments
.env.local
.env.development.local
.env.test.local
.env.production.local
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# misc
*~
.DS_Store
This diff is collapsed.
{
"name": "@unlsoft/homework-2-implementation",
"version": "1.0.0",
"description": "A minimal project to be used as starter code for Homework 2.",
"type": "module",
"private": true,
"license": "UNLICENSED",
"scripts": {
"lint:js": "eslint --max-warnings 0 ./src",
"lint": "run-s --continue-on-error lint:**",
"test-once:closestPair": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage -t 'findClosestPair'",
"test-once:sumToN": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage -t 'findElementsSummingTo'",
"test-once:intervals": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage -t 'findChromaticNumber'",
"test-once:trains": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage -t 'planJourney'",
"test-once": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watchAll --coverage"
},
"dependencies": {
"npm-run-all": "^4.1.5"
},
"devDependencies": {
"@unlsoft/eslint-config": "file:../eslint-config",
"eslint": "^7.30.0",
"jest": "^27.0.6",
"jest-environment-node": "^27.0.6"
},
"eslintConfig": {
"extends": "@unlsoft"
},
"jest": {
"clearMocks": true,
"collectCoverageFrom": [
"src/**/*.js",
"!src/testing/**/*.js"
],
"resetMocks": false,
"restoreMocks": false,
"testEnvironment": "./src/testing/failFast.js",
"transform": {}
},
"//": [
"See https://github.com/facebook/jest/issues/9430 for information on Jest+ES6."
]
}
export class BasicOperationObserver {
constructor(array) {
this.count = 0;
for (const index of array.keys()) {
const value = array[index];
Object.defineProperty(array, index, {
get: () => {
++this.count;
return value;
},
enumerable: true,
configurable: true,
});
}
}
}
export function findClosestPair(list) {
// INSTRUCTIONS: For the functional correctness points, complete this
// JavaScript function so that it finds the minimum difference between two
// distinct elements of `list`. You may assume that the input `list` is
// sorted.
//
// For the performance points, modify the search so that it runs in linear
// time. Consider that, while a straightforward exhaustive search will need
// Θ(n²) time, your generate step does not need to consider all possible pairs
// because the list is presorted.
}
/* eslint-disable no-magic-numbers */
import { BasicOperationObserver } from './basicOperationCounting.js';
import { findClosestPair } from './closestPair.js';
describe('the findClosestPair function', () => {
test('finds a shortest distance of infinity in an empty list', () => {
expect(findClosestPair([])).toBe(Infinity);
});
test('finds a shortest distance of infinity in a one-element list', () => {
expect(findClosestPair([464])).toBe(Infinity);
});
test('finds a shortest distance of 146 in sample list 0', () => {
expect(findClosestPair([150, 296])).toBe(146);
});
test('finds a shortest distance of 3 in sample list 1', () => {
expect(findClosestPair([500, 938, 941])).toBe(3);
});
test('finds a shortest distance of 141 in sample list 2', () => {
expect(findClosestPair([48, 342, 483, 736])).toBe(141);
});
test('finds a shortest distance of 95 in sample list 3', () => {
expect(findClosestPair([118, 346, 486, 581, 686])).toBe(95);
});
test('finds a shortest distance of 19 in sample list 4', () => {
expect(findClosestPair([288, 477, 547, 662, 681, 759])).toBe(19);
});
test('finds a shortest distance of 13 in sample list 5', () => {
expect(findClosestPair([90, 321, 429, 442, 644, 739, 772])).toBe(13);
});
test('finds a shortest distance of 6 in sample list 6', () => {
expect(findClosestPair([42, 192, 291, 438, 563, 703, 784, 790])).toBe(6);
});
test('finds a shortest distance of 6 in sample list 7', () => {
expect(findClosestPair([176, 256, 262, 358, 457, 560, 572, 698, 918])).toBe(6);
});
test('finds a shortest distance of 7 in sample list 8', () => {
expect(findClosestPair([65, 117, 392, 458, 593, 600, 729, 760, 915, 933])).toBe(7);
});
test('finds a shortest distance of 49 in sample list 9', () => {
expect(findClosestPair([82, 143, 296, 397, 483, 576, 625, 715, 781, 993])).toBe(49);
});
test('finds a shortest distance of 4 in sample list 10', () => {
expect(findClosestPair([10, 170, 174, 192, 343, 366, 387, 462, 647, 728])).toBe(4);
});
test('finds a shortest distance of 8 in sample list 11', () => {
expect(findClosestPair([13, 169, 276, 326, 366, 398, 456, 493, 501, 908])).toBe(8);
});
test('finds a shortest distance of 10 in sample list 12', () => {
expect(findClosestPair([68, 481, 504, 581, 708, 718, 741, 755, 843, 962])).toBe(10);
});
test('finds a shortest distance of 10 in sample list 13', () => {
expect(findClosestPair([44, 200, 348, 443, 453, 588, 670, 681, 698, 973])).toBe(10);
});
test('finds a shortest distance of 5 in sample list 14', () => {
expect(findClosestPair([142, 209, 343, 411, 483, 499, 566, 571, 931, 986])).toBe(5);
});
test('finds a shortest distance of 3 in sample list 15', () => {
expect(findClosestPair([74, 109, 112, 215, 501, 550, 698, 791, 850, 904])).toBe(3);
});
test('finds a shortest distance of 4 in sample list 16', () => {
expect(findClosestPair([109, 113, 156, 365, 414, 422, 473, 552, 556, 855])).toBe(4);
});
test('finds a shortest distance of 9 in sample list 17', () => {
expect(findClosestPair([197, 227, 377, 406, 479, 516, 538, 663, 979, 988])).toBe(9);
});
test('finds a shortest distance of 8 in sample list 18', () => {
expect(findClosestPair([2, 80, 337, 383, 391, 595, 682, 835, 861, 918])).toBe(8);
});
test('finds a shortest distance of 7 in sample list 19', () => {
expect(findClosestPair([82, 90, 130, 254, 428, 477, 492, 953, 960, 977])).toBe(7);
});
test('finds a shortest distance of 23 in sample list 20', () => {
expect(findClosestPair([149, 199, 279, 302, 342, 414, 504, 800, 936, 996])).toBe(23);
});
test('finds a shortest distance of 23 in sample list 21', () => {
expect(findClosestPair([82, 116, 150, 283, 329, 352, 517, 578, 654, 935])).toBe(23);
});
test('finds a shortest distance of 4 in sample list 22', () => {
expect(findClosestPair([62, 103, 237, 244, 267, 383, 533, 590, 594, 693])).toBe(4);
});
test('finds a shortest distance of 11 in sample list 23', () => {
expect(findClosestPair([8, 115, 166, 240, 283, 662, 827, 923, 934, 971])).toBe(11);
});
test('finds a shortest distance of 3 in sample list 24', () => {
expect(findClosestPair([19, 178, 193, 293, 457, 460, 536, 745, 827, 977])).toBe(3);
});
test('finds a shortest distance of 14 in sample list 25', () => {
expect(findClosestPair([39, 211, 380, 581, 725, 807, 821, 925, 952, 991])).toBe(14);
});
test('finds a shortest distance of 10 in sample list 26', () => {
expect(findClosestPair([83, 302, 529, 545, 583, 777, 875, 886, 966, 976])).toBe(10);
});
test('finds a shortest distance of 5 in sample list 27', () => {
expect(findClosestPair([35, 189, 279, 392, 397, 407, 478, 550, 923, 981])).toBe(5);
});
test('finds a shortest distance of 19 in sample list 28', () => {
expect(findClosestPair([10, 275, 294, 376, 413, 604, 687, 753, 815, 995])).toBe(19);
});
test('finds a shortest distance of 27 in sample list 29', () => {
expect(findClosestPair([47, 74, 150, 198, 727, 783, 814, 864, 941, 979])).toBe(27);
});
test('finds a shortest distance of 2 in sample list 30', () => {
expect(findClosestPair([121, 183, 185, 251, 363, 436, 675, 773, 817, 898])).toBe(2);
});
test('finds a shortest distance of 26 in sample list 31', () => {
expect(findClosestPair([63, 89, 119, 287, 348, 394, 420, 529, 606, 719])).toBe(26);
});
test('finds a shortest distance of 0 in sample list 32', () => {
expect(findClosestPair([241, 241, 318, 364, 416, 491, 523, 614, 742, 907])).toBe(0);
});
test('finds a shortest distance of 29 in sample list 33', () => {
expect(findClosestPair([101, 260, 452, 490, 610, 694, 773, 874, 903, 935])).toBe(29);
});
test('finds a shortest distance of 3 in sample list 34', () => {
expect(findClosestPair([49, 334, 374, 384, 573, 629, 691, 694, 764, 935])).toBe(3);
});
test('finds a shortest distance of 6 in sample list 35', () => {
expect(findClosestPair([61, 180, 244, 250, 516, 634, 795, 807, 828, 848])).toBe(6);
});
test('finds a shortest distance of 7 in sample list 36', () => {
expect(findClosestPair([95, 303, 461, 468, 484, 539, 693, 732, 830, 944])).toBe(7);
});
test('finds a shortest distance of 8 in sample list 37', () => {
expect(findClosestPair([21, 236, 254, 295, 356, 380, 489, 895, 963, 971])).toBe(8);
});
test('finds a shortest distance of 4 in sample list 38', () => {
expect(findClosestPair([46, 50, 178, 347, 368, 497, 661, 689, 695, 958])).toBe(4);
});
test('finds a shortest distance of 5 in sample list 39', () => {
expect(findClosestPair([1, 28, 409, 477, 482, 552, 561, 605, 687, 711])).toBe(5);
});
test('finds a shortest distance of 1 in sample list 40', () => {
expect(findClosestPair([492, 639, 658, 683, 684, 718, 821, 878, 946, 973])).toBe(1);
});
test('finds a shortest distance of 13 in sample list 41', () => {
expect(findClosestPair([2, 16, 41, 146, 163, 267, 280, 402, 539, 670])).toBe(13);
});
test('finds a shortest distance of 3 in sample list 42', () => {
expect(findClosestPair([29, 236, 495, 505, 648, 711, 737, 740, 777, 903])).toBe(3);
});
test('finds a shortest distance of 5 in sample list 43', () => {
expect(findClosestPair([19, 125, 295, 408, 480, 599, 825, 931, 979, 984])).toBe(5);
});
test('finds a shortest distance of 4 in sample list 44', () => {
expect(findClosestPair([147, 197, 203, 286, 502, 506, 657, 719, 780, 802])).toBe(4);
});
test('finds a shortest distance of 1 in sample list 45', () => {
expect(findClosestPair([26, 74, 164, 183, 243, 267, 268, 380, 412, 889])).toBe(1);
});
test('finds a shortest distance of 2 in sample list 46', () => {
expect(findClosestPair([16, 360, 624, 635, 651, 790, 880, 883, 885, 947])).toBe(2);
});
test('finds a shortest distance of 6 in sample list 47', () => {
expect(findClosestPair([96, 176, 277, 338, 364, 436, 442, 689, 810, 979])).toBe(6);
});
test('finds a shortest distance of 8 in sample list 48', () => {
expect(findClosestPair([117, 191, 209, 258, 324, 855, 882, 890, 986, 998])).toBe(8);
});
test('finds a shortest distance of 3 in sample list 49', () => {
expect(findClosestPair([145, 148, 234, 263, 350, 380, 448, 527, 702, 938])).toBe(3);
});
test('finds a shortest distance of 5 in sample list 50', () => {
expect(findClosestPair([7, 87, 108, 165, 379, 413, 486, 822, 827, 860])).toBe(5);
});
test('finds a shortest distance of 1 in sample list 51', () => {
expect(findClosestPair([104, 353, 511, 620, 635, 665, 684, 848, 921, 922])).toBe(1);
});
test('finds a shortest distance of 19 in sample list 52', () => {
expect(findClosestPair([54, 239, 285, 372, 514, 608, 627, 771, 816, 925])).toBe(19);
});
test('finds a shortest distance of 21 in sample list 53', () => {
expect(findClosestPair([113, 326, 380, 415, 436, 611, 691, 750, 782, 833])).toBe(21);
});
test('finds a shortest distance of 5 in sample list 54', () => {
expect(findClosestPair([138, 186, 205, 245, 273, 320, 325, 662, 778, 845])).toBe(5);
});
test('finds a shortest distance of 1 in sample list 55', () => {
expect(findClosestPair([59, 222, 361, 405, 445, 534, 539, 818, 819, 836])).toBe(1);
});
test('finds a shortest distance of 6 in sample list 56', () => {
expect(findClosestPair([17, 40, 103, 132, 138, 325, 599, 638, 816, 883])).toBe(6);
});
test('finds a shortest distance of 4 in sample list 57', () => {
expect(findClosestPair([118, 230, 300, 418, 452, 674, 698, 702, 710, 725])).toBe(4);
});
test('finds a shortest distance of 5 in sample list 58', () => {
expect(findClosestPair([231, 246, 514, 519, 658, 693, 729, 780, 793, 850])).toBe(5);
});
test('finds a shortest distance of 4 in sample list 59', () => {
expect(findClosestPair([84, 97, 101, 261, 391, 486, 555, 642, 697, 795])).toBe(4);
});
test('finds a shortest distance of 7 in sample list 60', () => {
expect(findClosestPair([161, 187, 348, 355, 550, 735, 750, 812, 902, 968])).toBe(7);
});
test('finds a shortest distance of 7 in sample list 61', () => {
expect(findClosestPair([177, 188, 201, 382, 530, 537, 689, 747, 859, 887])).toBe(7);
});
test('finds a shortest distance of 2 in sample list 62', () => {
expect(findClosestPair([115, 551, 621, 677, 679, 792, 800, 802, 962, 997])).toBe(2);
});
test('finds a shortest distance of 33 in sample list 63', () => {
expect(findClosestPair([77, 134, 167, 294, 351, 425, 536, 630, 711, 823])).toBe(33);
});
test('finds a shortest distance of 17 in sample list 64', () => {
expect(findClosestPair([15, 32, 215, 419, 580, 685, 720, 829, 865, 932])).toBe(17);
});
test('finds a shortest distance of 13 in sample list 65', () => {
expect(findClosestPair([162, 358, 416, 433, 510, 534, 560, 688, 701, 809])).toBe(13);
});
test('finds a shortest distance of 15 in sample list 66', () => {
expect(findClosestPair([109, 142, 263, 465, 537, 552, 648, 725, 785, 965])).toBe(15);
});
test('finds a shortest distance of 22 in sample list 67', () => {
expect(findClosestPair([168, 267, 371, 424, 546, 646, 672, 694, 765, 920])).toBe(22);
});
test('finds a shortest distance of 16 in sample list 68', () => {
expect(findClosestPair([119, 135, 226, 256, 313, 399, 549, 907, 935, 974])).toBe(16);
});
test('finds a shortest distance of 7 in sample list 69', () => {
expect(findClosestPair([0, 106, 113, 397, 405, 594, 624, 719, 881, 916])).toBe(7);
});
test('finds a shortest distance of 0 in sample list 70', () => {
expect(findClosestPair([25, 145, 215, 343, 400, 588, 588, 591, 667, 815])).toBe(0);
});
test('finds a shortest distance of 2 in sample list 71', () => {
expect(findClosestPair([27, 129, 283, 536, 577, 800, 802, 880, 903, 920])).toBe(2);
});
test('finds a shortest distance of 2 in sample list 72', () => {
expect(findClosestPair([8, 132, 134, 254, 298, 496, 574, 614, 717, 829])).toBe(2);
});
test('finds a shortest distance of 3 in sample list 73', () => {
expect(findClosestPair([70, 191, 296, 385, 388, 504, 534, 768, 861, 873])).toBe(3);
});
test('finds a shortest distance of 9 in sample list 74', () => {
expect(findClosestPair([144, 287, 335, 475, 484, 565, 645, 794, 885, 999])).toBe(9);
});
test('finds a shortest distance of 18 in sample list 75', () => {
expect(findClosestPair([33, 66, 164, 187, 234, 440, 484, 502, 631, 900])).toBe(18);
});
test('finds a shortest distance of 19 in sample list 76', () => {
expect(findClosestPair([66, 302, 430, 477, 596, 707, 726, 807, 919, 953])).toBe(19);
});
test('finds a shortest distance of 38 in sample list 77', () => {
expect(findClosestPair([202, 292, 370, 409, 464, 600, 660, 698, 737, 887])).toBe(38);
});
test('finds a shortest distance of 6 in sample list 78', () => {
expect(findClosestPair([55, 61, 111, 133, 218, 512, 677, 786, 901, 955])).toBe(6);
});
test('finds a shortest distance of 12 in sample list 79', () => {
expect(findClosestPair([18, 137, 149, 178, 247, 285, 607, 799, 823, 853])).toBe(12);
});
test('finds a shortest distance of 10 in sample list 80', () => {
expect(findClosestPair([91, 131, 141, 485, 519, 564, 616, 632, 810, 994])).toBe(10);
});
test('finds a shortest distance of 3 in sample list 81', () => {
expect(findClosestPair([94, 208, 291, 388, 399, 415, 822, 867, 870, 902])).toBe(3);
});
test('finds a shortest distance of 1 in sample list 82', () => {
expect(findClosestPair([282, 401, 402, 725, 776, 792, 867, 883, 960, 967])).toBe(1);
});
test('finds a shortest distance of 1 in sample list 83', () => {
expect(findClosestPair([72, 276, 277, 481, 501, 526, 595, 768, 900, 950])).toBe(1);
});
test('finds a shortest distance of 14 in sample list 84', () => {
expect(findClosestPair([79, 370, 415, 452, 588, 630, 788, 906, 920, 947])).toBe(14);
});
test('finds a shortest distance of 26 in sample list 85', () => {
expect(findClosestPair([72, 133, 159, 428, 518, 672, 756, 803, 926, 954])).toBe(26);
});
test('finds a shortest distance of 10 in sample list 86', () => {
expect(findClosestPair([5, 36, 48, 184, 194, 308, 413, 454, 464, 930])).toBe(10);
});
test('finds a shortest distance of 4 in sample list 87', () => {
expect(findClosestPair([141, 157, 302, 411, 466, 635, 708, 956, 960, 979])).toBe(4);
});
test('finds a shortest distance of 5 in sample list 88', () => {
expect(findClosestPair([137, 224, 229, 321, 371, 478, 489, 525, 661, 728])).toBe(5);
});
test('finds a shortest distance of 7 in sample list 89', () => {
expect(findClosestPair([2, 19, 261, 376, 404, 468, 676, 683, 801, 809])).toBe(7);
});
test('finds a shortest distance of 8 in sample list 90', () => {
expect(findClosestPair([149, 242, 445, 465, 479, 613, 621, 672, 934, 966])).toBe(8);
});
test('finds a shortest distance of 3 in sample list 91', () => {
expect(findClosestPair([287, 290, 309, 537, 571, 716, 721, 779, 871, 986])).toBe(3);
});
test('finds a shortest distance of 1 in sample list 92', () => {
expect(findClosestPair([38, 169, 203, 211, 263, 406, 789, 885, 936, 937])).toBe(1);
});
test('finds a shortest distance of 18 in sample list 93', () => {
expect(findClosestPair([84, 173, 198, 275, 359, 444, 563, 581, 935, 978])).toBe(18);
});
test('finds a shortest distance of 1 in sample list 94', () => {
expect(findClosestPair([73, 208, 297, 317, 545, 559, 626, 744, 892, 893])).toBe(1);
});
test('finds a shortest distance of 6 in sample list 95', () => {
expect(findClosestPair([7, 320, 388, 394, 529, 595, 722, 730, 819, 921])).toBe(6);
});
test('finds a shortest distance of 1 in sample list 96', () => {
expect(findClosestPair([31, 172, 179, 469, 498, 519, 802, 845, 961, 962])).toBe(1);
});
test('finds a shortest distance of 35 in sample list 97', () => {
expect(findClosestPair([9, 160, 208, 413, 494, 812, 849, 891, 926, 963])).toBe(35);
});
test('finds a shortest distance of 4 in sample list 98', () => {
expect(findClosestPair([42, 123, 369, 373, 429, 461, 672, 689, 700, 761])).toBe(4);
});
test('finds a shortest distance of 8 in sample list 99', () => {
expect(findClosestPair([271, 484, 501, 685, 786, 845, 853, 866, 946, 983])).toBe(8);
});
test('needs fewer than 2.5n array accesses on a small list', () => {
const list = [122, 152, 423, 464, 497, 589, 596, 714, 806, 888];
const basicOperations = new BasicOperationObserver(list);
findClosestPair(list);
expect(basicOperations.count).toBeLessThan(25);
});
test('needs fewer than 2.5n array accesses on a medium list', () => {
const list = [25, 34, 52, 66, 130, 159, 173, 215, 240, 286, 350, 384, 421, 440, 492, 647, 719, 765, 792, 919];
const basicOperations = new BasicOperationObserver(list);
findClosestPair(list);
expect(basicOperations.count).toBeLessThan(50);
});
test('needs fewer than 2.5n array accesses on a large list', () => {
const list = [
12, 49, 106, 206, 277, 287, 340, 356, 369, 447, 485, 489, 489, 492, 560,
598, 606, 698, 727, 733, 780, 816, 848, 906, 911, 912, 921, 928, 940, 976,
];
const basicOperations = new BasicOperationObserver(list);
findClosestPair(list);
expect(basicOperations.count).toBeLessThan(75);
});
});
export class PriorityQueue {
constructor() {
this._vertices = [];
}
get size() {
return this._vertices.length;
}
insert(element, measure) {
let index = this._vertices.length;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
const parent = this._vertices[parentIndex];
if (parent.measure < measure) {
break;
}
this._vertices[index] = parent;
index = parentIndex;
}
this._vertices[index] = {
element,
measure,
};
}
remove() {
console.assert(this._vertices.length > 0, 'Cannot remove an element from an empty priority queue');
const result = this._vertices[0].element;
const vertex = this._vertices[this._vertices.length - 1];
for (let index = 0; ;) {
this._vertices[index] = vertex;
let swapIndex = index;
for (const candidateIndex of [2 * index + 1, 2 * index + 2]) {
if (candidateIndex < this._vertices.length - 1 &&
this._vertices[candidateIndex].measure < this._vertices[swapIndex].measure) {
swapIndex = candidateIndex;
}
}
if (swapIndex === index) {
this._vertices[index] = vertex;
this._vertices.pop();
return result;
}
this._vertices[index] = this._vertices[swapIndex];
index = swapIndex;
}
}
}
import { PriorityQueue } from './collections.js';
class Edge {
constructor(from, action, to, totalTime) {
this.from = from;
this.action = action;
this.to = to;
this.totalTime = totalTime;
}
}
export function dijkstras(graph, source, isDestination) {
const backpointers = new Map();
const worklist = new PriorityQueue();
worklist.insert(new Edge(undefined, undefined, source, 0), 0);
while (worklist.size > 0) {
const workitem = worklist.remove();
if (backpointers.has(workitem.to)) {
continue;
}
backpointers.set(workitem.to, workitem);
if (isDestination(workitem.to)) {
const reversedPath = [];
for (let current = workitem; current.from !== undefined; current = backpointers.get(current.from)) {
reversedPath.push(current.action);
}
return reversedPath.reverse();
}
for (const action of graph.getPossibleActionsFrom(workitem.to)) {
const newTotalTime = workitem.totalTime + action.timeTaken;
worklist.insert(new Edge(workitem.to, action, action.newSituation, newTotalTime), newTotalTime);
}
}
return undefined;
}
export class OpenInterval {
constructor(low, high) {
this.low = low;
this.high = high;
this.color = undefined;
}
intersects(otherInterval) {
return this.low < otherInterval.high && otherInterval.low < this.high;
}
}
export function findChromaticNumber(intervals) {
// INSTRUCTIONS: For the functional correctness points, complete this
// JavaScript function so that it finds the chromatic number of a collection
// of intervals—the minimum number of colors needed to give every interval a
// color while also ensuring that each interval has a different color than any
// intervals it intersects with. You may assume that the input list
// `intervals` initially contains only uncolored `OpenInterval` objects and
// that it is sorted so that the intervals' `low` points are in increasing
// order.
//
// As a hint, you can compute the chromatic number by actually coloring the
// intervals and then counting how many colors you needed. Colors in
// graph-coloring problems are typically represented by natural numbers, and a
// greedy coloring is optimal for sorted intervals: you can color each
// interval in order by looking at the colors of all of the already-colored
// intervals it intersects and then finding the smallest natural number that
// is not one of those numbers.
//
// For example, given `[new OpenInterval(0, 1), new OpenInterval(0, 3), new
// OpenInterval(2, 3)]`, the first interval can be colored with color 0, the
// second interval (which intersects with the first) can be colored with color
// 1, and then the third interval (with intersects with the second but not the
// first) can be colored with color 0. Two colors were used in total, so the
// chromatic number is two.
//
// For the design points, identify the three exhaustive searches in your code
// and move them each to their own function written above this one. If you
// identify the searches correctly, the only things left here should be an
// assignment in a loop, a return statement, and calls to those functions.
}
This diff is collapsed.
export function findElementsSummingTo(list, sum) {
// INSTRUCTIONS: For the functional correctness points, complete this
// JavaScript function so that it finds the two elements of `list` whose total
// is `sum` and returns those two elements in a sorted list. You may assume
// that the input `list` is sorted and that there is always exactly one pair
// of (not necessarily distinct) elements with the correct total.
//
// For the performance points, modify the search so that it runs in linear
// time. Consider that, while a straightforward exhaustive search will need
// Θ(n²) time, your generate step does not need to consider all possible pairs
// because the list is presorted. Instead, you can start with the elements at
// the first and last indices in the list and then, if the check step fails,
// adjust the indices you are looking at for the next iteration based on how
// the check step failed.
}
/* eslint-disable no-magic-numbers */
import { BasicOperationObserver } from './basicOperationCounting.js';
import { findElementsSummingTo } from './sumToN.js';
describe('the findElementsSummingTo function', () => {
test('finds two elements in sample list 0 that sum to 64', () => {
expect(findElementsSummingTo([32], 64)).toEqual([32, 32]);
});
test('finds two elements in sample list 1 that sum to 40', () => {
expect(findElementsSummingTo([16, 24], 40)).toEqual([16, 24]);
});
test('finds two elements in sample list 2 that sum to 155', () => {
expect(findElementsSummingTo([22, 61, 94], 155)).toEqual([61, 94]);
});
test('finds two elements in sample list 3 that sum to 53', () => {
expect(findElementsSummingTo([7, 10, 46, 80], 53)).toEqual([7, 46]);
});
test('finds two elements in sample list 4 that sum to 181', () => {
expect(findElementsSummingTo([1, 56, 58, 84, 97], 181)).toEqual([84, 97]);
});
test('finds two elements in sample list 5 that sum to 135', () => {
expect(findElementsSummingTo([61, 62, 74, 76, 81, 87], 135)).toEqual([61, 74]);
});
test('finds two elements in sample list 6 that sum to 53', () => {
expect(findElementsSummingTo([12, 22, 31, 44, 75, 85, 95], 53)).toEqual([22, 31]);
});
test('finds two elements in sample list 7 that sum to 108', () => {
expect(findElementsSummingTo([0, 19, 29, 32, 47, 51, 76, 97], 108)).toEqual([32, 76]);
});
test('finds two elements in sample list 8 that sum to 134', () => {
expect(findElementsSummingTo([28, 28, 32, 33, 46, 49, 59, 75, 81], 134)).toEqual([59, 75]);
});
test('finds two elements in sample list 9 that sum to 175', () => {
expect(findElementsSummingTo([6, 35, 51, 62, 82, 83, 87, 92, 95, 98], 175)).toEqual([83, 92]);
});
test('finds two elements in sample list 10 that sum to 33', () => {
expect(findElementsSummingTo([7, 12, 19, 21, 34, 41, 48, 61, 64, 82], 33)).toEqual([12, 21]);
});
test('finds two elements in sample list 11 that sum to 160', () => {
expect(findElementsSummingTo([8, 14, 23, 23, 48, 78, 82, 85, 92, 96], 160)).toEqual([78, 82]);
});
test('finds two elements in sample list 12 that sum to 37', () => {
expect(findElementsSummingTo([7, 16, 21, 37, 39, 42, 52, 65, 75, 84], 37)).toEqual([16, 21]);
});
test('finds two elements in sample list 13 that sum to 91', () => {
expect(findElementsSummingTo([12, 24, 31, 64, 66, 67, 70, 70, 83, 85], 91)).toEqual([24, 67]);
});
test('finds two elements in sample list 14 that sum to 55', () => {
expect(findElementsSummingTo([2, 14, 23, 28, 29, 53, 55, 84, 89, 92], 55)).toEqual([2, 53]);
});
test('finds two elements in sample list 15 that sum to 132', () => {
expect(findElementsSummingTo([34, 35, 44, 57, 63, 65, 79, 81, 86, 98], 132)).toEqual([34, 98]);
});
test('finds two elements in sample list 16 that sum to 143', () => {
expect(findElementsSummingTo([7, 14, 14, 19, 34, 37, 41, 47, 78, 96], 143)).toEqual([47, 96]);
});
test('finds two elements in sample list 17 that sum to 152', () => {
expect(findElementsSummingTo([2, 2, 4, 7, 27, 33, 36, 56, 60, 92], 152)).toEqual([60, 92]);
});
test('finds two elements in sample list 18 that sum to 52', () => {
expect(findElementsSummingTo([18, 20, 32, 36, 36, 38, 47, 76, 95, 97], 52)).toEqual([20, 32]);
});
test('finds two elements in sample list 19 that sum to 88', () => {
expect(findElementsSummingTo([9, 12, 17, 24, 25, 35, 38, 57, 71, 88], 88)).toEqual([17, 71]);
});
test('finds two elements in sample list 20 that sum to 57', () => {
expect(findElementsSummingTo([2, 9, 14, 20, 21, 26, 36, 70, 72, 86], 57)).toEqual([21, 36]);
});
test('finds two elements in sample list 21 that sum to 13', () => {
expect(findElementsSummingTo([6, 7, 21, 40, 41, 43, 51, 51, 72, 90], 13)).toEqual([6, 7]);
});
test('finds two elements in sample list 22 that sum to 112', () => {
expect(findElementsSummingTo([12, 14, 20, 29, 37, 54, 58, 66, 84, 87], 112)).toEqual([54, 58]);
});
test('finds two elements in sample list 23 that sum to 102', () => {
expect(findElementsSummingTo([21, 41, 43, 52, 55, 56, 61, 67, 69, 83], 102)).toEqual([41, 61]);
});
test('finds two elements in sample list 24 that sum to 89', () => {
expect(findElementsSummingTo([18, 23, 24, 29, 44, 47, 60, 67, 78, 79], 89)).toEqual([29, 60]);
});
test('finds two elements in sample list 25 that sum to 175', () => {
expect(findElementsSummingTo([14, 43, 51, 57, 58, 63, 68, 77, 84, 91], 175)).toEqual([84, 91]);
});
test('finds two elements in sample list 26 that sum to 67', () => {
expect(findElementsSummingTo([22, 30, 45, 46, 64, 76, 80, 82, 96, 99], 67)).toEqual([22, 45]);
});
test('finds two elements in sample list 27 that sum to 117', () => {
expect(findElementsSummingTo([1, 4, 7, 20, 23, 40, 46, 70, 94, 95], 117)).toEqual([23, 94]);
});
test('finds two elements in sample list 28 that sum to 63', () => {
expect(findElementsSummingTo([4, 6, 7, 23, 47, 48, 52, 59, 75, 99], 63)).toEqual([4, 59]);
});
test('finds two elements in sample list 29 that sum to 143', () => {
expect(findElementsSummingTo([0, 0, 12, 25, 31, 36, 36, 71, 72, 82], 143)).toEqual([71, 72]);
});
test('finds two elements in sample list 30 that sum to 157', () => {
expect(findElementsSummingTo([16, 20, 25, 71, 73, 81, 82, 84, 92, 95], 157)).toEqual([73, 84]);
});
test('finds two elements in sample list 31 that sum to 108', () => {
expect(findElementsSummingTo([5, 8, 21, 39, 39, 45, 58, 63, 71, 88], 108)).toEqual([45, 63]);
});
test('finds two elements in sample list 32 that sum to 106', () => {
expect(findElementsSummingTo([7, 8, 10, 35, 50, 58, 61, 62, 90, 96], 106)).toEqual([10, 96]);
});
test('finds two elements in sample list 33 that sum to 80', () => {
expect(findElementsSummingTo([5, 17, 22, 31, 34, 46, 50, 62, 89, 95], 80)).toEqual([34, 46]);
});
test('finds two elements in sample list 34 that sum to 145', () => {
expect(findElementsSummingTo([6, 15, 55, 58, 58, 59, 63, 70, 82, 89], 145)).toEqual([63, 82]);
});
test('finds two elements in sample list 35 that sum to 22', () => {
expect(findElementsSummingTo([6, 13, 16, 17, 44, 51, 64, 73, 79, 89], 22)).toEqual([6, 16]);
});
test('finds two elements in sample list 36 that sum to 93', () => {
expect(findElementsSummingTo([15, 23, 41, 44, 49, 56, 67, 76, 79, 98], 93)).toEqual([44, 49]);
});
test('finds two elements in sample list 37 that sum to 58', () => {
expect(findElementsSummingTo([9, 13, 34, 34, 45, 55, 79, 81, 90, 95], 58)).toEqual([13, 45]);
});
test('finds two elements in sample list 38 that sum to 73', () => {
expect(findElementsSummingTo([16, 25, 38, 54, 57, 60, 68, 78, 82, 89], 73)).toEqual([16, 57]);
});
test('finds two elements in sample list 39 that sum to 103', () => {
expect(findElementsSummingTo([1, 3, 10, 14, 40, 41, 44, 56, 93, 99], 103)).toEqual([10, 93]);
});
test('finds two elements in sample list 40 that sum to 163', () => {
expect(findElementsSummingTo([8, 9, 36, 44, 59, 68, 76, 79, 84, 94], 163)).toEqual([79, 84]);
});
test('finds two elements in sample list 41 that sum to 62', () => {
expect(findElementsSummingTo([0, 1, 11, 12, 20, 51, 55, 70, 70, 86], 62)).toEqual([11, 51]);
});
test('finds two elements in sample list 42 that sum to 100', () => {
expect(findElementsSummingTo([16, 21, 34, 64, 69, 71, 72, 72, 84, 87], 100)).toEqual([16, 84]);
});
test('finds two elements in sample list 43 that sum to 122', () => {
expect(findElementsSummingTo([13, 14, 24, 50, 56, 74, 83, 89, 93, 98], 122)).toEqual([24, 98]);
});
test('finds two elements in sample list 44 that sum to 88', () => {
expect(findElementsSummingTo([3, 13, 17, 34, 36, 40, 48, 87, 91, 97], 88)).toEqual([40, 48]);
});
test('finds two elements in sample list 45 that sum to 58', () => {
expect(findElementsSummingTo([4, 16, 17, 33, 41, 62, 65, 72, 78, 81], 58)).toEqual([17, 41]);
});
test('finds two elements in sample list 46 that sum to 161', () => {
expect(findElementsSummingTo([6, 17, 40, 48, 51, 56, 57, 79, 82, 99], 161)).toEqual([79, 82]);
});
test('finds two elements in sample list 47 that sum to 59', () => {
expect(findElementsSummingTo([10, 15, 17, 35, 43, 49, 67, 78, 82, 97], 59)).toEqual([10, 49]);
});
test('finds two elements in sample list 48 that sum to 182', () => {
expect(findElementsSummingTo([14, 18, 23, 25, 33, 38, 63, 69, 83, 99], 182)).toEqual([83, 99]);
});
test('finds two elements in sample list 49 that sum to 119', () => {
expect(findElementsSummingTo([13, 15, 18, 20, 30, 42, 43, 58, 65, 77], 119)).toEqual([42, 77]);
});
test('finds two elements in sample list 50 that sum to 25', () => {
expect(findElementsSummingTo([8, 17, 41, 49, 50, 55, 55, 59, 66, 88], 25)).toEqual([8, 17]);
});
test('finds two elements in sample list 51 that sum to 168', () => {
expect(findElementsSummingTo([16, 24, 29, 30, 32, 35, 48, 66, 78, 90], 168)).toEqual([78, 90]);
});
test('finds two elements in sample list 52 that sum to 174', () => {
expect(findElementsSummingTo([1, 5, 9, 9, 18, 32, 69, 77, 88, 97], 174)).toEqual([77, 97]);
});
test('finds two elements in sample list 53 that sum to 136', () => {
expect(findElementsSummingTo([30, 43, 55, 58, 65, 66, 67, 74, 84, 93], 136)).toEqual([43, 93]);
});
test('finds two elements in sample list 54 that sum to 7', () => {
expect(findElementsSummingTo([2, 5, 25, 31, 40, 40, 44, 54, 73, 99], 7)).toEqual([2, 5]);
});
test('finds two elements in sample list 55 that sum to 66', () => {
expect(findElementsSummingTo([8, 10, 14, 23, 25, 32, 41, 69, 83, 84], 66)).toEqual([25, 41]);
});
test('finds two elements in sample list 56 that sum to 54', () => {
expect(findElementsSummingTo([9, 11, 18, 19, 20, 33, 38, 45, 62, 63], 54)).toEqual([9, 45]);
});
test('finds two elements in sample list 57 that sum to 125', () => {
expect(findElementsSummingTo([1, 34, 36, 50, 52, 57, 63, 66, 83, 91], 125)).toEqual([34, 91]);
});
test('finds two elements in sample list 58 that sum to 176', () => {
expect(findElementsSummingTo([0, 1, 27, 59, 74, 76, 84, 92, 94, 99], 176)).toEqual([84, 92]);
});
test('finds two elements in sample list 59 that sum to 107', () => {
expect(findElementsSummingTo([1, 4, 16, 49, 79, 82, 90, 91, 94, 97], 107)).toEqual([16, 91]);
});
test('finds two elements in sample list 60 that sum to 63', () => {
expect(findElementsSummingTo([17, 20, 46, 47, 47, 57, 76, 83, 96, 99], 63)).toEqual([17, 46]);
});
test('finds two elements in sample list 61 that sum to 82', () => {
expect(findElementsSummingTo([8, 14, 19, 28, 40, 57, 60, 74, 77, 89], 82)).toEqual([8, 74]);
});
test('finds two elements in sample list 62 that sum to 176', () => {
expect(findElementsSummingTo([13, 13, 33, 64, 78, 79, 81, 91, 95, 99], 176)).toEqual([81, 95]);
});
test('finds two elements in sample list 63 that sum to 69', () => {
expect(findElementsSummingTo([5, 7, 11, 18, 22, 24, 47, 55, 81, 84], 69)).toEqual([22, 47]);
});
test('finds two elements in sample list 64 that sum to 182', () => {
expect(findElementsSummingTo([1, 25, 38, 39, 40, 40, 54, 91, 93, 98], 182)).toEqual([91, 91]);
});
test('finds two elements in sample list 65 that sum to 127', () => {
expect(findElementsSummingTo([7, 22, 40, 42, 71, 84, 85, 88, 94, 96], 127)).toEqual([42, 85]);
});
test('finds two elements in sample list 66 that sum to 106', () => {
expect(findElementsSummingTo([13, 13, 17, 23, 39, 41, 43, 63, 69, 73], 106)).toEqual([43, 63]);
});
test('finds two elements in sample list 67 that sum to 119', () => {
expect(findElementsSummingTo([1, 20, 33, 39, 43, 49, 66, 79, 86, 98], 119)).toEqual([33, 86]);
});
test('finds two elements in sample list 68 that sum to 119', () => {
expect(findElementsSummingTo([17, 22, 30, 35, 38, 39, 58, 60, 80, 95], 119)).toEqual([39, 80]);
});
test('finds two elements in sample list 69 that sum to 23', () => {
expect(findElementsSummingTo([7, 10, 13, 36, 38, 39, 39, 64, 87, 94], 23)).toEqual([10, 13]);
});
test('finds two elements in sample list 70 that sum to 112', () => {
expect(findElementsSummingTo([4, 20, 21, 31, 43, 50, 80, 86, 91, 93], 112)).toEqual([21, 91]);
});
test('finds two elements in sample list 71 that sum to 130', () => {
expect(findElementsSummingTo([3, 6, 43, 46, 70, 77, 78, 84, 90, 92], 130)).toEqual([46, 84]);
});
test('finds two elements in sample list 72 that sum to 14', () => {
expect(findElementsSummingTo([5, 9, 23, 26, 46, 69, 71, 80, 94, 98], 14)).toEqual([5, 9]);
});
test('finds two elements in sample list 73 that sum to 141', () => {
expect(findElementsSummingTo([30, 35, 46, 49, 50, 57, 58, 71, 91, 98], 141)).toEqual([50, 91]);
});
test('finds two elements in sample list 74 that sum to 134', () => {
expect(findElementsSummingTo([6, 9, 15, 20, 22, 31, 66, 68, 71, 86], 134)).toEqual([66, 68]);
});
test('finds two elements in sample list 75 that sum to 145', () => {
expect(findElementsSummingTo([9, 28, 28, 34, 50, 60, 67, 71, 78, 88], 145)).toEqual([67, 78]);
});
test('finds two elements in sample list 76 that sum to 120', () => {
expect(findElementsSummingTo([16, 18, 23, 24, 27, 42, 52, 56, 64, 91], 120)).toEqual([56, 64]);
});
test('finds two elements in sample list 77 that sum to 90', () => {
expect(findElementsSummingTo([5, 7, 13, 15, 16, 33, 34, 56, 65, 67], 90)).toEqual([34, 56]);
});
test('finds two elements in sample list 78 that sum to 87', () => {
expect(findElementsSummingTo([7, 14, 30, 38, 51, 55, 72, 76, 80, 88], 87)).toEqual([7, 80]);
});
test('finds two elements in sample list 79 that sum to 129', () => {
expect(findElementsSummingTo([9, 11, 22, 28, 40, 41, 71, 87, 88, 91], 129)).toEqual([41, 88]);
});
test('finds two elements in sample list 80 that sum to 120', () => {
expect(findElementsSummingTo([7, 7, 13, 18, 22, 23, 25, 47, 65, 73], 120)).toEqual([47, 73]);
});
test('finds two elements in sample list 81 that sum to 101', () => {
expect(findElementsSummingTo([13, 14, 15, 23, 40, 50, 51, 54, 54, 56], 101)).toEqual([50, 51]);
});
test('finds two elements in sample list 82 that sum to 26', () => {
expect(findElementsSummingTo([1, 1, 5, 7, 8, 21, 61, 71, 91, 93], 26)).toEqual([5, 21]);
});
test('finds two elements in sample list 83 that sum to 97', () => {
expect(findElementsSummingTo([2, 41, 47, 48, 50, 60, 66, 73, 79, 97], 97)).toEqual([47, 50]);
});
test('finds two elements in sample list 84 that sum to 39', () => {
expect(findElementsSummingTo([8, 20, 26, 31, 43, 60, 78, 86, 88, 95], 39)).toEqual([8, 31]);
});
test('finds two elements in sample list 85 that sum to 28', () => {
expect(findElementsSummingTo([0, 2, 9, 10, 28, 34, 84, 91, 93, 97], 28)).toEqual([0, 28]);
});
test('finds two elements in sample list 86 that sum to 112', () => {
expect(findElementsSummingTo([9, 15, 16, 16, 34, 52, 59, 60, 66, 99], 112)).toEqual([52, 60]);
});
test('finds two elements in sample list 87 that sum to 81', () => {
expect(findElementsSummingTo([6, 7, 37, 38, 39, 45, 66, 71, 74, 97], 81)).toEqual([7, 74]);
});
test('finds two elements in sample list 88 that sum to 97', () => {
expect(findElementsSummingTo([1, 27, 31, 32, 64, 65, 73, 75, 81, 82], 97)).toEqual([32, 65]);
});
test('finds two elements in sample list 89 that sum to 182', () => {
expect(findElementsSummingTo([0, 8, 15, 24, 43, 43, 46, 77, 85, 97], 182)).toEqual([85, 97]);
});
test('finds two elements in sample list 90 that sum to 59', () => {
expect(findElementsSummingTo([5, 21, 41, 44, 54, 62, 67, 88, 93, 93], 59)).toEqual([5, 54]);
});
test('finds two elements in sample list 91 that sum to 67', () => {
expect(findElementsSummingTo([2, 32, 36, 50, 53, 54, 65, 71, 74, 90], 67)).toEqual([2, 65]);
});
test('finds two elements in sample list 92 that sum to 156', () => {
expect(findElementsSummingTo([6, 27, 43, 44, 59, 63, 64, 65, 65, 92], 156)).toEqual([64, 92]);
});
test('finds two elements in sample list 93 that sum to 105', () => {
expect(findElementsSummingTo([10, 11, 32, 39, 43, 47, 60, 66, 77, 79], 105)).toEqual([39, 66]);
});
test('finds two elements in sample list 94 that sum to 69', () => {
expect(findElementsSummingTo([23, 27, 33, 36, 71, 75, 81, 83, 94, 94], 69)).toEqual([33, 36]);
});
test('finds two elements in sample list 95 that sum to 43', () => {
expect(findElementsSummingTo([1, 4, 27, 39, 53, 61, 64, 88, 95, 95], 43)).toEqual([4, 39]);
});
test('finds two elements in sample list 96 that sum to 30', () => {
expect(findElementsSummingTo([3, 18, 27, 33, 33, 38, 40, 56, 69, 98], 30)).toEqual([3, 27]);
});
test('finds two elements in sample list 97 that sum to 88', () => {
expect(findElementsSummingTo([1, 11, 27, 77, 80, 82, 86, 94, 99, 99], 88)).toEqual([11, 77]);
});
test('finds two elements in sample list 98 that sum to 165', () => {
expect(findElementsSummingTo([16, 22, 25, 33, 43, 48, 78, 84, 87, 91], 165)).toEqual([78, 87]);
});
test('finds two elements in sample list 99 that sum to 56', () => {
expect(findElementsSummingTo([11, 25, 29, 30, 31, 33, 41, 81, 97, 98], 56)).toEqual([25, 31]);
});
test('needs fewer than 2.5n array accesses on a small list', () => {
const list = [11, 25, 26, 30, 31, 33, 41, 81, 97, 98];
const basicOperations = new BasicOperationObserver(list);
findElementsSummingTo(list, 62);
expect(basicOperations.count).toBeLessThan(25);
});
test('needs fewer than 2.5n array accesses on a medium list', () => {
const list = [3, 4, 5, 7, 8, 9, 19, 21, 25, 29, 35, 38, 38, 59, 64, 66, 70, 71, 78, 98];
const basicOperations = new BasicOperationObserver(list);
findElementsSummingTo(list, 58);
expect(basicOperations.count).toBeLessThan(50);
});
test('needs fewer than 2.5n array accesses on a large list', () => {
const list = [
3, 6, 10, 18, 21, 27, 29, 30, 31, 36, 38, 40, 43, 43, 44,
45, 50, 50, 54, 54, 55, 61, 65, 73, 78, 80, 85, 87, 92, 95,
];
const basicOperations = new BasicOperationObserver(list);
findElementsSummingTo(list, 89);
expect(basicOperations.count).toBeLessThan(75);
});
});
import NodeEnvironment from 'jest-environment-node';
export default class FailFastEnvironment extends NodeEnvironment {
constructor(...rest) {
super(...rest);
this.failed = false;
}
async handleTestEvent(event, state) {
switch (event.name) {
case 'hook_failure':
case 'test_fn_failure':
this.failed = true;
break;
case 'test_start':
if (this.failed) {
event.test.mode = 'skip';
}
break;
default:
}
if (super.handleTestEvent !== undefined) {
await super.handleTestEvent(event, state);
}
}
}
// INSTRUCTIONS: Uncomment this import once you need to call Dijkstra's algorithm.
// import { dijkstras } from './dijkstras.js';
export class Stop {
constructor(time, station) {
this.time = time; // in minutes since the beginning of the journey
this.station = station; // represented as a string
}
}
export class Train {
constructor(name, stops) {
this.name = name;
this.stops = stops;
}
toString() {
return `the ${this.name} train`;
}
}
// eslint-disable-next-line no-unused-vars -- INSTRUCTIONS: Remove this comment once you use the Action class.
class Action {
constructor(delay, train, station, duration, newSituation) {
this.delay = delay;
this.train = train;
this.station = station;
this.duration = duration;
this.newSituation = newSituation;
}
get timeTaken() {
return this.delay + this.duration;
}
toString() {
const wait = this.delay > 0 ? `wait for ${this.delay} minute(s) and then ` : '';
return `${wait}ride ${this.train} to ${this.station} for ${this.duration} minute(s)`;
}
}
export class Schedule {
constructor(trains) {
this.trains = trains;
}
*getPossibleActionsFrom(situation) {
// INSTRUCTIONS: Implement this generator function to support your design of
// `planJourney` (see below).
}
}
export function planJourney(schedule, fromStation, toStation) {
// INSTRUCTIONS: Complete this JavaScript function so that it uses a single
// call to Dijkstra's algorithm to plan a sequence of actions that will get a
// traveler from `fromStation` at time zero to `toStation` as soon as
// possible. You may assume that the only way for the traveler to get between
// stations is to ride the trains whose stops are described by `schedule`.
//
// In your design you will have to determine what class to use for your graph,
// what class to use for your graph vertices, what class to use for your graph
// edges, how to create a starting vertex, and how to create a goal predicate
// to pass to Dijkstra's algorithm. You will also have to fill in the
// `getPossibleActionsFrom` generator function above, which will contain the
// bulk of the logic that you write.
//
// You can import Dijkstra's algorithm by uncommenting the import at the top
// of the file. Be sure to read that implementation of the algorithm to
// understand how it differs from the implementation shown in class.
//
// As a note, good style is to write your goal predicate as an arrow function.
// You may find the following article useful as a reminder about how to write
// an arrow function:
//
// * https://javascript.info/arrow-functions-basics
const path = undefined;
return path !== undefined ? path.map((action) => `${action}`) : undefined;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment