Comparing version 0.2.2 to 0.3.0
@@ -6,2 +6,16 @@ # Change Log | ||
# [0.3.0](https://github.com/devrsi0n/Algorithms/compare/v0.2.2...v0.3.0) (2019-12-03) | ||
### Bug Fixes | ||
- **algs4:** `StdIn` readline and run-main script input file ([f5b7221](https://github.com/devrsi0n/Algorithms/commit/f5b7221fa529f8198bdb64774b1da6c24f7fee05)) | ||
### Features | ||
- new lib `QuickFindUF` and `QuickUnionUF`, add UF tests ([a1af132](https://github.com/devrsi0n/Algorithms/commit/a1af1321f980756e0bc24933440ad21f5aefd046)) | ||
- new lib `Selection` ([6d5efa7](https://github.com/devrsi0n/Algorithms/commit/6d5efa74079af4d61acdfcfaf0eaa622ea0dca91)) | ||
- new UF lib `WeightedQuickUnionUF` ([52e0a2b](https://github.com/devrsi0n/Algorithms/commit/52e0a2bc57662c2105e6f33ddd65d894f5376429)) | ||
- **algs4:** add new lib `DoublingTest`, `DoublingRatio` ([e573264](https://github.com/devrsi0n/Algorithms/commit/e5732642ebd5ad480d749111cd0fc350da242272)) | ||
- **algs4:** new lib UF ([1fa44c5](https://github.com/devrsi0n/Algorithms/commit/1fa44c5405c78a65e14b3255f1e0f56d53b9275e)) | ||
## 0.2.1 (2019-11-08) | ||
@@ -8,0 +22,0 @@ |
@@ -0,0 +0,0 @@ /** |
@@ -1,2 +0,5 @@ | ||
import printf from 'printf'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var printf_1 = tslib_1.__importDefault(require("printf")); | ||
/** | ||
@@ -20,4 +23,4 @@ * The `Accumulator` class is a data type for computing the running | ||
*/ | ||
export default class Accumulator { | ||
constructor() { | ||
var Accumulator = /** @class */ (function () { | ||
function Accumulator() { | ||
this.n = 0; | ||
@@ -31,8 +34,8 @@ this.sum = 0; | ||
*/ | ||
addDataValue(val) { | ||
Accumulator.prototype.addDataValue = function (val) { | ||
this.n++; | ||
const delta = val - this.mu; | ||
var delta = val - this.mu; | ||
this.mu += delta / this.n; | ||
this.sum += ((this.n - 1) / this.n) * delta * delta; | ||
} | ||
}; | ||
/** | ||
@@ -42,5 +45,5 @@ * Returns the mean of the data values. | ||
*/ | ||
mean() { | ||
Accumulator.prototype.mean = function () { | ||
return this.mu; | ||
} | ||
}; | ||
/** | ||
@@ -50,7 +53,7 @@ * Returns the sample variance of the data values. | ||
*/ | ||
var() { | ||
Accumulator.prototype.var = function () { | ||
if (this.n <= 1) | ||
return Number.NaN; | ||
return this.sum / (this.n - 1); | ||
} | ||
}; | ||
/** | ||
@@ -60,5 +63,5 @@ * Returns the sample standard deviation of the data values. | ||
*/ | ||
stddev() { | ||
Accumulator.prototype.stddev = function () { | ||
return Math.sqrt(this.var()); | ||
} | ||
}; | ||
/** | ||
@@ -68,9 +71,11 @@ * Returns the number of data values. | ||
*/ | ||
count() { | ||
Accumulator.prototype.count = function () { | ||
return this.n; | ||
} | ||
toString() { | ||
return `Mean (${this.n} values): ${printf('%7.5f', this.mean())}, stddev = ${this.stddev()}`; | ||
} | ||
} | ||
}; | ||
Accumulator.prototype.toString = function () { | ||
return "Mean (" + this.n + " values): " + printf_1.default('%7.5f', this.mean()) + ", stddev = " + this.stddev(); | ||
}; | ||
return Accumulator; | ||
}()); | ||
exports.default = Accumulator; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ /** |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -5,19 +7,21 @@ * FixedCapacityStack and ResizingArrayStack. | ||
*/ | ||
export default class ArrayStack { | ||
constructor() { | ||
var ArrayStack = /** @class */ (function () { | ||
function ArrayStack() { | ||
this.a = []; | ||
} | ||
push(item) { | ||
ArrayStack.prototype.push = function (item) { | ||
this.a.push(item); | ||
} | ||
pop() { | ||
}; | ||
ArrayStack.prototype.pop = function () { | ||
return this.a.pop(); | ||
} | ||
isEmpty() { | ||
}; | ||
ArrayStack.prototype.isEmpty = function () { | ||
return this.size() === 0; | ||
} | ||
size() { | ||
}; | ||
ArrayStack.prototype.size = function () { | ||
return this.a.length; | ||
} | ||
} | ||
}; | ||
return ArrayStack; | ||
}()); | ||
exports.default = ArrayStack; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ export declare class Node<Item> { |
@@ -0,8 +1,13 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
// helper linked list class | ||
export class Node { | ||
constructor() { | ||
var Node = /** @class */ (function () { | ||
function Node() { | ||
this.item = null; | ||
this.next = null; | ||
} | ||
} | ||
return Node; | ||
}()); | ||
exports.Node = Node; | ||
/** | ||
@@ -25,7 +30,7 @@ * The Bag class represents a bag (or multiset) of | ||
*/ | ||
export default class Bag { | ||
var Bag = /** @class */ (function () { | ||
/** | ||
* Initializes an empty bag. | ||
*/ | ||
constructor() { | ||
function Bag() { | ||
this.first = null; | ||
@@ -41,5 +46,5 @@ this.n = 0; | ||
*/ | ||
isEmpty() { | ||
Bag.prototype.isEmpty = function () { | ||
return this.first == null; | ||
} | ||
}; | ||
/** | ||
@@ -50,5 +55,5 @@ * Returns the number of items in this bag. | ||
*/ | ||
size() { | ||
Bag.prototype.size = function () { | ||
return this.n; | ||
} | ||
}; | ||
/** | ||
@@ -59,5 +64,5 @@ * Adds the item to this bag. | ||
*/ | ||
add(item) { | ||
Bag.prototype.add = function (item) { | ||
this.modCount += 1; | ||
const oldfirst = this.first; | ||
var oldfirst = this.first; | ||
this.first = new Node(); | ||
@@ -67,3 +72,3 @@ this.first.item = item; | ||
this.n++; | ||
} | ||
}; | ||
/** | ||
@@ -74,9 +79,9 @@ * Returns an iterator that iterates over the items in this bag in arbitrary order. | ||
*/ | ||
[Symbol.iterator]() { | ||
const { first, modCount } = this; | ||
let current = first; | ||
const expectModCount = modCount; | ||
const self = this; | ||
Bag.prototype[Symbol.iterator] = function () { | ||
var _a = this, first = _a.first, modCount = _a.modCount; | ||
var current = first; | ||
var expectModCount = modCount; | ||
var self = this; | ||
return { | ||
next() { | ||
next: function () { | ||
if (expectModCount !== self.modCount) { | ||
@@ -86,3 +91,3 @@ throw new Error('ConcurrentModificationException'); | ||
if (current !== null) { | ||
const result = { | ||
var result = { | ||
done: false, | ||
@@ -100,3 +105,3 @@ value: current.item, | ||
}; | ||
} | ||
}; | ||
/** | ||
@@ -107,10 +112,23 @@ * Returns a string representation of this queue. | ||
*/ | ||
toString() { | ||
let str = ''; | ||
for (const node of this) { | ||
str += `${node}, `; | ||
Bag.prototype.toString = function () { | ||
var e_1, _a; | ||
var str = ''; | ||
try { | ||
for (var _b = tslib_1.__values(this), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var node = _c.value; | ||
str += node + ", "; | ||
} | ||
} | ||
return `Bag [${str}]`; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return "Bag [" + str + "]"; | ||
}; | ||
return Bag; | ||
}()); | ||
exports.default = Bag; | ||
//# sourceMappingURL=index.js.map |
@@ -17,5 +17,5 @@ export default class BinarySearch { | ||
* @param a the array of integers, must be sorted in ascending order | ||
* @return index of key in array {@code a} if present; {@code -1} otherwise | ||
* @return index of key in array `a` if present; `-1` otherwise | ||
*/ | ||
static rank(key: number, a: number[]): number; | ||
} |
@@ -1,2 +0,6 @@ | ||
export default class BinarySearch { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var BinarySearch = /** @class */ (function () { | ||
function BinarySearch() { | ||
} | ||
/** | ||
@@ -9,8 +13,8 @@ * Returns the index of the specified key in the specified array. | ||
*/ | ||
static indexOf(a, target) { | ||
let lo = 0; | ||
let hi = a.length - 1; | ||
BinarySearch.indexOf = function (a, target) { | ||
var lo = 0; | ||
var hi = a.length - 1; | ||
while (lo <= hi) { | ||
const mid = lo + Math.round((hi - lo) / 2); | ||
const val = a[mid]; | ||
var mid = lo + Math.round((hi - lo) / 2); | ||
var val = a[mid]; | ||
if (val > target) { | ||
@@ -27,3 +31,3 @@ hi = mid - 1; | ||
return -1; | ||
} | ||
}; | ||
/** | ||
@@ -36,8 +40,10 @@ * Returns the index of the specified key in the specified array. | ||
* @param a the array of integers, must be sorted in ascending order | ||
* @return index of key in array {@code a} if present; {@code -1} otherwise | ||
* @return index of key in array `a` if present; `-1` otherwise | ||
*/ | ||
static rank(key, a) { | ||
BinarySearch.rank = function (key, a) { | ||
return BinarySearch.indexOf(a, key); | ||
} | ||
} | ||
}; | ||
return BinarySearch; | ||
}()); | ||
exports.default = BinarySearch; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ /** |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -8,4 +10,4 @@ * The Counter class is a mutable data type to encapsulate a counter. | ||
*/ | ||
export default class Counter { | ||
constructor(id) { | ||
var Counter = /** @class */ (function () { | ||
function Counter(id) { | ||
this.name = id; | ||
@@ -17,5 +19,5 @@ this.count = 0; | ||
*/ | ||
increment() { | ||
Counter.prototype.increment = function () { | ||
this.count++; | ||
} | ||
}; | ||
/** | ||
@@ -26,5 +28,5 @@ * Returns the current value of this counter. | ||
*/ | ||
tally() { | ||
Counter.prototype.tally = function () { | ||
return this.count; | ||
} | ||
}; | ||
/** | ||
@@ -35,6 +37,8 @@ * Returns a string representation of this counter. | ||
*/ | ||
toString() { | ||
return `${this.count} ${this.name}`; | ||
} | ||
} | ||
Counter.prototype.toString = function () { | ||
return this.count + " " + this.name; | ||
}; | ||
return Counter; | ||
}()); | ||
exports.default = Counter; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ export default class Evaluate { |
@@ -1,44 +0,60 @@ | ||
import Stack from '../Stack'; | ||
export default class Evaluate { | ||
constructor(inp) { | ||
this.inputArray = inp.split(' ').filter((char) => !!char); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var Stack_1 = tslib_1.__importDefault(require("../Stack")); | ||
var Evaluate = /** @class */ (function () { | ||
function Evaluate(inp) { | ||
this.inputArray = inp.split(' ').filter(function (char) { return !!char; }); | ||
} | ||
calculate() { | ||
const ops = new Stack(); | ||
const vals = new Stack(); | ||
for (const input of this.inputArray) { | ||
if (input === '(') { | ||
// Nothing | ||
} | ||
else if (['+', '-', '*', '/', 'sqrt'].includes(input)) { | ||
ops.push(input); | ||
} | ||
else if (input === ')') { | ||
const op = ops.pop(); | ||
let v = vals.pop(); | ||
if (op === '+') { | ||
v = vals.pop() + v; | ||
Evaluate.prototype.calculate = function () { | ||
var e_1, _a; | ||
var ops = new Stack_1.default(); | ||
var vals = new Stack_1.default(); | ||
try { | ||
for (var _b = tslib_1.__values(this.inputArray), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var input = _c.value; | ||
if (input === '(') { | ||
// Nothing | ||
} | ||
else if (op === '-') { | ||
v = vals.pop() - v; | ||
else if (['+', '-', '*', '/', 'sqrt'].includes(input)) { | ||
ops.push(input); | ||
} | ||
else if (op === '*') { | ||
/* eslint-disable operator-assignment */ | ||
v = vals.pop() * v; | ||
else if (input === ')') { | ||
var op = ops.pop(); | ||
var v = vals.pop(); | ||
if (op === '+') { | ||
v = vals.pop() + v; | ||
} | ||
else if (op === '-') { | ||
v = vals.pop() - v; | ||
} | ||
else if (op === '*') { | ||
/* eslint-disable operator-assignment */ | ||
v = vals.pop() * v; | ||
} | ||
else if (op === '/') { | ||
v = vals.pop() / v; | ||
} | ||
else if (op === 'sqrt') { | ||
v = Math.sqrt(v); | ||
} | ||
vals.push(v); | ||
} | ||
else if (op === '/') { | ||
v = vals.pop() / v; | ||
else { | ||
vals.push(parseFloat(input)); | ||
} | ||
else if (op === 'sqrt') { | ||
v = Math.sqrt(v); | ||
} | ||
vals.push(v); | ||
} | ||
else { | ||
vals.push(parseFloat(input)); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return vals.pop(); | ||
} | ||
} | ||
}; | ||
return Evaluate; | ||
}()); | ||
exports.default = Evaluate; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ /** |
@@ -0,1 +1,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -6,3 +8,3 @@ * Calculate 2 nonnegative integers's greatest common divisor(最大公约数) | ||
*/ | ||
export default function gcb(p, q) { | ||
function gcb(p, q) { | ||
if (p < 0 || q < 0) { | ||
@@ -14,5 +16,6 @@ throw new Error('Input params must be nonnegative integer'); | ||
} | ||
const r = p % q; | ||
var r = p % q; | ||
return gcb(q, r); | ||
} | ||
exports.default = gcb; | ||
//# sourceMappingURL=index.js.map |
export { default as Accumulator } from './Accumulator'; | ||
export { default as Bag } from './Bag'; | ||
export { default as Counter } from './Counter'; | ||
export { default as DoublingRatio } from './DoublingRatio'; | ||
export { default as DoublingTest } from './DoublingTest'; | ||
export { default as Evaluate } from './Evaluate'; | ||
@@ -10,2 +12,3 @@ export { default as GCB } from './GCB'; | ||
export { default as Queue } from './Queue'; | ||
export { default as UF } from './UF'; | ||
export { default as Stack } from './Stack'; | ||
@@ -12,0 +15,0 @@ export { default as StdDraw } from './StdDraw'; |
@@ -1,17 +0,41 @@ | ||
export { default as Accumulator } from './Accumulator'; | ||
export { default as Bag } from './Bag'; | ||
export { default as Counter } from './Counter'; | ||
export { default as Evaluate } from './Evaluate'; | ||
export { default as GCB } from './GCB'; | ||
export { default as Interval1D } from './Interval1D'; | ||
export { default as Interval2D } from './Interval2D'; | ||
export { default as Point2D } from './Point2D'; | ||
export { default as Queue } from './Queue'; | ||
export { default as Stack } from './Stack'; | ||
export { default as StdDraw } from './StdDraw'; | ||
export { default as StdOut } from './StdOut'; | ||
export { default as StdRandom } from './StdRandom'; | ||
export { default as StopWatch } from './StopWatch'; | ||
export { default as ThreeSumFast } from './ThreeSumFast'; | ||
export { default as VisualAccumulator } from './VisualAccumulator'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Accumulator_1 = require("./Accumulator"); | ||
exports.Accumulator = Accumulator_1.default; | ||
var Bag_1 = require("./Bag"); | ||
exports.Bag = Bag_1.default; | ||
var Counter_1 = require("./Counter"); | ||
exports.Counter = Counter_1.default; | ||
var DoublingRatio_1 = require("./DoublingRatio"); | ||
exports.DoublingRatio = DoublingRatio_1.default; | ||
var DoublingTest_1 = require("./DoublingTest"); | ||
exports.DoublingTest = DoublingTest_1.default; | ||
var Evaluate_1 = require("./Evaluate"); | ||
exports.Evaluate = Evaluate_1.default; | ||
var GCB_1 = require("./GCB"); | ||
exports.GCB = GCB_1.default; | ||
var Interval1D_1 = require("./Interval1D"); | ||
exports.Interval1D = Interval1D_1.default; | ||
var Interval2D_1 = require("./Interval2D"); | ||
exports.Interval2D = Interval2D_1.default; | ||
var Point2D_1 = require("./Point2D"); | ||
exports.Point2D = Point2D_1.default; | ||
var Queue_1 = require("./Queue"); | ||
exports.Queue = Queue_1.default; | ||
var UF_1 = require("./UF"); | ||
exports.UF = UF_1.default; | ||
var Stack_1 = require("./Stack"); | ||
exports.Stack = Stack_1.default; | ||
var StdDraw_1 = require("./StdDraw"); | ||
exports.StdDraw = StdDraw_1.default; | ||
var StdOut_1 = require("./StdOut"); | ||
exports.StdOut = StdOut_1.default; | ||
var StdRandom_1 = require("./StdRandom"); | ||
exports.StdRandom = StdRandom_1.default; | ||
var StopWatch_1 = require("./StopWatch"); | ||
exports.StopWatch = StopWatch_1.default; | ||
var ThreeSumFast_1 = require("./ThreeSumFast"); | ||
exports.ThreeSumFast = ThreeSumFast_1.default; | ||
var VisualAccumulator_1 = require("./VisualAccumulator"); | ||
exports.VisualAccumulator = VisualAccumulator_1.default; | ||
//# sourceMappingURL=index.js.map |
/** | ||
* The {@code Interval1D} class represents a one-dimensional interval. | ||
* The `Interval1D` class represents a one-dimensional interval. | ||
* The interval is <em>closed</em>—it contains both endpoints. | ||
* Intervals are immutable: their values cannot be changed after they are created. | ||
* The class {@code Interval1D} includes methods for checking whether | ||
* The class `Interval1D` includes methods for checking whether | ||
* an interval contains a point and determining whether two intervals intersect. | ||
@@ -7,0 +7,0 @@ * <p> |
@@ -1,7 +0,9 @@ | ||
import { assertSafeNumber } from '../utils/assert'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var assert_1 = require("../utils/assert"); | ||
/** | ||
* The {@code Interval1D} class represents a one-dimensional interval. | ||
* The `Interval1D` class represents a one-dimensional interval. | ||
* The interval is <em>closed</em>—it contains both endpoints. | ||
* Intervals are immutable: their values cannot be changed after they are created. | ||
* The class {@code Interval1D} includes methods for checking whether | ||
* The class `Interval1D` includes methods for checking whether | ||
* an interval contains a point and determining whether two intervals intersect. | ||
@@ -13,5 +15,5 @@ * <p> | ||
*/ | ||
export default class Interval1D { | ||
constructor(min, max) { | ||
assertSafeNumber([min, max]); | ||
var Interval1D = /** @class */ (function () { | ||
function Interval1D(min, max) { | ||
assert_1.assertSafeNumber([min, max]); | ||
if (max < min) { | ||
@@ -28,5 +30,5 @@ throw new Error('max must greater than min'); | ||
*/ | ||
length() { | ||
Interval1D.prototype.length = function () { | ||
return this._max - this._min; | ||
} | ||
}; | ||
/** | ||
@@ -39,5 +41,5 @@ * Returns true if this interval contains the specified value. | ||
*/ | ||
contains(x) { | ||
Interval1D.prototype.contains = function (x) { | ||
return this._min <= x && x <= this._max; | ||
} | ||
}; | ||
/** | ||
@@ -50,3 +52,3 @@ * Returns true if this interval intersects the specified interval. | ||
*/ | ||
intersects(that) { | ||
Interval1D.prototype.intersects = function (that) { | ||
if (this._max < that._min) | ||
@@ -57,11 +59,13 @@ return false; | ||
return true; | ||
} | ||
min() { | ||
}; | ||
Interval1D.prototype.min = function () { | ||
return this._min; | ||
} | ||
max() { | ||
}; | ||
Interval1D.prototype.max = function () { | ||
return this._max; | ||
} | ||
draw() { } | ||
} | ||
}; | ||
Interval1D.prototype.draw = function () { }; | ||
return Interval1D; | ||
}()); | ||
exports.default = Interval1D; | ||
//# sourceMappingURL=index.js.map |
import Interval1D from '../Interval1D'; | ||
import Point2D from '../Point2D'; | ||
/** | ||
* The {@code Interval2D} class represents a closed two-dimensional interval, | ||
* which represents all points (x, y) with both {@code xmin <= x <= xmax} and | ||
* {@code ymin <= y <= ymax}. | ||
* The `Interval2D` class represents a closed two-dimensional interval, | ||
* which represents all points (x, y) with both `xmin <= x <= xmax` and | ||
* `ymin <= y <= ymax`. | ||
* Two-dimensional intervals are immutable: their values cannot be changed | ||
* after they are created. | ||
* The class {@code Interval2D} includes methods for checking whether | ||
* The class `Interval2D` includes methods for checking whether | ||
* a two-dimensional interval contains a point and determining whether | ||
@@ -11,0 +11,0 @@ * two two-dimensional intervals intersect. |
@@ -1,9 +0,12 @@ | ||
import StdDraw from '../StdDraw'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var StdDraw_1 = tslib_1.__importDefault(require("../StdDraw")); | ||
/** | ||
* The {@code Interval2D} class represents a closed two-dimensional interval, | ||
* which represents all points (x, y) with both {@code xmin <= x <= xmax} and | ||
* {@code ymin <= y <= ymax}. | ||
* The `Interval2D` class represents a closed two-dimensional interval, | ||
* which represents all points (x, y) with both `xmin <= x <= xmax` and | ||
* `ymin <= y <= ymax`. | ||
* Two-dimensional intervals are immutable: their values cannot be changed | ||
* after they are created. | ||
* The class {@code Interval2D} includes methods for checking whether | ||
* The class `Interval2D` includes methods for checking whether | ||
* a two-dimensional interval contains a point and determining whether | ||
@@ -16,4 +19,4 @@ * two two-dimensional intervals intersect. | ||
*/ | ||
export default class Interval2D { | ||
constructor(x, y) { | ||
var Interval2D = /** @class */ (function () { | ||
function Interval2D(x, y) { | ||
this._x = x; | ||
@@ -26,5 +29,5 @@ this._y = y; | ||
*/ | ||
area() { | ||
Interval2D.prototype.area = function () { | ||
return this._x.length() * this._y.length(); | ||
} | ||
}; | ||
/** | ||
@@ -35,11 +38,11 @@ * Does this two-dimensional interval contain the point p? | ||
*/ | ||
contains(p) { | ||
Interval2D.prototype.contains = function (p) { | ||
return this._x.contains(p.x()) && this._y.contains(p.y()); | ||
} | ||
x() { | ||
}; | ||
Interval2D.prototype.x = function () { | ||
return this._x; | ||
} | ||
y() { | ||
}; | ||
Interval2D.prototype.y = function () { | ||
return this._y; | ||
} | ||
}; | ||
/** | ||
@@ -51,3 +54,3 @@ * Does this two-dimensional interval intersect that two-dimensional interval? | ||
*/ | ||
intersects(that) { | ||
Interval2D.prototype.intersects = function (that) { | ||
if (!this._x.intersects(that.x())) | ||
@@ -58,12 +61,14 @@ return false; | ||
return true; | ||
} | ||
}; | ||
/** | ||
* Draws this two-dimensional interval to standard draw. | ||
*/ | ||
draw() { | ||
const xc = (this._x.min() + this._x.max()) / 2.0; | ||
const yc = (this._y.min() + this._y.max()) / 2.0; | ||
StdDraw.rectangle(xc, yc, this._x.length() / 2.0, this._y.length() / 2.0); | ||
} | ||
} | ||
Interval2D.prototype.draw = function () { | ||
var xc = (this._x.min() + this._x.max()) / 2.0; | ||
var yc = (this._y.min() + this._y.max()) / 2.0; | ||
StdDraw_1.default.rectangle(xc, yc, this._x.length() / 2.0, this._y.length() / 2.0); | ||
}; | ||
return Interval2D; | ||
}()); | ||
exports.default = Interval2D; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ /** |
@@ -1,3 +0,6 @@ | ||
import { assertSafeNumber } from '../utils/assert'; | ||
import StdDraw from '../StdDraw'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var assert_1 = require("../utils/assert"); | ||
var StdDraw_1 = tslib_1.__importDefault(require("../StdDraw")); | ||
/** | ||
@@ -16,15 +19,15 @@ * The Point class is an immutable data type to encapsulate a | ||
*/ | ||
export default class Point2D { | ||
constructor(x, y) { | ||
assertSafeNumber(x); | ||
assertSafeNumber(y); | ||
var Point2D = /** @class */ (function () { | ||
function Point2D(x, y) { | ||
assert_1.assertSafeNumber(x); | ||
assert_1.assertSafeNumber(y); | ||
this._x = x; | ||
this._y = y; | ||
} | ||
x() { | ||
Point2D.prototype.x = function () { | ||
return this._x; | ||
} | ||
y() { | ||
}; | ||
Point2D.prototype.y = function () { | ||
return this._y; | ||
} | ||
}; | ||
/** | ||
@@ -34,5 +37,5 @@ * Returns the polar radius of this point. | ||
*/ | ||
r() { | ||
Point2D.prototype.r = function () { | ||
return Math.sqrt(Math.pow(this._x, 2) + Math.pow(this._y, 2)); | ||
} | ||
}; | ||
/** | ||
@@ -42,5 +45,5 @@ * Returns the angle of this point in polar coordinates. | ||
*/ | ||
theta() { | ||
Point2D.prototype.theta = function () { | ||
return Math.atan2(this._y, this._x); | ||
} | ||
}; | ||
/** | ||
@@ -51,13 +54,13 @@ * Returns the Euclidean distance between this point and that point. | ||
*/ | ||
distanceTo(that) { | ||
const dx = this._x - that.x(); | ||
const dy = this._y - that.y(); | ||
Point2D.prototype.distanceTo = function (that) { | ||
var dx = this._x - that.x(); | ||
var dy = this._y - that.y(); | ||
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); | ||
} | ||
}; | ||
/** | ||
* Plot this point using standard draw. | ||
*/ | ||
draw() { | ||
StdDraw.point(this._x, this._y); | ||
} | ||
Point2D.prototype.draw = function () { | ||
StdDraw_1.default.point(this._x, this._y); | ||
}; | ||
/** | ||
@@ -67,6 +70,8 @@ * Return a string representation of this point. | ||
*/ | ||
toString() { | ||
return `(${this._x}, ${this._y})`; | ||
} | ||
} | ||
Point2D.prototype.toString = function () { | ||
return "(" + this._x + ", " + this._y + ")"; | ||
}; | ||
return Point2D; | ||
}()); | ||
exports.default = Point2D; | ||
//# sourceMappingURL=index.js.map |
@@ -6,3 +6,3 @@ export declare class Node<Item> { | ||
/** | ||
* The {@code Queue} class represents a first-in-first-out (FIFO) | ||
* The `Queue` class represents a first-in-first-out (FIFO) | ||
* queue of generic items. | ||
@@ -35,3 +35,3 @@ * It supports the usual <em>enqueue</em> and <em>dequeue</em> | ||
* | ||
* @return {@code true} if this queue is empty; {@code false} otherwise | ||
* @return `true` if this queue is empty; `false` otherwise | ||
*/ | ||
@@ -38,0 +38,0 @@ isEmpty(): boolean; |
@@ -1,9 +0,14 @@ | ||
export class Node { | ||
constructor() { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var Node = /** @class */ (function () { | ||
function Node() { | ||
this.item = null; | ||
this.next = null; | ||
} | ||
} | ||
return Node; | ||
}()); | ||
exports.Node = Node; | ||
/** | ||
* The {@code Queue} class represents a first-in-first-out (FIFO) | ||
* The `Queue` class represents a first-in-first-out (FIFO) | ||
* queue of generic items. | ||
@@ -27,4 +32,4 @@ * It supports the usual <em>enqueue</em> and <em>dequeue</em> | ||
*/ | ||
export default class Queue { | ||
constructor() { | ||
var Queue = /** @class */ (function () { | ||
function Queue() { | ||
this.first = null; | ||
@@ -38,7 +43,7 @@ this.last = null; | ||
* | ||
* @return {@code true} if this queue is empty; {@code false} otherwise | ||
* @return `true` if this queue is empty; `false` otherwise | ||
*/ | ||
isEmpty() { | ||
Queue.prototype.isEmpty = function () { | ||
return this.first == null; | ||
} | ||
}; | ||
/** | ||
@@ -49,5 +54,5 @@ * Returns the number of items in this queue. | ||
*/ | ||
size() { | ||
Queue.prototype.size = function () { | ||
return this.n; | ||
} | ||
}; | ||
/** | ||
@@ -59,7 +64,7 @@ * Returns the item least recently added to this queue. | ||
*/ | ||
peek() { | ||
Queue.prototype.peek = function () { | ||
if (this.isEmpty()) | ||
throw new Error('Queue underflow'); | ||
return this.first.item; | ||
} | ||
}; | ||
/** | ||
@@ -70,5 +75,5 @@ * Adds the item to this queue. | ||
*/ | ||
enqueue(item) { | ||
Queue.prototype.enqueue = function (item) { | ||
this.modCount += 1; | ||
const oldlast = this.last; | ||
var oldlast = this.last; | ||
this.last = new Node(); | ||
@@ -84,3 +89,3 @@ this.last.item = item; | ||
this.n++; | ||
} | ||
}; | ||
/** | ||
@@ -92,3 +97,3 @@ * Removes and returns the item on this queue that was least recently added. | ||
*/ | ||
dequeue() { | ||
Queue.prototype.dequeue = function () { | ||
this.modCount += 1; | ||
@@ -98,3 +103,3 @@ if (this.isEmpty()) { | ||
} | ||
const { item } = this.first; | ||
var item = this.first.item; | ||
this.first = this.first.next; | ||
@@ -105,3 +110,3 @@ this.n--; | ||
return item; | ||
} | ||
}; | ||
/** | ||
@@ -112,9 +117,20 @@ * Returns a string representation of this queue. | ||
*/ | ||
toString() { | ||
let str = ''; | ||
for (const node of this) { | ||
str += `${node}, `; | ||
Queue.prototype.toString = function () { | ||
var e_1, _a; | ||
var str = ''; | ||
try { | ||
for (var _b = tslib_1.__values(this), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var node = _c.value; | ||
str += node + ", "; | ||
} | ||
} | ||
return `Queue [${str}]`; | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return "Queue [" + str + "]"; | ||
}; | ||
/** | ||
@@ -125,9 +141,9 @@ * Returns an iterator that iterates over the items in this queue in FIFO order. | ||
*/ | ||
[Symbol.iterator]() { | ||
const { first, modCount } = this; | ||
let current = first; | ||
const expectModCount = modCount; | ||
const self = this; | ||
Queue.prototype[Symbol.iterator] = function () { | ||
var _a = this, first = _a.first, modCount = _a.modCount; | ||
var current = first; | ||
var expectModCount = modCount; | ||
var self = this; | ||
return { | ||
next() { | ||
next: function () { | ||
if (expectModCount !== self.modCount) { | ||
@@ -137,3 +153,3 @@ throw new Error('ConcurrentModificationException'); | ||
if (current !== null) { | ||
const result = { | ||
var result = { | ||
done: false, | ||
@@ -151,4 +167,6 @@ value: current.item, | ||
}; | ||
} | ||
} | ||
}; | ||
return Queue; | ||
}()); | ||
exports.default = Queue; | ||
//# sourceMappingURL=index.js.map |
@@ -6,3 +6,3 @@ export declare class Node<Item> { | ||
/** | ||
* The {@code Stack} class represents a last-in-first-out (LIFO) stack of generic items. | ||
* The `Stack` class represents a last-in-first-out (LIFO) stack of generic items. | ||
* It supports the usual <em>push</em> and <em>pop</em> operations, along with methods | ||
@@ -9,0 +9,0 @@ * for peeking at the top item, testing if the stack is empty, and iterating through |
@@ -1,9 +0,14 @@ | ||
export class Node { | ||
constructor() { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var Node = /** @class */ (function () { | ||
function Node() { | ||
this.item = null; | ||
this.next = null; | ||
} | ||
} | ||
return Node; | ||
}()); | ||
exports.Node = Node; | ||
/** | ||
* The {@code Stack} class represents a last-in-first-out (LIFO) stack of generic items. | ||
* The `Stack` class represents a last-in-first-out (LIFO) stack of generic items. | ||
* It supports the usual <em>push</em> and <em>pop</em> operations, along with methods | ||
@@ -26,4 +31,4 @@ * for peeking at the top item, testing if the stack is empty, and iterating through | ||
*/ | ||
export default class Stack { | ||
constructor() { | ||
var Stack = /** @class */ (function () { | ||
function Stack() { | ||
this.first = null; | ||
@@ -38,5 +43,5 @@ this.n = 0; | ||
*/ | ||
isEmpty() { | ||
Stack.prototype.isEmpty = function () { | ||
return this.first == null; | ||
} | ||
}; | ||
/** | ||
@@ -47,5 +52,5 @@ * Returns the number of items in this stack. | ||
*/ | ||
size() { | ||
Stack.prototype.size = function () { | ||
return this.n; | ||
} | ||
}; | ||
/** | ||
@@ -56,5 +61,5 @@ * Adds the item to this stack. | ||
*/ | ||
push(item) { | ||
Stack.prototype.push = function (item) { | ||
this.modCount += 1; | ||
const oldfirst = this.first; | ||
var oldfirst = this.first; | ||
this.first = new Node(); | ||
@@ -64,3 +69,3 @@ this.first.item = item; | ||
this.n++; | ||
} | ||
}; | ||
/** | ||
@@ -72,3 +77,3 @@ * Removes and returns the item most recently added to this stack. | ||
*/ | ||
pop() { | ||
Stack.prototype.pop = function () { | ||
this.modCount += 1; | ||
@@ -78,7 +83,7 @@ if (this.isEmpty()) { | ||
} | ||
const { item } = this.first; | ||
var item = this.first.item; | ||
this.first = this.first.next; | ||
this.n--; | ||
return item; | ||
} | ||
}; | ||
/** | ||
@@ -90,3 +95,3 @@ * Returns (but does not remove) the item most recently added to this stack. | ||
*/ | ||
peek() { | ||
Stack.prototype.peek = function () { | ||
if (this.isEmpty()) { | ||
@@ -96,3 +101,3 @@ throw new Error('Stack underflow'); | ||
return this.first.item; | ||
} | ||
}; | ||
/** | ||
@@ -103,9 +108,20 @@ * Returns a string representation of this stack. | ||
*/ | ||
toString() { | ||
let str = ''; | ||
for (const node of this) { | ||
str += `${node}, `; | ||
Stack.prototype.toString = function () { | ||
var e_1, _a; | ||
var str = ''; | ||
try { | ||
for (var _b = tslib_1.__values(this), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var node = _c.value; | ||
str += node + ", "; | ||
} | ||
} | ||
return `Stack [${str}]`; | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return "Stack [" + str + "]"; | ||
}; | ||
/** | ||
@@ -116,9 +132,9 @@ * Returns an iterator to this stack that iterates through the items in LIFO order. | ||
*/ | ||
[Symbol.iterator]() { | ||
const { first, modCount } = this; | ||
let current = first; | ||
const expectModCount = modCount; | ||
const self = this; | ||
Stack.prototype[Symbol.iterator] = function () { | ||
var _a = this, first = _a.first, modCount = _a.modCount; | ||
var current = first; | ||
var expectModCount = modCount; | ||
var self = this; | ||
return { | ||
next() { | ||
next: function () { | ||
if (expectModCount !== self.modCount) { | ||
@@ -128,3 +144,3 @@ throw new Error('ConcurrentModificationException'); | ||
if (current !== null) { | ||
const result = { | ||
var result = { | ||
done: false, | ||
@@ -142,4 +158,6 @@ value: current.item, | ||
}; | ||
} | ||
} | ||
}; | ||
return Stack; | ||
}()); | ||
exports.default = Stack; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ export default class StdDraw { |
@@ -1,5 +0,10 @@ | ||
import assert from '../utils/assert'; | ||
export default class StdDraw { | ||
static line(x0, y0, x1, y1) { | ||
const { ctx } = StdDraw; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var assert_1 = tslib_1.__importDefault(require("../utils/assert")); | ||
var StdDraw = /** @class */ (function () { | ||
function StdDraw() { | ||
} | ||
StdDraw.line = function (x0, y0, x1, y1) { | ||
var ctx = StdDraw.ctx; | ||
ctx.beginPath(); | ||
@@ -9,3 +14,3 @@ ctx.moveTo(x0, y0); | ||
ctx.stroke(); | ||
} | ||
}; | ||
/** | ||
@@ -20,5 +25,5 @@ * Draws a point centered at (x, y). | ||
*/ | ||
static point(x, y) { | ||
StdDraw.point = function (x, y) { | ||
StdDraw._assertXYSafeInt(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.fillRect(x, y, 1, 1); | ||
@@ -32,3 +37,3 @@ // const id = ctx.createImageData(1, 1); | ||
// ctx.putImageData(id, x, y); | ||
} | ||
}; | ||
/** | ||
@@ -43,8 +48,8 @@ * Writes the given text string in the current font, centered at (x, y). | ||
*/ | ||
static text(x, y, text) { | ||
assert(!text, 'text is not valid'); | ||
StdDraw.text = function (x, y, text) { | ||
assert_1.default(!text, 'text is not valid'); | ||
StdDraw._assertXYSafeInt(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.fillText(text, x, y); | ||
} | ||
}; | ||
/** | ||
@@ -59,7 +64,7 @@ * Draws a circle of the specified radius, centered at (x, y). | ||
*/ | ||
static circle(x, y, radius) { | ||
const { ctx } = StdDraw; | ||
StdDraw.circle = function (x, y, radius) { | ||
var ctx = StdDraw.ctx; | ||
StdDraw._circlePath(x, y, radius); | ||
ctx.stroke(); | ||
} | ||
}; | ||
/** | ||
@@ -74,7 +79,7 @@ * Draws a filled circle of the specified radius, centered at (x, y). | ||
*/ | ||
static filledCircle(x, y, radius) { | ||
const { ctx } = StdDraw; | ||
StdDraw.filledCircle = function (x, y, radius) { | ||
var ctx = StdDraw.ctx; | ||
StdDraw._circlePath(x, y, radius); | ||
ctx.fill(); | ||
} | ||
}; | ||
/** | ||
@@ -92,7 +97,7 @@ * Draws an ellipse with the specified semimajor and semiminor axes, | ||
*/ | ||
static ellipse(x, y, semiMajorAxis, semiMinorAxis) { | ||
const { ctx } = StdDraw; | ||
StdDraw.ellipse = function (x, y, semiMajorAxis, semiMinorAxis) { | ||
var ctx = StdDraw.ctx; | ||
StdDraw._ellipsePath(x, y, semiMajorAxis, semiMinorAxis); | ||
ctx.stroke(); | ||
} | ||
}; | ||
/** | ||
@@ -110,7 +115,7 @@ * Draws a filled ellipse with the specified semimajor and semiminor axes, | ||
*/ | ||
static filledEllipse(x, y, semiMajorAxis, semiMinorAxis) { | ||
const { ctx } = StdDraw; | ||
StdDraw.filledEllipse = function (x, y, semiMajorAxis, semiMinorAxis) { | ||
var ctx = StdDraw.ctx; | ||
StdDraw._ellipsePath(x, y, semiMajorAxis, semiMinorAxis); | ||
ctx.fill(); | ||
} | ||
}; | ||
/** | ||
@@ -125,11 +130,11 @@ * Draws a square of the specified size, centered at (x, y). | ||
*/ | ||
static square(x, y, halfLength) { | ||
assert(halfLength <= 0, 'halfLength must be positive number'); | ||
StdDraw.square = function (x, y, halfLength) { | ||
assert_1.default(halfLength <= 0, 'halfLength must be positive number'); | ||
StdDraw._assertXYSafeInt(x, y); | ||
const x0 = x - halfLength; | ||
const y0 = y - halfLength; | ||
const sideLength = 2 * halfLength; | ||
const { ctx } = StdDraw; | ||
var x0 = x - halfLength; | ||
var y0 = y - halfLength; | ||
var sideLength = 2 * halfLength; | ||
var ctx = StdDraw.ctx; | ||
ctx.strokeRect(x0, y0, sideLength, sideLength); | ||
} | ||
}; | ||
/** | ||
@@ -144,11 +149,11 @@ * Draws a filled square of the specified size, centered at (x, y). | ||
*/ | ||
static filledSquare(x, y, halfLength) { | ||
assert(halfLength <= 0, 'halfLength must be positive number'); | ||
StdDraw.filledSquare = function (x, y, halfLength) { | ||
assert_1.default(halfLength <= 0, 'halfLength must be positive number'); | ||
StdDraw._assertXYSafeInt(x, y); | ||
const x0 = x - halfLength; | ||
const y0 = y - halfLength; | ||
const sideLength = 2 * halfLength; | ||
const { ctx } = StdDraw; | ||
var x0 = x - halfLength; | ||
var y0 = y - halfLength; | ||
var sideLength = 2 * halfLength; | ||
var ctx = StdDraw.ctx; | ||
ctx.fillRect(x0, y0, sideLength, sideLength); | ||
} | ||
}; | ||
/** | ||
@@ -164,13 +169,13 @@ * Draws a rectangle of the specified size, centered at (x, y). | ||
*/ | ||
static rectangle(x, y, halfWidth, halfHeight) { | ||
StdDraw.rectangle = function (x, y, halfWidth, halfHeight) { | ||
StdDraw._assertXYSafeInt(x, y); | ||
assert(halfWidth <= 0, 'halfWidth must be positive number'); | ||
assert(halfHeight <= 0, 'halfHeight must be positive number'); | ||
const x0 = x - halfWidth; | ||
const y0 = y - halfHeight; | ||
const width = 2 * halfWidth; | ||
const height = 2 * halfHeight; | ||
const { ctx } = StdDraw; | ||
assert_1.default(halfWidth <= 0, 'halfWidth must be positive number'); | ||
assert_1.default(halfHeight <= 0, 'halfHeight must be positive number'); | ||
var x0 = x - halfWidth; | ||
var y0 = y - halfHeight; | ||
var width = 2 * halfWidth; | ||
var height = 2 * halfHeight; | ||
var ctx = StdDraw.ctx; | ||
ctx.strokeRect(x0, y0, width, height); | ||
} | ||
}; | ||
/** | ||
@@ -186,13 +191,13 @@ * Draws a filled rectangle of the specified size, centered at (x, y). | ||
*/ | ||
static filledRectangle(x, y, halfWidth, halfHeight) { | ||
StdDraw.filledRectangle = function (x, y, halfWidth, halfHeight) { | ||
StdDraw._assertXYSafeInt(x, y); | ||
assert(halfWidth <= 0, 'halfWidth must be positive number'); | ||
assert(halfHeight <= 0, 'halfHeight must be positive number'); | ||
const x0 = x - halfWidth; | ||
const y0 = y - halfHeight; | ||
const width = 2 * halfWidth; | ||
const height = 2 * halfHeight; | ||
const { ctx } = StdDraw; | ||
assert_1.default(halfWidth <= 0, 'halfWidth must be positive number'); | ||
assert_1.default(halfHeight <= 0, 'halfHeight must be positive number'); | ||
var x0 = x - halfWidth; | ||
var y0 = y - halfHeight; | ||
var width = 2 * halfWidth; | ||
var height = 2 * halfHeight; | ||
var ctx = StdDraw.ctx; | ||
ctx.fillRect(x0, y0, width, height); | ||
} | ||
}; | ||
/** | ||
@@ -211,7 +216,7 @@ * Draws a polygon with the vertices | ||
*/ | ||
static polygon(x, y) { | ||
StdDraw.polygon = function (x, y) { | ||
StdDraw._polygonPath(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.stroke(); | ||
} | ||
}; | ||
/** | ||
@@ -230,9 +235,9 @@ * Draws a filled polygon with the vertices | ||
*/ | ||
static filledPolygon(x, y) { | ||
StdDraw.filledPolygon = function (x, y) { | ||
StdDraw._polygonPath(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.fill(); | ||
} | ||
static setScale(x, y) { | ||
const { ctx } = StdDraw; | ||
}; | ||
StdDraw.setScale = function (x, y) { | ||
var ctx = StdDraw.ctx; | ||
StdDraw.scaleX = x; | ||
@@ -242,8 +247,8 @@ StdDraw.scaleY = -y; | ||
ctx.scale(x, -y); | ||
} | ||
static setTranslate(x, y) { | ||
}; | ||
StdDraw.setTranslate = function (x, y) { | ||
StdDraw._assertXYSafeInt(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.translate(x, y); | ||
} | ||
}; | ||
/** | ||
@@ -255,7 +260,7 @@ * Sets the pen size to the default size (0.002). | ||
*/ | ||
static setPenRadius(r) { | ||
const { ctx } = StdDraw; | ||
StdDraw.setPenRadius = function (r) { | ||
var ctx = StdDraw.ctx; | ||
ctx.lineWidth = 2 * r; | ||
ctx.lineCap = 'round'; | ||
} | ||
}; | ||
/** | ||
@@ -273,6 +278,6 @@ * Sets the pen color to the specified color. | ||
*/ | ||
static setPenColor(c) { | ||
const { ctx } = StdDraw; | ||
StdDraw.setPenColor = function (c) { | ||
var ctx = StdDraw.ctx; | ||
ctx.strokeStyle = c; | ||
} | ||
}; | ||
/** | ||
@@ -283,6 +288,6 @@ * Sets the font to the specified value. | ||
*/ | ||
static setFont(f) { | ||
const { ctx } = StdDraw; | ||
StdDraw.setFont = function (f) { | ||
var ctx = StdDraw.ctx; | ||
ctx.font = f; | ||
} | ||
}; | ||
/** | ||
@@ -295,10 +300,10 @@ * Sets the canvas (drawing area) to be 512-by-512 pixels. | ||
*/ | ||
static setCanvasSize(width, height) { | ||
StdDraw.setCanvasSize = function (width, height) { | ||
StdDraw._assertXYSafeInt(width, height); | ||
assert(width > 0, 'width must greater than 0'); | ||
assert(height > 0, 'width must greater than 0'); | ||
const { canvas } = StdDraw; | ||
assert_1.default(width > 0, 'width must greater than 0'); | ||
assert_1.default(height > 0, 'width must greater than 0'); | ||
var canvas = StdDraw.canvas; | ||
canvas.width = width; | ||
canvas.height = height; | ||
} | ||
}; | ||
/** | ||
@@ -309,44 +314,55 @@ * Clears the screen to the specified color. | ||
*/ | ||
static clear(color) { | ||
const { ctx } = StdDraw; | ||
StdDraw.clear = function (color) { | ||
var ctx = StdDraw.ctx; | ||
ctx.clearRect(0, 0, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER); | ||
ctx.fillStyle = color; | ||
ctx.fillRect(0, 0, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER); | ||
} | ||
static show(time) { } | ||
static _polygonPath(x, y) { | ||
assert(!!x, 'x-coordinate array'); | ||
assert(!!y, 'y-coordinate array'); | ||
assert(x.length !== y.length, 'arrays must be of the same length'); | ||
const { ctx } = StdDraw; | ||
}; | ||
StdDraw.show = function (time) { }; | ||
StdDraw._polygonPath = function (x, y) { | ||
var e_1, _a; | ||
assert_1.default(!!x, 'x-coordinate array'); | ||
assert_1.default(!!y, 'y-coordinate array'); | ||
assert_1.default(x.length !== y.length, 'arrays must be of the same length'); | ||
var ctx = StdDraw.ctx; | ||
ctx.beginPath(); | ||
const [[firstX, firstY], ...points] = x.map((iX, index) => [iX, y[index]]); | ||
var _b = tslib_1.__read(x.map(function (iX, index) { return [iX, y[index]]; })), _c = tslib_1.__read(_b[0], 2), firstX = _c[0], firstY = _c[1], points = _b.slice(1); | ||
ctx.moveTo(firstX, firstY); | ||
for (const [x, y] of points) { | ||
ctx.lineTo(x, y); | ||
try { | ||
for (var points_1 = tslib_1.__values(points), points_1_1 = points_1.next(); !points_1_1.done; points_1_1 = points_1.next()) { | ||
var _d = tslib_1.__read(points_1_1.value, 2), _x = _d[0], _y = _d[1]; | ||
ctx.lineTo(_x, _y); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (points_1_1 && !points_1_1.done && (_a = points_1.return)) _a.call(points_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
ctx.closePath(); | ||
} | ||
static _circlePath(x, y, radius) { | ||
assert(radius < 0, 'radius must not negative'); | ||
}; | ||
StdDraw._circlePath = function (x, y, radius) { | ||
assert_1.default(radius < 0, 'radius must not negative'); | ||
StdDraw._assertXYSafeInt(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.beginPath(); | ||
ctx.arc(x, y, radius, 0, 2 * Math.PI, true); | ||
} | ||
static _ellipsePath(x, y, semiMajorAxis, semiMinorAxis) { | ||
}; | ||
StdDraw._ellipsePath = function (x, y, semiMajorAxis, semiMinorAxis) { | ||
StdDraw._assertXYSafeInt(x, y); | ||
const { ctx } = StdDraw; | ||
var ctx = StdDraw.ctx; | ||
ctx.beginPath(); | ||
ctx.ellipse(x, y, semiMajorAxis, semiMinorAxis, 0, 0, 2 * Math.PI, true); | ||
} | ||
static getRad(degree) { | ||
}; | ||
StdDraw.getRad = function (degree) { | ||
return (degree / 180) * Math.PI; | ||
} | ||
static drawAxis() { | ||
const { ctx } = StdDraw; | ||
}; | ||
StdDraw.drawAxis = function () { | ||
var ctx = StdDraw.ctx; | ||
ctx.strokeStyle = 'black'; | ||
ctx.fillStyle = 'black'; | ||
// Translate to cartesian coordinate system | ||
const offset = 20; // Offset for coordinate axis | ||
var offset = 20; // Offset for coordinate axis | ||
ctx.save(); | ||
@@ -358,5 +374,5 @@ ctx.translate(0 + offset, StdDraw.DEFAULT_HEIGHT - offset); | ||
StdDraw.drawAxisY(); | ||
} | ||
static drawAxisX() { | ||
const { ctx } = StdDraw; | ||
}; | ||
StdDraw.drawAxisX = function () { | ||
var ctx = StdDraw.ctx; | ||
ctx.save(); | ||
@@ -366,3 +382,3 @@ ctx.lineWidth = 0.5; | ||
ctx.fillStyle = 'navy'; | ||
const width = StdDraw.DEFAULT_WIDTH - 40; | ||
var width = StdDraw.DEFAULT_WIDTH - 40; | ||
// Draw axis | ||
@@ -381,4 +397,4 @@ ctx.beginPath(); | ||
// Draw coordinates calibration | ||
let x; | ||
const y = 5; | ||
var x; | ||
var y = 5; | ||
for (x = 50; x < width; x += 50) { | ||
@@ -399,5 +415,5 @@ ctx.beginPath(); | ||
ctx.restore(); | ||
} | ||
static drawAxisY() { | ||
const { ctx } = StdDraw; | ||
}; | ||
StdDraw.drawAxisY = function () { | ||
var ctx = StdDraw.ctx; | ||
ctx.save(); | ||
@@ -407,3 +423,3 @@ ctx.lineWidth = 0.5; | ||
ctx.fillStyle = 'navy'; | ||
const height = StdDraw.DEFAULT_HEIGHT - 62; | ||
var height = StdDraw.DEFAULT_HEIGHT - 62; | ||
// Draw axis | ||
@@ -422,4 +438,4 @@ ctx.beginPath(); | ||
// Draw coordinates calibration | ||
let y; | ||
let x = 5; | ||
var y; | ||
var x = 5; | ||
for (y = 50; y < height; y += 50) { | ||
@@ -442,3 +458,3 @@ ctx.beginPath(); | ||
ctx.restore(); | ||
} | ||
}; | ||
/** | ||
@@ -449,8 +465,8 @@ * Create new canvas, container element must exist | ||
*/ | ||
static createNewCanvas(containerId, canvasId) { | ||
StdDraw.createNewCanvas = function (containerId, canvasId) { | ||
if (document.getElementById(canvasId)) { | ||
throw new Error(`Canvas(${canvasId}) already exist`); | ||
throw new Error("Canvas(" + canvasId + ") already exist"); | ||
} | ||
if (!document.getElementById(containerId)) { | ||
throw new Error(`Canvas container(${containerId}) must exist`); | ||
throw new Error("Canvas container(" + containerId + ") must exist"); | ||
} | ||
@@ -460,5 +476,7 @@ StdDraw.canvas = StdDraw._createCanvas(containerId, canvasId); | ||
StdDraw.drawAxis(); | ||
} | ||
static _createCanvas(containerId = 'CanvasContainer', canvasId = 'StdCanvas') { | ||
let _canvas = window.document.getElementById(canvasId); | ||
}; | ||
StdDraw._createCanvas = function (containerId, canvasId) { | ||
if (containerId === void 0) { containerId = 'CanvasContainer'; } | ||
if (canvasId === void 0) { canvasId = 'StdCanvas'; } | ||
var _canvas = window.document.getElementById(canvasId); | ||
if (!_canvas) { | ||
@@ -473,5 +491,5 @@ _canvas = document.createElement('canvas'); | ||
_canvas.style.border = '1px solid #eee'; | ||
const canvasContainer = document.getElementById(containerId); | ||
var canvasContainer = document.getElementById(containerId); | ||
if (!canvasContainer) { | ||
throw new Error(`${containerId} element not found`); | ||
throw new Error(containerId + " element not found"); | ||
} | ||
@@ -481,5 +499,5 @@ canvasContainer.appendChild(_canvas); | ||
return _canvas; | ||
} | ||
static _createContext() { | ||
const _ctx = StdDraw.canvas.getContext('2d'); | ||
}; | ||
StdDraw._createContext = function () { | ||
var _ctx = StdDraw.canvas.getContext('2d'); | ||
if (!_ctx) { | ||
@@ -493,17 +511,19 @@ throw new Error('Canvas element not found'); | ||
return _ctx; | ||
} | ||
static _assertXYSafeInt(x, y) { | ||
assert(!Number.isSafeInteger(x) || !Number.isSafeInteger(y), 'x or y is either NaN or infinite'); | ||
} | ||
} | ||
StdDraw.scaleX = 1; | ||
StdDraw.scaleY = -1; | ||
StdDraw.DEFAULT_WIDTH = 512; | ||
StdDraw.DEFAULT_HEIGHT = 512; | ||
StdDraw.canvas = StdDraw._createCanvas(); | ||
StdDraw.ctx = StdDraw._createContext(); | ||
/** @see https://www.colorhexa.com/ */ | ||
StdDraw.DARK_GRAY = '#a9a9a9'; | ||
StdDraw.RED = '#ff0000'; | ||
}; | ||
StdDraw._assertXYSafeInt = function (x, y) { | ||
assert_1.default(!Number.isSafeInteger(x) || !Number.isSafeInteger(y), 'x or y is either NaN or infinite'); | ||
}; | ||
StdDraw.scaleX = 1; | ||
StdDraw.scaleY = -1; | ||
StdDraw.DEFAULT_WIDTH = 512; | ||
StdDraw.DEFAULT_HEIGHT = 512; | ||
StdDraw.canvas = StdDraw._createCanvas(); | ||
StdDraw.ctx = StdDraw._createContext(); | ||
/** @see https://www.colorhexa.com/ */ | ||
StdDraw.DARK_GRAY = '#a9a9a9'; | ||
StdDraw.RED = '#ff0000'; | ||
return StdDraw; | ||
}()); | ||
exports.default = StdDraw; | ||
StdDraw.drawAxis(); | ||
//# sourceMappingURL=index.js.map |
/** | ||
* The {@code StdIn} class provides static methods for reading strings | ||
* The `StdIn` class provides static methods for reading strings | ||
* and numbers from standard input. | ||
@@ -18,3 +18,3 @@ * These functions fall into one of four categories: | ||
* <b>Getting started.</b> | ||
* To use this class, you must have {@code StdIn.class} in your | ||
* To use this class, you must have `StdIn.class` in your | ||
* Java classpath. If you used our autoinstaller, you should be all set. | ||
@@ -101,5 +101,5 @@ * Otherwise, either download | ||
* A <em>line separator</em> is defined to be one of the following strings: | ||
* {@code \n} (Linux), {@code \r} (old Macintosh), | ||
* {@code \r\n} (Windows), | ||
* {@code \}{@code u2028}, {@code \}{@code u2029}, or {@code \}{@code u0085}. | ||
* `\n` (Linux), `\r` (old Macintosh), | ||
* `\r\n` (Windows), | ||
* `\}`u2028`, `\}`u2029`, or `\``u0085`. | ||
* <p> | ||
@@ -130,6 +130,6 @@ * As an example, the following code fragment reads text from standard input, | ||
* the specified type, as in the corresponding | ||
* {@code readDouble}, {@code readInt}, and {@code readString()} methods. | ||
* The {@code readAllLines()} method reads all remaining lines on standard | ||
* `readDouble`, `readInt`, and `readString()` methods. | ||
* The `readAllLines()` method reads all remaining lines on standard | ||
* input and returns them as an array of strings. | ||
* The {@code readAll()} method reads all remaining input on standard | ||
* The `readAll()` method reads all remaining input on standard | ||
* input and returns it as a string. | ||
@@ -144,7 +144,7 @@ * <p> | ||
* <b>Differences with Scanner.</b> | ||
* {@code StdIn} and {@link Scanner} are both designed to parse | ||
* `StdIn` and {@link Scanner} are both designed to parse | ||
* tokens and convert them to primitive types and strings. | ||
* The main differences are summarized below: | ||
* <ul> | ||
* <li> {@code StdIn} is a set of static methods and reads | ||
* <li> `StdIn` is a set of static methods and reads | ||
* reads input from only standard input. It is suitable for use before | ||
@@ -155,11 +155,11 @@ * a programmer knows about objects. | ||
* and sockets. | ||
* <li> {@code StdIn} uses whitespace as the delimiter pattern | ||
* <li> `StdIn` uses whitespace as the delimiter pattern | ||
* that separates tokens. | ||
* {@link Scanner} supports arbitrary delimiter patterns. | ||
* <li> {@code StdIn} coerces the character-set encoding to UTF-8, | ||
* <li> `StdIn` coerces the character-set encoding to UTF-8, | ||
* which is the most widely used character encoding for Unicode. | ||
* <li> {@code StdIn} coerces the locale to {@link Locale#US}, | ||
* <li> `StdIn` coerces the locale to {@link Locale#US}, | ||
* for consistency with {@link StdOut}, {@link Double#parseDouble(String)}, | ||
* and floating-point literals. | ||
* <li> {@code StdIn} has convenient methods for reading a single | ||
* <li> `StdIn` has convenient methods for reading a single | ||
* character; reading in sequences of integers, doubles, or strings; | ||
@@ -169,4 +169,4 @@ * and reading in all of the remaining input. | ||
* <p> | ||
* Historical note: {@code StdIn} preceded {@code Scanner}; when | ||
* {@code Scanner} was introduced, this class was re-implemented to use {@code Scanner}. | ||
* Historical note: `StdIn` preceded `Scanner`; when | ||
* `Scanner` was introduced, this class was re-implemented to use `Scanner`. | ||
* <p> | ||
@@ -176,3 +176,3 @@ * <b>Using standard input.</b> | ||
* Windows, and Linux. | ||
* The methods in {@code StdIn} are <em>blocking</em>, which means that they | ||
* The methods in `StdIn` are <em>blocking</em>, which means that they | ||
* will wait until you enter input on standard input. | ||
@@ -182,3 +182,3 @@ * If your program has a loop that repeats until standard input is empty, | ||
* To do so, depending on your operating system and IDE, | ||
* use either {@code <Ctrl-d>} or {@code <Ctrl-z>}, on its own line. | ||
* use either `<Ctrl-d>` or `<Ctrl-z>`, on its own line. | ||
* If you are redirecting standard input from a file, you will not need | ||
@@ -190,4 +190,4 @@ * to do anything to signal that the input is finished. | ||
* <a href = "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058">byte-order mask</a>. | ||
* If the input begins with the optional byte-order mask, {@code StdIn} | ||
* will have an extra character {@code \}{@code uFEFF} at the beginning. | ||
* If the input begins with the optional byte-order mask, `StdIn` | ||
* will have an extra character `\``uFEFF` at the beginning. | ||
* <p> | ||
@@ -203,5 +203,7 @@ * <b>Reference.</b> | ||
private static rl; | ||
private static doesStreamEnd; | ||
private static inputLines; | ||
private static nextIndex; | ||
private static _initialize; | ||
static init(fileName?: string): void; | ||
/** | ||
@@ -212,4 +214,4 @@ * Returns true if standard input is empty (except possibly for whitespace). | ||
* | ||
* @return {@code true} if standard input is empty (except possibly | ||
* for whitespace); {@code false} otherwise | ||
* @return `true` if standard input is empty (except possibly | ||
* for whitespace); `false` otherwise | ||
*/ | ||
@@ -223,4 +225,4 @@ static isEmpty(): boolean; | ||
* | ||
* @return {@code true} if standard input has more input (including whitespace); | ||
* {@code false} otherwise | ||
* @return `true` if standard input has more input (including whitespace); | ||
* `false` otherwise | ||
*/ | ||
@@ -233,3 +235,3 @@ static hasNextLine(): boolean; | ||
* | ||
* {@code false} otherwise | ||
* `false` otherwise | ||
*/ | ||
@@ -241,5 +243,5 @@ static hasNextChar(): void; | ||
* @return the next line, excluding the line separator if present; | ||
* {@code null} if no such line | ||
* `null` if no such line | ||
*/ | ||
static readLine(): string; | ||
static readLine(): Promise<string>; | ||
/** | ||
@@ -249,3 +251,3 @@ * Reads and returns the next character. | ||
* | ||
* @return the next {@code char} | ||
* @return the next `char` | ||
* @throws Error if standard input is empty | ||
@@ -260,9 +262,9 @@ */ | ||
*/ | ||
static readAll(): string; | ||
static readAll(): Promise<string>; | ||
/** | ||
* Reads the next token and returns the {@code String}. | ||
* @return the next {@code String} | ||
* Reads the next token and returns the `String`. | ||
* @return the next `String` | ||
* @throws Error if standard input is empty | ||
*/ | ||
static readString(): string; | ||
static readString(): Promise<string>; | ||
/** | ||
@@ -272,5 +274,5 @@ * Reads the next token from standard input, parses it as an integer, and returns the integer. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as an {@code int} | ||
* @throws InputMismatchException if the next token cannot be parsed as an `int` | ||
*/ | ||
static readInt(): number; | ||
static readInt(): Promise<number>; | ||
/** | ||
@@ -281,5 +283,5 @@ * Reads the next token from standard input, parses it as a double, and returns the double. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code double} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `double` | ||
*/ | ||
static readDouble(): number; | ||
static readDouble(): Promise<number>; | ||
/** | ||
@@ -290,5 +292,5 @@ * Reads the next token from standard input, parses it as a float, and returns the float. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code float} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `float` | ||
*/ | ||
static readFloat(): number; | ||
static readFloat(): Promise<number>; | ||
/** | ||
@@ -299,5 +301,5 @@ * Reads the next token from standard input, parses it as a long integer, and returns the long integer. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code long} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `long` | ||
*/ | ||
static readLong(): number; | ||
static readLong(): Promise<number>; | ||
/** | ||
@@ -308,5 +310,5 @@ * Reads the next token from standard input, parses it as a short integer, and returns the short integer. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code short} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `short` | ||
*/ | ||
static readShort(): number; | ||
static readShort(): Promise<number>; | ||
/** | ||
@@ -317,5 +319,5 @@ * Reads the next token from standard input, parses it as a byte, and returns the byte. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code byte} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `byte` | ||
*/ | ||
static readByte(): number; | ||
static readByte(): Promise<number>; | ||
/** | ||
@@ -327,4 +329,4 @@ * Reads the next token from standard input, parses it as a boolean, | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code boolean}: | ||
* {@code true} or {@code 1} for true, and {@code false} or {@code 0} for false, | ||
* @throws InputMismatchException if the next token cannot be parsed as a `boolean`: | ||
* `true` or `1` for true, and `false` or `0` for false, | ||
* ignoring case | ||
@@ -338,3 +340,3 @@ */ | ||
*/ | ||
static readAllStrings(): string[]; | ||
static readAllStrings(): Promise<string[]>; | ||
/** | ||
@@ -344,3 +346,3 @@ * Reads all remaining lines from standard input and returns them as an array of strings. | ||
*/ | ||
static readAllLines(): string[]; | ||
static readAllLines(): Promise<string[]>; | ||
/** | ||
@@ -350,5 +352,5 @@ * Reads all remaining tokens from standard input, parses them as integers, and returns | ||
* @return all remaining integers on standard input, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as an {@code int} | ||
* @throws InputMismatchException if any token cannot be parsed as an `int` | ||
*/ | ||
static readAllInts(): number[]; | ||
static readAllInts(): Promise<number[]>; | ||
/** | ||
@@ -358,5 +360,5 @@ * Reads all remaining tokens from standard input, parses them as longs, and returns | ||
* @return all remaining longs on standard input, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as a {@code long} | ||
* @throws InputMismatchException if any token cannot be parsed as a `long` | ||
*/ | ||
static readAllLongs(): number[]; | ||
static readAllLongs(): Promise<number[]>; | ||
/** | ||
@@ -366,5 +368,5 @@ * Reads all remaining tokens from standard input, parses them as doubles, and returns | ||
* @return all remaining doubles on standard input, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as a {@code double} | ||
* @throws InputMismatchException if any token cannot be parsed as a `double` | ||
*/ | ||
static readAllDoubles(): number[]; | ||
static readAllDoubles(): Promise<number[]>; | ||
/** | ||
@@ -374,6 +376,6 @@ * Reads all remaining tokens, parses them as integers, and returns | ||
* @return all remaining integers, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as an {@code int} | ||
* @throws InputMismatchException if any token cannot be parsed as an `int` | ||
* @deprecated Replaced by {@link #readAllInts()}. | ||
*/ | ||
static readInts(): number[]; | ||
static readInts(): Promise<number[]>; | ||
/** | ||
@@ -383,6 +385,6 @@ * Reads all remaining tokens, parses them as doubles, and returns | ||
* @return all remaining doubles, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as a {@code double} | ||
* @throws InputMismatchException if any token cannot be parsed as a `double` | ||
* @deprecated Replaced by {@link #readAllDoubles()}. | ||
*/ | ||
static readDoubles(): number[]; | ||
static readDoubles(): Promise<number[]>; | ||
/** | ||
@@ -393,3 +395,3 @@ * Reads all remaining tokens and returns them as an array of strings. | ||
*/ | ||
static readStrings(): string[]; | ||
static readStrings(): Promise<string[]>; | ||
} |
@@ -1,5 +0,11 @@ | ||
import readline from 'readline'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var fs_1 = tslib_1.__importDefault(require("fs")); | ||
var readline_1 = tslib_1.__importDefault(require("readline")); | ||
var debug_1 = tslib_1.__importDefault(require("debug")); | ||
// import StdOut from '../StdOut'; | ||
var d = debug_1.default('algs4:StdIn'); | ||
/** | ||
* The {@code StdIn} class provides static methods for reading strings | ||
* The `StdIn` class provides static methods for reading strings | ||
* and numbers from standard input. | ||
@@ -20,3 +26,3 @@ * These functions fall into one of four categories: | ||
* <b>Getting started.</b> | ||
* To use this class, you must have {@code StdIn.class} in your | ||
* To use this class, you must have `StdIn.class` in your | ||
* Java classpath. If you used our autoinstaller, you should be all set. | ||
@@ -103,5 +109,5 @@ * Otherwise, either download | ||
* A <em>line separator</em> is defined to be one of the following strings: | ||
* {@code \n} (Linux), {@code \r} (old Macintosh), | ||
* {@code \r\n} (Windows), | ||
* {@code \}{@code u2028}, {@code \}{@code u2029}, or {@code \}{@code u0085}. | ||
* `\n` (Linux), `\r` (old Macintosh), | ||
* `\r\n` (Windows), | ||
* `\}`u2028`, `\}`u2029`, or `\``u0085`. | ||
* <p> | ||
@@ -132,6 +138,6 @@ * As an example, the following code fragment reads text from standard input, | ||
* the specified type, as in the corresponding | ||
* {@code readDouble}, {@code readInt}, and {@code readString()} methods. | ||
* The {@code readAllLines()} method reads all remaining lines on standard | ||
* `readDouble`, `readInt`, and `readString()` methods. | ||
* The `readAllLines()` method reads all remaining lines on standard | ||
* input and returns them as an array of strings. | ||
* The {@code readAll()} method reads all remaining input on standard | ||
* The `readAll()` method reads all remaining input on standard | ||
* input and returns it as a string. | ||
@@ -146,7 +152,7 @@ * <p> | ||
* <b>Differences with Scanner.</b> | ||
* {@code StdIn} and {@link Scanner} are both designed to parse | ||
* `StdIn` and {@link Scanner} are both designed to parse | ||
* tokens and convert them to primitive types and strings. | ||
* The main differences are summarized below: | ||
* <ul> | ||
* <li> {@code StdIn} is a set of static methods and reads | ||
* <li> `StdIn` is a set of static methods and reads | ||
* reads input from only standard input. It is suitable for use before | ||
@@ -157,11 +163,11 @@ * a programmer knows about objects. | ||
* and sockets. | ||
* <li> {@code StdIn} uses whitespace as the delimiter pattern | ||
* <li> `StdIn` uses whitespace as the delimiter pattern | ||
* that separates tokens. | ||
* {@link Scanner} supports arbitrary delimiter patterns. | ||
* <li> {@code StdIn} coerces the character-set encoding to UTF-8, | ||
* <li> `StdIn` coerces the character-set encoding to UTF-8, | ||
* which is the most widely used character encoding for Unicode. | ||
* <li> {@code StdIn} coerces the locale to {@link Locale#US}, | ||
* <li> `StdIn` coerces the locale to {@link Locale#US}, | ||
* for consistency with {@link StdOut}, {@link Double#parseDouble(String)}, | ||
* and floating-point literals. | ||
* <li> {@code StdIn} has convenient methods for reading a single | ||
* <li> `StdIn` has convenient methods for reading a single | ||
* character; reading in sequences of integers, doubles, or strings; | ||
@@ -171,4 +177,4 @@ * and reading in all of the remaining input. | ||
* <p> | ||
* Historical note: {@code StdIn} preceded {@code Scanner}; when | ||
* {@code Scanner} was introduced, this class was re-implemented to use {@code Scanner}. | ||
* Historical note: `StdIn` preceded `Scanner`; when | ||
* `Scanner` was introduced, this class was re-implemented to use `Scanner`. | ||
* <p> | ||
@@ -178,3 +184,3 @@ * <b>Using standard input.</b> | ||
* Windows, and Linux. | ||
* The methods in {@code StdIn} are <em>blocking</em>, which means that they | ||
* The methods in `StdIn` are <em>blocking</em>, which means that they | ||
* will wait until you enter input on standard input. | ||
@@ -184,3 +190,3 @@ * If your program has a loop that repeats until standard input is empty, | ||
* To do so, depending on your operating system and IDE, | ||
* use either {@code <Ctrl-d>} or {@code <Ctrl-z>}, on its own line. | ||
* use either `<Ctrl-d>` or `<Ctrl-z>`, on its own line. | ||
* If you are redirecting standard input from a file, you will not need | ||
@@ -192,4 +198,4 @@ * to do anything to signal that the input is finished. | ||
* <a href = "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058">byte-order mask</a>. | ||
* If the input begins with the optional byte-order mask, {@code StdIn} | ||
* will have an extra character {@code \}{@code uFEFF} at the beginning. | ||
* If the input begins with the optional byte-order mask, `StdIn` | ||
* will have an extra character `\``uFEFF` at the beginning. | ||
* <p> | ||
@@ -203,3 +209,29 @@ * <b>Reference.</b> | ||
*/ | ||
export default class StdIn { | ||
var StdIn = /** @class */ (function () { | ||
function StdIn() { | ||
} | ||
StdIn.init = function (fileName) { | ||
if (fileName) { | ||
StdIn.rl = readline_1.default.createInterface({ | ||
input: fs_1.default.createReadStream(fileName), | ||
crlfDelay: Infinity, | ||
}); | ||
} | ||
StdIn.doesStreamEnd = false; | ||
StdIn.inputLines = []; | ||
StdIn.nextIndex = 0; | ||
StdIn.rl.on('line', function (line) { | ||
var _a; | ||
d('line: %s', line); | ||
(_a = StdIn.inputLines).push.apply(_a, tslib_1.__spread(line.split(/\s/))); | ||
}); | ||
StdIn.rl.on('close', function () { | ||
d('input stream closed normally'); | ||
StdIn.doesStreamEnd = true; | ||
}); | ||
StdIn.rl.on('SIGINT', function () { | ||
d('input stream closed by SIGINT'); | ||
StdIn.doesStreamEnd = true; | ||
}); | ||
}; | ||
/** | ||
@@ -210,8 +242,8 @@ * Returns true if standard input is empty (except possibly for whitespace). | ||
* | ||
* @return {@code true} if standard input is empty (except possibly | ||
* for whitespace); {@code false} otherwise | ||
* @return `true` if standard input is empty (except possibly | ||
* for whitespace); `false` otherwise | ||
*/ | ||
static isEmpty() { | ||
StdIn.isEmpty = function () { | ||
return StdIn.nextIndex >= StdIn.inputLines.length; | ||
} | ||
}; | ||
/** | ||
@@ -223,8 +255,8 @@ * Returns true if standard input has a next line. | ||
* | ||
* @return {@code true} if standard input has more input (including whitespace); | ||
* {@code false} otherwise | ||
* @return `true` if standard input has more input (including whitespace); | ||
* `false` otherwise | ||
*/ | ||
static hasNextLine() { | ||
StdIn.hasNextLine = function () { | ||
return StdIn.nextIndex < StdIn.inputLines.length; | ||
} | ||
}; | ||
/** | ||
@@ -235,7 +267,7 @@ * Returns true if standard input has more input (including whitespace). | ||
* | ||
* {@code false} otherwise | ||
* `false` otherwise | ||
*/ | ||
static hasNextChar() { | ||
StdIn.hasNextChar = function () { | ||
throw new Error('No implemented'); | ||
} | ||
}; | ||
/** | ||
@@ -245,12 +277,25 @@ * Reads and returns the next line, excluding the line separator if present. | ||
* @return the next line, excluding the line separator if present; | ||
* {@code null} if no such line | ||
* `null` if no such line | ||
*/ | ||
static readLine() { | ||
if (StdIn.isEmpty()) { | ||
return null; | ||
} | ||
const line = StdIn.inputLines[StdIn.nextIndex]; | ||
StdIn.nextIndex++; | ||
return line; | ||
} | ||
StdIn.readLine = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var line; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!StdIn.isEmpty()) return [3 /*break*/, 2]; | ||
// Wait a short time to read lines | ||
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 50); })]; | ||
case 1: | ||
// Wait a short time to read lines | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: | ||
line = StdIn.inputLines[StdIn.nextIndex]; | ||
StdIn.nextIndex++; | ||
return [2 /*return*/, line]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -260,8 +305,8 @@ * Reads and returns the next character. | ||
* | ||
* @return the next {@code char} | ||
* @return the next `char` | ||
* @throws Error if standard input is empty | ||
*/ | ||
static readChar() { | ||
StdIn.readChar = function () { | ||
throw new Error('No implemented'); | ||
} | ||
}; | ||
/** | ||
@@ -273,13 +318,27 @@ * Reads and returns the remainder of the input, as a string. | ||
*/ | ||
static readAll() { | ||
return StdIn.readAllStrings().join(''); | ||
} | ||
StdIn.readAll = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var s; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, StdIn.readAllStrings()]; | ||
case 1: | ||
s = _a.sent(); | ||
return [2 /*return*/, s.join('')]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Reads the next token and returns the {@code String}. | ||
* @return the next {@code String} | ||
* Reads the next token and returns the `String`. | ||
* @return the next `String` | ||
* @throws Error if standard input is empty | ||
*/ | ||
static readString() { | ||
return StdIn.readLine(); | ||
} | ||
StdIn.readString = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, StdIn.readLine()]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -289,7 +348,17 @@ * Reads the next token from standard input, parses it as an integer, and returns the integer. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as an {@code int} | ||
* @throws InputMismatchException if the next token cannot be parsed as an `int` | ||
*/ | ||
static readInt() { | ||
return parseInt(StdIn.readLine(), 10); | ||
} | ||
StdIn.readInt = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_a = parseInt; | ||
return [4 /*yield*/, StdIn.readLine()]; | ||
case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent(), 10])]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -300,7 +369,11 @@ * Reads the next token from standard input, parses it as a double, and returns the double. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code double} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `double` | ||
*/ | ||
static readDouble() { | ||
return StdIn.readInt(); | ||
} | ||
StdIn.readDouble = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, StdIn.readInt()]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -311,7 +384,11 @@ * Reads the next token from standard input, parses it as a float, and returns the float. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code float} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `float` | ||
*/ | ||
static readFloat() { | ||
return StdIn.readInt(); | ||
} | ||
StdIn.readFloat = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, StdIn.readInt()]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -322,7 +399,11 @@ * Reads the next token from standard input, parses it as a long integer, and returns the long integer. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code long} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `long` | ||
*/ | ||
static readLong() { | ||
return StdIn.readInt(); | ||
} | ||
StdIn.readLong = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, StdIn.readInt()]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -333,7 +414,11 @@ * Reads the next token from standard input, parses it as a short integer, and returns the short integer. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code short} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `short` | ||
*/ | ||
static readShort() { | ||
return StdIn.readInt(); | ||
} | ||
StdIn.readShort = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, StdIn.readInt()]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -344,7 +429,11 @@ * Reads the next token from standard input, parses it as a byte, and returns the byte. | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code byte} | ||
* @throws InputMismatchException if the next token cannot be parsed as a `byte` | ||
*/ | ||
static readByte() { | ||
return StdIn.readInt(); | ||
} | ||
StdIn.readByte = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
return [2 /*return*/, StdIn.readInt()]; | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -356,9 +445,9 @@ * Reads the next token from standard input, parses it as a boolean, | ||
* @throws Error if standard input is empty | ||
* @throws InputMismatchException if the next token cannot be parsed as a {@code boolean}: | ||
* {@code true} or {@code 1} for true, and {@code false} or {@code 0} for false, | ||
* @throws InputMismatchException if the next token cannot be parsed as a `boolean`: | ||
* `true` or `1` for true, and `false` or `0` for false, | ||
* ignoring case | ||
*/ | ||
static readBoolean() { | ||
StdIn.readBoolean = function () { | ||
return Boolean(StdIn.readLine()); | ||
} | ||
}; | ||
/** | ||
@@ -369,11 +458,26 @@ * Reads all remaining tokens from standard input and returns them as an array of strings. | ||
*/ | ||
static readAllStrings() { | ||
if (StdIn.isEmpty()) { | ||
throw new Error('input is empty'); | ||
} | ||
const { inputLines, nextIndex } = StdIn; | ||
const val = inputLines.slice(nextIndex); | ||
StdIn.nextIndex = StdIn.inputLines.length - 1; | ||
return val; | ||
} | ||
StdIn.readAllStrings = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var inputLines, nextIndex, val; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!!StdIn.doesStreamEnd) return [3 /*break*/, 2]; | ||
// Wait a short time to read lines | ||
return [4 /*yield*/, new Promise(function (resolve) { return setTimeout(resolve, 250); })]; | ||
case 1: | ||
// Wait a short time to read lines | ||
_a.sent(); | ||
d('sleep'); | ||
return [2 /*return*/, StdIn.readAllStrings()]; | ||
case 2: | ||
d('>>> Input stream ended!'); | ||
inputLines = StdIn.inputLines, nextIndex = StdIn.nextIndex; | ||
val = inputLines.slice(nextIndex); | ||
StdIn.nextIndex = StdIn.inputLines.length - 1; | ||
return [2 /*return*/, val]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -383,5 +487,5 @@ * Reads all remaining lines from standard input and returns them as an array of strings. | ||
*/ | ||
static readAllLines() { | ||
StdIn.readAllLines = function () { | ||
return StdIn.readAllStrings(); | ||
} | ||
}; | ||
/** | ||
@@ -391,8 +495,17 @@ * Reads all remaining tokens from standard input, parses them as integers, and returns | ||
* @return all remaining integers on standard input, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as an {@code int} | ||
* @throws InputMismatchException if any token cannot be parsed as an `int` | ||
*/ | ||
static readAllInts() { | ||
const fields = StdIn.readAllStrings(); | ||
return fields.map((field) => parseInt(field, 10)); | ||
} | ||
StdIn.readAllInts = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var fields; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, StdIn.readAllStrings()]; | ||
case 1: | ||
fields = _a.sent(); | ||
return [2 /*return*/, fields.map(function (field) { return parseInt(field, 10); })]; | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -402,7 +515,7 @@ * Reads all remaining tokens from standard input, parses them as longs, and returns | ||
* @return all remaining longs on standard input, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as a {@code long} | ||
* @throws InputMismatchException if any token cannot be parsed as a `long` | ||
*/ | ||
static readAllLongs() { | ||
StdIn.readAllLongs = function () { | ||
return StdIn.readAllInts(); | ||
} | ||
}; | ||
/** | ||
@@ -412,7 +525,7 @@ * Reads all remaining tokens from standard input, parses them as doubles, and returns | ||
* @return all remaining doubles on standard input, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as a {@code double} | ||
* @throws InputMismatchException if any token cannot be parsed as a `double` | ||
*/ | ||
static readAllDoubles() { | ||
StdIn.readAllDoubles = function () { | ||
return StdIn.readAllInts(); | ||
} | ||
}; | ||
/** | ||
@@ -422,8 +535,8 @@ * Reads all remaining tokens, parses them as integers, and returns | ||
* @return all remaining integers, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as an {@code int} | ||
* @throws InputMismatchException if any token cannot be parsed as an `int` | ||
* @deprecated Replaced by {@link #readAllInts()}. | ||
*/ | ||
static readInts() { | ||
StdIn.readInts = function () { | ||
return StdIn.readAllInts(); | ||
} | ||
}; | ||
/** | ||
@@ -433,8 +546,8 @@ * Reads all remaining tokens, parses them as doubles, and returns | ||
* @return all remaining doubles, as an array | ||
* @throws InputMismatchException if any token cannot be parsed as a {@code double} | ||
* @throws InputMismatchException if any token cannot be parsed as a `double` | ||
* @deprecated Replaced by {@link #readAllDoubles()}. | ||
*/ | ||
static readDoubles() { | ||
StdIn.readDoubles = function () { | ||
return StdIn.readAllDoubles(); | ||
} | ||
}; | ||
/** | ||
@@ -445,16 +558,16 @@ * Reads all remaining tokens and returns them as an array of strings. | ||
*/ | ||
static readStrings() { | ||
StdIn.readStrings = function () { | ||
return StdIn.readAllStrings(); | ||
} | ||
} | ||
StdIn.rl = readline.createInterface({ | ||
input: process.stdin, | ||
}); | ||
StdIn.inputLines = []; | ||
StdIn.nextIndex = 0; | ||
StdIn._initialize = (() => { | ||
StdIn.rl.on('line', (line) => { | ||
StdIn.inputLines.push(line); | ||
}; | ||
StdIn.rl = readline_1.default.createInterface({ | ||
input: process.stdin, | ||
crlfDelay: Infinity, | ||
}); | ||
})(); | ||
StdIn.doesStreamEnd = false; | ||
StdIn.inputLines = []; | ||
StdIn.nextIndex = 0; | ||
StdIn._initialize = StdIn.init(); | ||
return StdIn; | ||
}()); | ||
exports.default = StdIn; | ||
//# sourceMappingURL=index.js.map |
export default class StdOut { | ||
/** | ||
* Print without newline | ||
* @param s | ||
*/ | ||
static print(s: any): void; | ||
static println(s?: any): void; | ||
static println(s?: string): void; | ||
static printf(format: string, ...args: any[]): void; | ||
} |
@@ -1,14 +0,30 @@ | ||
import printf from 'printf'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var printf_1 = tslib_1.__importDefault(require("printf")); | ||
/* eslint-disable no-console */ | ||
export default class StdOut { | ||
static print(s) { | ||
console.log(s); | ||
var StdOut = /** @class */ (function () { | ||
function StdOut() { | ||
} | ||
static println(s) { | ||
console.log(`${s}`); | ||
} | ||
static printf(format, ...args) { | ||
console.log(printf(format, ...args)); | ||
} | ||
} | ||
/** | ||
* Print without newline | ||
* @param s | ||
*/ | ||
StdOut.print = function (s) { | ||
process.stdout.write("" + s); | ||
}; | ||
StdOut.println = function (s) { | ||
if (s === void 0) { s = ''; } | ||
console.log("" + s); | ||
}; | ||
StdOut.printf = function (format) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
printf_1.default.apply(void 0, tslib_1.__spread([process.stdout, format], args)); | ||
}; | ||
return StdOut; | ||
}()); | ||
exports.default = StdOut; | ||
//# sourceMappingURL=index.js.map |
@@ -0,0 +0,0 @@ import { prng } from 'seedrandom'; |
@@ -1,3 +0,8 @@ | ||
import seedRandom from 'seedrandom'; | ||
export default class StdRandom { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var seedrandom_1 = tslib_1.__importDefault(require("seedrandom")); | ||
var StdRandom = /** @class */ (function () { | ||
function StdRandom() { | ||
} | ||
/** | ||
@@ -11,6 +16,6 @@ * Sets the seed of the pseudo-random number generator. | ||
*/ | ||
static setSeed(seed) { | ||
StdRandom.setSeed = function (seed) { | ||
StdRandom._seed = seed; | ||
StdRandom._random = seedRandom(String(StdRandom._seed)); | ||
} | ||
StdRandom._random = seedrandom_1.default(String(StdRandom._seed)); | ||
}; | ||
/** | ||
@@ -21,5 +26,5 @@ * Returns a random real number uniformly in [0, 1). | ||
*/ | ||
static random() { | ||
StdRandom.random = function () { | ||
return StdRandom._random.double(); | ||
} | ||
}; | ||
/** | ||
@@ -31,5 +36,5 @@ * Get random number from a to b(not included), [a, b). | ||
*/ | ||
static uniform(a, b) { | ||
StdRandom.uniform = function (a, b) { | ||
if (a && b && (b <= a || b - a >= Number.MAX_VALUE)) { | ||
throw new Error(`invalid range: [${a}, ${b})`); | ||
throw new Error("invalid range: [" + a + ", " + b + ")"); | ||
} | ||
@@ -43,3 +48,3 @@ if (typeof a === 'number' && typeof b === 'number') { | ||
return StdRandom.random(); | ||
} | ||
}; | ||
/** | ||
@@ -50,8 +55,8 @@ * Returns a random boolean from a Bernoulli distribution with success | ||
*/ | ||
static bernoulli(p) { | ||
StdRandom.bernoulli = function (p) { | ||
if (p < 0 || p > 1) { | ||
throw new Error(`probability p must be between 0.0 and 1.0: ${p}`); | ||
throw new Error("probability p must be between 0.0 and 1.0: " + p); | ||
} | ||
return StdRandom.uniform() < p; | ||
} | ||
}; | ||
/** | ||
@@ -65,6 +70,6 @@ * Returns a random real number from a Gaussian distribution with mean μ | ||
*/ | ||
static gaussian(mu, sigma) { | ||
let r; | ||
let x; | ||
let y; | ||
StdRandom.gaussian = function (mu, sigma) { | ||
var r; | ||
var x; | ||
var y; | ||
do { | ||
@@ -74,4 +79,4 @@ x = StdRandom.uniform(-1.0, 1.0); | ||
r = x * x + y * y; | ||
} while (r >= 1 || r == 0); | ||
const result = x * Math.sqrt((-2 * Math.log(r)) / r); | ||
} while (r >= 1 || r === 0); | ||
var result = x * Math.sqrt((-2 * Math.log(r)) / r); | ||
if (typeof mu === 'number' && typeof sigma === 'number') { | ||
@@ -81,3 +86,3 @@ return mu + sigma * result; | ||
return result; | ||
} | ||
}; | ||
/** | ||
@@ -93,20 +98,20 @@ * Returns a random integer from the specified discrete distribution. | ||
*/ | ||
static discrete(probabilities) { | ||
StdRandom.discrete = function (probabilities) { | ||
if (!probabilities) | ||
throw new Error('argument array is null or undefined'); | ||
const EPSILON = 1.0e-14; | ||
let sum = 0.0; | ||
for (let i = 0; i < probabilities.length; i++) { | ||
var EPSILON = 1.0e-14; | ||
var sum = 0.0; | ||
for (var i = 0; i < probabilities.length; i++) { | ||
if (!(probabilities[i] >= 0.0)) | ||
throw new Error(`array entry ${i} must be nonnegative: ${probabilities[i]}`); | ||
throw new Error("array entry " + i + " must be nonnegative: " + probabilities[i]); | ||
sum += probabilities[i]; | ||
} | ||
if (sum > 1.0 + EPSILON || sum < 1.0 - EPSILON) | ||
throw new Error(`sum of array entries does not approximately equal 1.0: ${sum}`); | ||
throw new Error("sum of array entries does not approximately equal 1.0: " + sum); | ||
// the for loop may not return a value when both r is (nearly) 1.0 and when the | ||
// cumulative sum is less than 1.0 (as a result of floating-point roundoff error) | ||
while (true) { | ||
const r = StdRandom.uniform(); | ||
var r = StdRandom.uniform(); | ||
sum = 0.0; | ||
for (let i = 0; i < probabilities.length; i++) { | ||
for (var i = 0; i < probabilities.length; i++) { | ||
sum += probabilities[i]; | ||
@@ -117,3 +122,3 @@ if (sum > r) | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -123,14 +128,16 @@ * Shuffle array | ||
*/ | ||
static shuffle(arr) { | ||
const { length } = arr; | ||
for (let i = 0; i < length; i++) { | ||
const targetIndex = i + StdRandom.uniform(length - i); | ||
const temp = arr[targetIndex]; | ||
StdRandom.shuffle = function (arr) { | ||
var length = arr.length; | ||
for (var i = 0; i < length; i++) { | ||
var targetIndex = i + StdRandom.uniform(length - i); | ||
var temp = arr[targetIndex]; | ||
arr[targetIndex] = arr[i]; | ||
arr[i] = temp; | ||
} | ||
} | ||
} | ||
StdRandom._seed = Date.now(); | ||
StdRandom._random = seedRandom(String(StdRandom._seed)); | ||
}; | ||
StdRandom._seed = Date.now(); | ||
StdRandom._random = seedrandom_1.default(String(StdRandom._seed)); | ||
return StdRandom; | ||
}()); | ||
exports.default = StdRandom; | ||
//# sourceMappingURL=index.js.map |
export default class StopWatch { | ||
private start; | ||
elapseTime(): number; | ||
elapsedTime(): number; | ||
toString(): void; | ||
private getReadableStr; | ||
} |
@@ -1,22 +0,27 @@ | ||
import ms from 'ms'; | ||
export default class StopWatch { | ||
constructor() { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var ms_1 = tslib_1.__importDefault(require("ms")); | ||
var StopWatch = /** @class */ (function () { | ||
function StopWatch() { | ||
this.start = Date.now(); | ||
} | ||
elapseTime() { | ||
StopWatch.prototype.elapsedTime = function () { | ||
return Date.now() - this.start; | ||
} | ||
toString() { | ||
console.log(`Elapse ${this.getReadableStr(this.elapseTime())} ms`); | ||
} | ||
getReadableStr(time) { | ||
}; | ||
StopWatch.prototype.toString = function () { | ||
console.log("Elapse " + this.getReadableStr(this.elapsedTime()) + " ms"); | ||
}; | ||
StopWatch.prototype.getReadableStr = function (time) { | ||
if (time < 1000) { | ||
return `${time} ms`; | ||
return time + " ms"; | ||
} | ||
if (time >= 1000 && time < 6000) { | ||
return `${time / 1000} s`; | ||
return time / 1000 + " s"; | ||
} | ||
return ms(time, { long: true }); | ||
} | ||
} | ||
return ms_1.default(time, { long: true }); | ||
}; | ||
return StopWatch; | ||
}()); | ||
exports.default = StopWatch; | ||
//# sourceMappingURL=index.js.map |
/** | ||
* The {@code ThreeSumFast} class provides static methods for counting | ||
* The `ThreeSumFast` class provides static methods for counting | ||
* and printing the number of triples in an array of distinct integers that | ||
@@ -4,0 +4,0 @@ * sum to 0 (ignoring integer overflow). |
@@ -1,4 +0,7 @@ | ||
import BinarySearch from '../BinarySearch'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var BinarySearch_1 = tslib_1.__importDefault(require("../BinarySearch")); | ||
/** | ||
* The {@code ThreeSumFast} class provides static methods for counting | ||
* The `ThreeSumFast` class provides static methods for counting | ||
* and printing the number of triples in an array of distinct integers that | ||
@@ -13,13 +16,16 @@ * sum to 0 (ignoring integer overflow). | ||
*/ | ||
export default class ThreeSumFast { | ||
static count(a, sum = 0) { | ||
const sortedA = a.sort(); | ||
var ThreeSumFast = /** @class */ (function () { | ||
function ThreeSumFast() { | ||
} | ||
ThreeSumFast.count = function (a, sum) { | ||
if (sum === void 0) { sum = 0; } | ||
var sortedA = a.sort(); | ||
if (ThreeSumFast.containsDuplicates(sortedA)) { | ||
throw new Error('array contains duplicate integers'); | ||
} | ||
const len = sortedA.length; | ||
let count = 0; | ||
for (let i = 0; i < len; i++) { | ||
for (let j = i + 1; j < len; j++) { | ||
if (BinarySearch.rank(sum - (sortedA[i] + sortedA[j]), sortedA) > j) { | ||
var len = sortedA.length; | ||
var count = 0; | ||
for (var i = 0; i < len; i++) { | ||
for (var j = i + 1; j < len; j++) { | ||
if (BinarySearch_1.default.rank(sum - (sortedA[i] + sortedA[j]), sortedA) > j) { | ||
count += 1; | ||
@@ -30,5 +36,5 @@ } | ||
return count; | ||
} | ||
static containsDuplicates(a) { | ||
for (let i = 1; i < a.length; i++) { | ||
}; | ||
ThreeSumFast.containsDuplicates = function (a) { | ||
for (var i = 1; i < a.length; i++) { | ||
if (a[i] === a[i - 1]) | ||
@@ -38,4 +44,6 @@ return true; | ||
return false; | ||
} | ||
} | ||
}; | ||
return ThreeSumFast; | ||
}()); | ||
exports.default = ThreeSumFast; | ||
//# sourceMappingURL=index.js.map |
export default function assert(x: boolean, message: string): void; | ||
export declare function assertSafeNumber(x: number | number[], message?: string): void; |
@@ -1,2 +0,5 @@ | ||
export default function assert(x, message) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
function assert(x, message) { | ||
if (typeof x === 'undefined' || x === null) { | ||
@@ -6,3 +9,6 @@ throw new Error(message); | ||
} | ||
export function assertSafeNumber(x, message = 'Must be valid number') { | ||
exports.default = assert; | ||
function assertSafeNumber(x, message) { | ||
var e_1, _a; | ||
if (message === void 0) { message = 'Must be valid number'; } | ||
if (!Array.isArray(x) && | ||
@@ -16,12 +22,23 @@ (typeof x === 'undefined' || | ||
if (Array.isArray(x)) { | ||
for (const item of x) { | ||
if (typeof item === 'undefined' || | ||
item === null || | ||
!Number.isFinite(item) || | ||
Number.isNaN(item)) { | ||
throw new Error(message); | ||
try { | ||
for (var _b = tslib_1.__values(x), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var item = _c.value; | ||
if (typeof item === 'undefined' || | ||
item === null || | ||
!Number.isFinite(item) || | ||
Number.isNaN(item)) { | ||
throw new Error(message); | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
} | ||
exports.assertSafeNumber = assertSafeNumber; | ||
//# sourceMappingURL=assert.js.map |
@@ -0,0 +0,0 @@ export default class VisualAccumulator { |
@@ -1,4 +0,7 @@ | ||
import StdDraw from '../StdDraw'; | ||
export default class VisualAccumulator { | ||
constructor() { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var StdDraw_1 = tslib_1.__importDefault(require("../StdDraw")); | ||
var VisualAccumulator = /** @class */ (function () { | ||
function VisualAccumulator() { | ||
this.total = 0; | ||
@@ -12,17 +15,19 @@ this.n = 0; | ||
// } | ||
addDataValue(value) { | ||
VisualAccumulator.prototype.addDataValue = function (value) { | ||
this.n++; | ||
this.total += value; | ||
StdDraw.setPenColor(StdDraw.DARK_GRAY); | ||
StdDraw.point(this.n, value); | ||
StdDraw.setPenColor(StdDraw.RED); | ||
StdDraw.point(this.n, this.mean()); | ||
} | ||
mean() { | ||
StdDraw_1.default.setPenColor(StdDraw_1.default.DARK_GRAY); | ||
StdDraw_1.default.point(this.n, value); | ||
StdDraw_1.default.setPenColor(StdDraw_1.default.RED); | ||
StdDraw_1.default.point(this.n, this.mean()); | ||
}; | ||
VisualAccumulator.prototype.mean = function () { | ||
return this.total / this.n; | ||
} | ||
toString() { | ||
return `n = ${this.n}, mean = ${this.mean()}`; | ||
} | ||
} | ||
}; | ||
VisualAccumulator.prototype.toString = function () { | ||
return "n = " + this.n + ", mean = " + this.mean(); | ||
}; | ||
return VisualAccumulator; | ||
}()); | ||
exports.default = VisualAccumulator; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "algs4", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "The book Algorithms's implementation, written with TypeScript", | ||
@@ -17,2 +17,3 @@ "main": "lib/index.js", | ||
"watch": "tsc --watch", | ||
"run-main": "node ./scripts/run-main.js", | ||
"prepublishOnly": "yarn run build && yarn test" | ||
@@ -28,4 +29,6 @@ }, | ||
"babel-jest": "^24.9.0", | ||
"inquirer": "^7.0.0", | ||
"jest": "^24.9.0", | ||
"jest-canvas-mock": "^2.2.0", | ||
"jest-mock-process": "^1.2.0", | ||
"ts-jest": "^24.1.0", | ||
@@ -36,5 +39,8 @@ "typescript": "^3.7.2" | ||
"@types/seedrandom": "^2.4.28", | ||
"debug": "^4.1.1", | ||
"ms": "^2.1.2", | ||
"printf": "^0.5.2", | ||
"seedrandom": "^3.0.5" | ||
"seedrandom": "^3.0.5", | ||
"shelljs": "^0.8.3", | ||
"ts-node": "^8.5.2" | ||
}, | ||
@@ -44,3 +50,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "6f5cbe873ff50bae7cc73b33d92bd01a230824fc" | ||
"gitHead": "92afad7aeb1735433df39a569b40f8d0f9d83c32" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
241982
90
4896
0
7
13
2
+ Addeddebug@^4.1.1
+ Addedshelljs@^0.8.3
+ Addedts-node@^8.5.2
+ Addedarg@4.1.3(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddiff@4.0.2(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedinterpret@1.4.0(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedmake-error@1.3.6(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedrechoir@0.6.2(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedshelljs@0.8.5(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedsource-map-support@0.5.21(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedts-node@8.10.2(transitive)
+ Addedtypescript@5.6.3(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedyn@3.1.1(transitive)