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

Updated lab with problems for 2018.

parent fa80ca54
Branches
No related tags found
No related merge requests found
/* exported determineBuyPrice */
function descendsThenAscends(prices) {
let previous = Infinity;
let recovering = false;
for (const price of prices) {
if (price < previous) {
if (recovering) {
return false;
}
} else if (!recovering) {
recovering = true;
}
previous = price;
}
return true;
}
function determineBuyPrice(forecastPrices) {
console.assert(forecastPrices.length > 0, 'Tried to find a buy price with an empty forecast.');
console.assert(descendsThenAscends(forecastPrices), 'Tried to find a buy price with a forecast that does something other than descend and then ascend.');
if (forecastPrices.length === 1) {
return forecastPrices[0];
}
const middle = Math.floor(forecastPrices.length / 2);
if (true) { // TODO: placeholder
return determineBuyPrice(forecastPrices.slice(0, middle));
}
return determineBuyPrice(forecastPrices.slice(middle));
}
File moved
/* exported SellPlan determineSellChange */
class SellPlan {
constructor(sellChange, totalChange) {
this.sellChange = sellChange;
this.totalChange = totalChange;
}
}
function combineSellPlans(firstPlan, secondPlan) {
return new SellPlan( // TODO: stub
firstPlan.sellChange,
secondPlan.totalChange
);
}
function determineSellChange(forecastChanges) {
if (forecastChanges.length === 0) {
return new SellPlan(0, 0);
}
if (forecastChanges.length === 1) {
return new SellPlan(forecastChanges[0], forecastChanges[0]); // TODO: placeholder
}
const middle = Math.floor(forecastChanges.length / 2);
const firstPlan = determineSellChange(forecastChanges.slice(0, middle));
const secondPlan = determineSellChange(forecastChanges.slice(middle));
return combineSellPlans(firstPlan, secondPlan);
}
QUnit.module('buy.js');
/* globals QUnit determineBuyPrice */
/* eslint-disable no-magic-numbers */
QUnit.test('find a buy price for a one-element forecast', (assert) => {
assert.deepEqual(determineBuyPrice([100]), 100);
});
QUnit.test('find a buy price for a two-element descending forecast', (assert) => {
assert.deepEqual(determineBuyPrice([100, 99]), 99);
});
QUnit.test('find a buy price for a two-element ascending forecast', (assert) => {
assert.deepEqual(determineBuyPrice([100, 101]), 100);
});
QUnit.test('find a buy price for a three-element descending forecast', (assert) => {
assert.deepEqual(determineBuyPrice([100, 99, 98]), 98);
});
QUnit.test('find a buy price for a three-element ascending forecast', (assert) => {
assert.deepEqual(determineBuyPrice([100, 101, 102]), 100);
});
QUnit.test('find a buy price for a three-element dipping forecast', (assert) => {
assert.deepEqual(determineBuyPrice([100, 99, 100]), 99);
});
QUnit.test('find a buy price for random forecast #0', (assert) => {
assert.deepEqual(determineBuyPrice([144, 122, 121, 110, 91, 50, 60, 64, 102, 129, 146]), 50);
});
QUnit.test('find a buy price for random forecast #1', (assert) => {
assert.deepEqual(determineBuyPrice([145, 143, 130, 70, 60, 53, 51, 127]), 51);
});
QUnit.test('find a buy price for random forecast #2', (assert) => {
assert.deepEqual(determineBuyPrice([138, 104, 95, 108]), 95);
});
QUnit.test('find a buy price for random forecast #3', (assert) => {
assert.deepEqual(determineBuyPrice([111, 80, 59, 73, 80, 97, 126, 134]), 59);
});
QUnit.test('find a buy price for random forecast #4', (assert) => {
assert.deepEqual(determineBuyPrice([129, 117, 105, 57, 79, 101, 106, 110]), 57);
});
QUnit.test('find a buy price for random forecast #5', (assert) => {
assert.deepEqual(determineBuyPrice([136, 135, 123, 86, 61, 60, 51, 53]), 51);
});
QUnit.test('find a buy price for random forecast #6', (assert) => {
assert.deepEqual(determineBuyPrice([52, 55, 60, 78, 136]), 52);
});
QUnit.test('find a buy price for random forecast #7', (assert) => {
assert.deepEqual(determineBuyPrice([60, 65, 71, 129]), 60);
});
QUnit.test('find a buy price for random forecast #8', (assert) => {
assert.deepEqual(determineBuyPrice([137, 61, 52, 70, 89, 101, 134, 148]), 52);
});
QUnit.test('find a buy price for random forecast #9', (assert) => {
assert.deepEqual(determineBuyPrice([144, 131, 120, 99, 51, 55, 65, 105, 109, 115, 116, 145]), 51);
});
QUnit.test('find a buy price for random forecast #10', (assert) => {
assert.deepEqual(determineBuyPrice([148, 146, 136, 112, 68, 52, 98, 115, 122]), 52);
});
QUnit.test('find a buy price for random forecast #11', (assert) => {
assert.deepEqual(determineBuyPrice([138, 116, 99, 68, 67, 50, 63, 112]), 50);
});
QUnit.test('find a buy price for random forecast #12', (assert) => {
assert.deepEqual(determineBuyPrice([110, 109, 82, 81, 79, 75, 69, 113, 115]), 69);
});
QUnit.test('find a buy price for random forecast #13', (assert) => {
assert.deepEqual(determineBuyPrice([127, 91, 81, 53, 50, 57, 74, 83, 84, 101, 103, 133]), 50);
});
QUnit.test('find a buy price for random forecast #14', (assert) => {
assert.deepEqual(determineBuyPrice([132, 113, 105, 71, 53, 55, 84, 88]), 53);
});
QUnit.test('find a buy price for random forecast #15', (assert) => {
assert.deepEqual(determineBuyPrice([143, 134, 86, 67]), 67);
});
QUnit.test('find a buy price for random forecast #16', (assert) => {
assert.deepEqual(determineBuyPrice([146, 134, 130, 120, 108, 66, 59, 83, 98, 115, 121, 129, 135, 147]), 59);
});
QUnit.test('find a buy price for random forecast #17', (assert) => {
assert.deepEqual(determineBuyPrice([128, 127, 126, 102, 89, 62, 71]), 62);
});
QUnit.test('find a buy price for random forecast #18', (assert) => {
assert.deepEqual(determineBuyPrice([86, 58, 75, 88, 89, 111, 127, 129]), 58);
});
QUnit.test('find a buy price for random forecast #19', (assert) => {
assert.deepEqual(determineBuyPrice([134, 132, 129, 121, 113, 112, 96, 58, 101, 131, 148, 149]), 58);
});
QUnit.test('find a buy price for random forecast #20', (assert) => {
assert.deepEqual(determineBuyPrice([147, 113, 98, 68, 55, 51]), 51);
});
QUnit.test('find a buy price for random forecast #21', (assert) => {
assert.deepEqual(determineBuyPrice([138, 134, 98, 97, 93, 61, 60, 96, 130, 147]), 60);
});
QUnit.test('find a buy price for random forecast #22', (assert) => {
assert.deepEqual(determineBuyPrice([137, 107, 92, 91, 64, 63, 55, 60, 74, 77, 86, 122, 134]), 55);
});
QUnit.test('find a buy price for random forecast #23', (assert) => {
assert.deepEqual(determineBuyPrice([148, 133, 68, 55, 98, 117]), 55);
});
QUnit.test('find a buy price for random forecast #24', (assert) => {
assert.deepEqual(determineBuyPrice([140, 125, 91, 64, 53, 65]), 53);
});
QUnit.test('find a buy price for random forecast #25', (assert) => {
assert.deepEqual(determineBuyPrice([133, 127, 119, 115, 84, 65, 64, 71, 141, 142, 143]), 64);
});
QUnit.test('find a buy price for random forecast #26', (assert) => {
assert.deepEqual(determineBuyPrice([116, 93, 50, 60, 106, 124, 137, 142]), 50);
});
QUnit.test('find a buy price for random forecast #27', (assert) => {
assert.deepEqual(determineBuyPrice([142, 118, 105, 94, 87, 53, 88, 106, 138, 144, 149]), 53);
});
QUnit.test('find a buy price for random forecast #28', (assert) => {
assert.deepEqual(determineBuyPrice([138, 102, 74, 75, 96, 117, 134, 146]), 74);
});
QUnit.test('find a buy price for random forecast #29', (assert) => {
assert.deepEqual(determineBuyPrice([144, 133, 100, 89, 88, 81, 58, 69, 75, 83, 91, 146]), 58);
});
QUnit.test('find a buy price for random forecast #30', (assert) => {
assert.deepEqual(determineBuyPrice([140, 64, 83, 90, 96, 103, 130, 144]), 64);
});
QUnit.test('find a buy price for random forecast #31', (assert) => {
assert.deepEqual(determineBuyPrice([136, 120, 103, 60, 92, 123, 124, 128, 130, 147]), 60);
});
QUnit.test('find a buy price for random forecast #32', (assert) => {
assert.deepEqual(determineBuyPrice([139, 119, 88, 78, 76, 54, 112, 125, 128]), 54);
});
QUnit.test('find a buy price for random forecast #33', (assert) => {
assert.deepEqual(determineBuyPrice([144, 81, 80, 64, 63, 97, 129, 131, 142, 144]), 63);
});
QUnit.test('find a buy price for random forecast #34', (assert) => {
assert.deepEqual(determineBuyPrice([51, 85, 121, 137]), 51);
});
QUnit.test('find a buy price for random forecast #35', (assert) => {
assert.deepEqual(determineBuyPrice([133, 120, 97, 94, 86, 83, 67, 59, 97, 99, 101, 130, 148]), 59);
});
QUnit.test('find a buy price for random forecast #36', (assert) => {
assert.deepEqual(determineBuyPrice([55, 61, 103, 107, 131, 132]), 55);
});
QUnit.test('find a buy price for random forecast #37', (assert) => {
assert.deepEqual(determineBuyPrice([95, 80, 79, 56, 54, 52, 51, 20, 54, 72, 115, 120, 129, 138]), 20);
});
QUnit.test('find a buy price for random forecast #38', (assert) => {
assert.deepEqual(determineBuyPrice([134, 133, 132, 127, 115, 90, 87, 105, 107]), 87);
});
QUnit.test('find a buy price for random forecast #39', (assert) => {
assert.deepEqual(determineBuyPrice([118, 117, 57, 67, 91, 120, 132]), 57);
});
QUnit.test('find a buy price for random forecast #40', (assert) => {
assert.deepEqual(determineBuyPrice([77, 65, 54, 53, 76, 84, 109, 126, 147]), 53);
});
QUnit.test('find a buy price for random forecast #41', (assert) => {
assert.deepEqual(determineBuyPrice([122, 110, 63, 80, 90, 117, 136, 138]), 63);
});
QUnit.test('find a buy price for random forecast #42', (assert) => {
assert.deepEqual(determineBuyPrice([143, 139, 115, 84, 66, 116]), 66);
});
QUnit.test('find a buy price for random forecast #43', (assert) => {
assert.deepEqual(determineBuyPrice([87, 73, 72, 71, 70, 62, 59, 69, 110]), 59);
});
QUnit.test('find a buy price for random forecast #44', (assert) => {
assert.deepEqual(determineBuyPrice([56, 67, 81, 101, 104, 113]), 56);
});
QUnit.test('find a buy price for random forecast #45', (assert) => {
assert.deepEqual(determineBuyPrice([121, 98, 95, 92, 73, 63, 61, 129]), 61);
});
QUnit.test('find a buy price for random forecast #46', (assert) => {
assert.deepEqual(determineBuyPrice([147, 99, 92, 94]), 92);
});
QUnit.test('find a buy price for random forecast #47', (assert) => {
assert.deepEqual(determineBuyPrice([143, 139, 58, 69, 147]), 58);
});
QUnit.test('find a buy price for random forecast #48', (assert) => {
assert.deepEqual(determineBuyPrice([51, 60, 61, 83, 108]), 51);
});
QUnit.test('find a buy price for random forecast #49', (assert) => {
assert.deepEqual(determineBuyPrice([97, 87, 84, 57, 56, 54, 101, 143, 149]), 54);
});
QUnit.test('find a buy price for random forecast #50', (assert) => {
assert.deepEqual(determineBuyPrice([137, 106, 105, 78]), 78);
});
QUnit.test('find a buy price for random forecast #51', (assert) => {
assert.deepEqual(determineBuyPrice([98, 74, 67, 60, 54, 59, 63, 94, 119, 130, 136]), 54);
});
QUnit.test('find a buy price for random forecast #52', (assert) => {
assert.deepEqual(determineBuyPrice([67, 99, 107, 143]), 67);
});
QUnit.test('find a buy price for random forecast #53', (assert) => {
assert.deepEqual(determineBuyPrice([70, 50, 63, 71, 83]), 50);
});
QUnit.test('find a buy price for random forecast #54', (assert) => {
assert.deepEqual(determineBuyPrice([136, 132, 125, 122, 83, 76, 68, 123, 137]), 68);
});
QUnit.test('find a buy price for random forecast #55', (assert) => {
assert.deepEqual(determineBuyPrice([52, 97, 133, 144]), 52);
});
QUnit.test('find a buy price for random forecast #56', (assert) => {
assert.deepEqual(determineBuyPrice([136, 129, 115, 94, 89, 87, 84, 61, 62, 63, 88, 117, 119, 137]), 61);
});
QUnit.test('find a buy price for random forecast #57', (assert) => {
assert.deepEqual(determineBuyPrice([54, 70, 90, 106, 121, 125]), 54);
});
QUnit.test('find a buy price for random forecast #58', (assert) => {
assert.deepEqual(determineBuyPrice([129, 69, 78, 88, 100, 104, 112]), 69);
});
QUnit.test('find a buy price for random forecast #59', (assert) => {
assert.deepEqual(determineBuyPrice([139, 137, 123, 104, 90, 50, 55, 66, 70, 114]), 50);
});
QUnit.test('find a buy price for random forecast #60', (assert) => {
assert.deepEqual(determineBuyPrice([111, 78, 63, 53, 70, 103, 127, 136]), 53);
});
QUnit.test('find a buy price for random forecast #61', (assert) => {
assert.deepEqual(determineBuyPrice([147, 110, 103, 82, 76, 87, 90, 127]), 76);
});
QUnit.test('find a buy price for random forecast #62', (assert) => {
assert.deepEqual(determineBuyPrice([121, 92, 79, 81, 86]), 79);
});
QUnit.test('find a buy price for random forecast #63', (assert) => {
assert.deepEqual(determineBuyPrice([52, 107, 129, 137]), 52);
});
QUnit.test('find a buy price for random forecast #64', (assert) => {
assert.deepEqual(determineBuyPrice([114, 79, 78, 55, 71, 104, 106, 107, 139, 144]), 55);
});
QUnit.test('find a buy price for random forecast #65', (assert) => {
assert.deepEqual(determineBuyPrice([146, 140, 134, 51, 59, 68, 69, 85, 106, 119]), 51);
});
QUnit.test('find a buy price for random forecast #66', (assert) => {
assert.deepEqual(determineBuyPrice([65, 71, 123, 133, 141]), 65);
});
QUnit.test('find a buy price for random forecast #67', (assert) => {
assert.deepEqual(determineBuyPrice([121, 82, 65, 60]), 60);
});
QUnit.test('find a buy price for random forecast #68', (assert) => {
assert.deepEqual(determineBuyPrice([142, 136, 83, 71, 56, 55, 81, 104, 118, 137]), 55);
});
QUnit.test('find a buy price for random forecast #69', (assert) => {
assert.deepEqual(determineBuyPrice([140, 134, 112, 90, 51, 59, 71, 109, 111, 124]), 51);
});
QUnit.test('find a buy price for random forecast #70', (assert) => {
assert.deepEqual(determineBuyPrice([132, 88, 73, 68, 64, 56, 40, 55, 60]), 40);
});
QUnit.test('find a buy price for random forecast #71', (assert) => {
assert.deepEqual(determineBuyPrice([112, 90, 89, 71, 59]), 59);
});
QUnit.test('find a buy price for random forecast #72', (assert) => {
assert.deepEqual(determineBuyPrice([144, 138, 130, 115, 88, 78, 70, 95, 98, 100, 105, 110, 137]), 70);
});
QUnit.test('find a buy price for random forecast #73', (assert) => {
assert.deepEqual(determineBuyPrice([126, 62, 70, 77, 86]), 62);
});
QUnit.test('find a buy price for random forecast #74', (assert) => {
assert.deepEqual(determineBuyPrice([60, 65, 85, 117, 129, 148]), 60);
});
QUnit.test('find a buy price for random forecast #75', (assert) => {
assert.deepEqual(determineBuyPrice([116, 92, 66, 60, 63, 94, 109, 113, 141]), 60);
});
QUnit.test('find a buy price for random forecast #76', (assert) => {
assert.deepEqual(determineBuyPrice([140, 103, 89, 60, 87]), 60);
});
QUnit.test('find a buy price for random forecast #77', (assert) => {
assert.deepEqual(determineBuyPrice([137, 134, 127, 96, 94, 81, 78, 75]), 75);
});
QUnit.test('find a buy price for random forecast #78', (assert) => {
assert.deepEqual(determineBuyPrice([87, 64, 71, 72, 96, 115, 116, 135]), 64);
});
QUnit.test('find a buy price for random forecast #79', (assert) => {
assert.deepEqual(determineBuyPrice([146, 144, 142, 129, 114, 60, 62, 91, 99, 117, 139, 169]), 60);
});
QUnit.test('find a buy price for random forecast #80', (assert) => {
assert.deepEqual(determineBuyPrice([143, 134, 130, 67, 143]), 67);
});
QUnit.test('find a buy price for random forecast #81', (assert) => {
assert.deepEqual(determineBuyPrice([117, 96, 83, 60, 64, 67, 76, 81, 86]), 60);
});
QUnit.test('find a buy price for random forecast #82', (assert) => {
assert.deepEqual(determineBuyPrice([130, 101, 98, 85, 78, 76, 57]), 57);
});
QUnit.test('find a buy price for random forecast #83', (assert) => {
assert.deepEqual(determineBuyPrice([130, 122, 58, 56, 91, 108]), 56);
});
QUnit.test('find a buy price for random forecast #84', (assert) => {
assert.deepEqual(determineBuyPrice([138, 84, 59, 102]), 59);
});
QUnit.test('find a buy price for random forecast #85', (assert) => {
assert.deepEqual(determineBuyPrice([114, 62, 58, 57, 126, 130, 133]), 57);
});
QUnit.test('find a buy price for random forecast #86', (assert) => {
assert.deepEqual(determineBuyPrice([127, 99, 96, 94, 93, 87, 79, 57, 84, 91, 118, 131, 133]), 57);
});
QUnit.test('find a buy price for random forecast #87', (assert) => {
assert.deepEqual(determineBuyPrice([74, 91, 96, 102, 128]), 74);
});
QUnit.test('find a buy price for random forecast #88', (assert) => {
assert.deepEqual(determineBuyPrice([149, 134, 118, 65, 64, 71, 95, 107, 118]), 64);
});
QUnit.test('find a buy price for random forecast #89', (assert) => {
assert.deepEqual(determineBuyPrice([134, 127, 116, 107, 105, 98, 59, 61, 115]), 59);
});
QUnit.test('find a buy price for random forecast #90', (assert) => {
assert.deepEqual(determineBuyPrice([73, 62, 60, 54, 76, 86, 105, 128]), 54);
});
QUnit.test('find a buy price for random forecast #91', (assert) => {
assert.deepEqual(determineBuyPrice([143, 139, 128, 127, 115, 52, 109]), 52);
});
QUnit.test('find a buy price for random forecast #92', (assert) => {
assert.deepEqual(determineBuyPrice([65, 64, 63, 78, 85, 92, 93, 143]), 63);
});
QUnit.test('find a buy price for random forecast #93', (assert) => {
assert.deepEqual(determineBuyPrice([129, 126, 117, 96, 74, 72, 52, 74, 87]), 52);
});
QUnit.test('find a buy price for random forecast #94', (assert) => {
assert.deepEqual(determineBuyPrice([138, 117, 112, 92, 57, 122]), 57);
});
QUnit.test('find a buy price for random forecast #95', (assert) => {
assert.deepEqual(determineBuyPrice([122, 113, 101, 75]), 75);
});
QUnit.test('find a buy price for random forecast #96', (assert) => {
assert.deepEqual(determineBuyPrice([67, 59, 79, 90, 102, 110, 113]), 59);
});
QUnit.test('find a buy price for random forecast #97', (assert) => {
assert.deepEqual(determineBuyPrice([53, 82, 97, 108, 120]), 53);
});
QUnit.test('find a buy price for random forecast #98', (assert) => {
assert.deepEqual(determineBuyPrice([145, 137, 83, 82, 72, 71, 50, 56, 130]), 50);
});
QUnit.test('find a buy price for random forecast #99', (assert) => {
assert.deepEqual(determineBuyPrice([51, 81, 101, 116, 127]), 51);
});
QUnit.module('forecast.js');
QUnit.module('buy_sell.js');
/* globals QUnit BuySellPlan determineBuyAndSellPrices */
/* eslint-disable no-magic-numbers */
......
This diff is collapsed.
......@@ -13,8 +13,14 @@
<script src="../libraries/qunit/qunit.js"></script>
<script src="overrides.js"></script>
<script src="../js/forecast.js"></script>
<script src="test_forecast.js"></script>
<script src="../js/sell.js"></script>
<script src="test_sell.js"></script>
<script src="../js/buy_sell.js"></script>
<script src="test_buy_sell.js"></script>
<script src="../js/buy.js"></script>
<script src="test_buy.js"></script>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment