Skip to content

Add solution and spec files for elyses-transformative-enchantments #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
}
],
"files": {
"solution": [],
"test": [],
"exemplar": [".meta/exemplar.js"]
"solution": ["enchantments.js"],
"test": ["enchantments.spec.js"],
"exemplar": ["exemplar.js"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Double every card in the deck
*
* @param {number[]} deck
*
* @returns {number[]} deck with every card doubled
*/
export function seeingDouble(deck) {
throw new Error('Implement the seeingDouble function');
}

/**
* Creates triplicates of every 3 found in the deck
*
* @param {number[]} deck
*
* @returns {number[]} deck with triplicate 3s
*/
export function threeOfEachThree(deck) {
throw new Error('Implement the threeOfEachThree function');
}

/**
* Removes every card from the deck but the middle two
* Assumes a deck is always 10 cards
*
* @param {number[]} deck of 10 cards
*
* @returns {number[]} deck with only two middle cards
*/
export function middleTwo(deck) {
throw new Error('Implement the middleTwo function');
}

/**
* Moves the outside two cards to the middle
*
* @param {number[]} deck with 10 cards
*
* @returns {number[]} transformed deck
*/

export function sandwichTrick(deck) {
throw new Error('Implement the sandwichTrick function');
}

/**
* Removes every card from the deck except 2s
*
* @param {number[]} deck
*
* @returns {number[]} deck with only 2s
*/
export function twoIsSpecial(deck) {
throw new Error('Implement the twoIsSpecial function');
}

/**
* Returns a perfectly order deck from lowest to highest
*
* @param {number[]} shuffled deck
*
* @returns {number[]} ordered deck
*/
export function perfectlyOrdered(deck) {
throw new Error('Implement the perfectlyOrdered function');
}

/**
* Returns a deck with every card equal to the total number of cards
*
* @param {number[]} deck
*
* @returns {number[]} deck
*/
export function countingCards(deck) {
throw new Error('Implement the countingCards function');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// @ts-check

/**
* @template T
* @template Expected
* @typedef {[T, Expected]} TestCase
*/

/**
* @template T
* @template Expected
* @typedef {TestCase<T, Expected>[]} TestCases
*/

import {
seeingDouble,
threeOfEachThree,
middleTwo,
sandwichTrick,
twoIsSpecial,
perfectlyOrdered,
countingCards,
} from './enchantments';

describe('array', () => {
describe('seeingDouble', () => {
/** @type {TestCases<Card[], Card[]>} */
const seeingDoubleTestCases = [
[[], []],
[[3], [6]],
[
[1, 2, 3, 4],
[2, 4, 6, 8],
],
[
[2, 5, 1, 9],
[4, 10, 2, 18],
],
];

seeingDoubleTestCases.forEach(([deck, expected]) => {
test(`seeingDouble([${deck}])`, () => {
expect(seeingDouble(deck)).toStrictEqual(expected);
});
});
});

describe('threeOfEachThree', () => {
/** @type {TestCases<Card[], Card[]>} */
const threeOfEachThreeTestCases = [
[[], []],
[[3], [3, 3, 3]],
[[2], [2]],
[
[1, 3, 9, 3, 7],
[1, 3, 3, 3, 9, 3, 3, 3, 7],
],
];

threeOfEachThreeTestCases.forEach(([deck, expected]) => {
test(`threeOfEachThree([${deck}])`, () => {
expect(threeOfEachThree(deck)).toStrictEqual(expected);
});
});
});

describe('middleTwo', () => {
/** @type {TestCases<Card[], Card[]>} */
const middleTwoTestCases = [
[
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[5, 6],
],
[
[12, 2, 36, 45, 15, 96, 27, 86, 1, 29],
[15, 96],
],
];

middleTwoTestCases.forEach(([deck, expected]) => {
test(`middleTwo([${deck}])`, () => {
expect(middleTwo(deck)).toStrictEqual(expected);
});
});
});

describe('sandwichTrick', () => {
/** @type {TestCases<Card[], Card[]>} */
const sandwichTrickTestCases = [
[
[1, 2, 3, 5, 6, 10],
[2, 3, 10, 1, 5, 6],
],
[
[12, 3, 5, 87],
[3, 87, 12, 5],
],
];

sandwichTrickTestCases.forEach(([deck, expected]) => {
test(`sandwichTrick([${deck}])`, () => {
expect(sandwichTrick(deck)).toStrictEqual(expected);
});
});
});

describe('twoIsSpecial', () => {
/** @type {TestCases<Card[], Card[]>} */
const twoIsSpecialTestCases = [
[[], []],
[[1, 9, 1], []],
[[1, 2, 9, 1], [2]],
[
[121, 22, 9, 12, 2, 2, 2, 23, 2],
[2, 2, 2, 2],
],
];

twoIsSpecialTestCases.forEach(([deck, expected]) => {
test(`twoIsSpecial([${deck}])`, () => {
expect(twoIsSpecial(deck)).toStrictEqual(expected);
});
});
});

describe('perfectlyOrdered', () => {
/** @type {TestCases<Card[], Card[]>} */
const perfectlyOrderedTestCases = [
[[], []],
[
[1, 9, 1],
[1, 1, 9],
],
[
[1, 2, 9, 1],
[1, 1, 2, 9],
],
[
[10, 1, 5, 3, 2, 8, 7],
[1, 2, 3, 5, 7, 8, 10],
],
];

perfectlyOrderedTestCases.forEach(([deck, expected]) => {
test(`perfectlyOrdered([${deck}])`, () => {
expect(perfectlyOrdered(deck)).toStrictEqual(expected);
});
});
});

describe('countingCards', () => {
/** @type {TestCases<Card[], Card[]>} */
const countingCardsTestCases = [
[[], []],
[
[1, 9, 1],
[3, 3, 3],
],
[
[1, 2, 9, 1],
[4, 4, 4, 4],
],
[
[121, 22, 9, 12, 2, 2, 23],
[7, 7, 7, 7, 7, 7, 7],
],
];

countingCardsTestCases.forEach(([deck, expected]) => {
test(`countingCards([${deck}])`, () => {
expect(countingCards(deck)).toStrictEqual(expected);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function middleTwo(deck) {
// TODO: which implementation?
// const middle = Math.floor(deck.length / 2)
// return deck.slice(middle, middle + 1)
return deck.slice(4, 5);
return deck.slice(4, 6);
}

/**
Expand All @@ -53,7 +53,9 @@ export function middleTwo(deck) {
export function sandwichTrick(deck) {
const firstCard = deck.shift();
const lastCard = deck.pop();
return deck.splice(3, 0, lastCard, firstCard);
const mid = deck.length / 2;
deck.splice(mid, 0, lastCard, firstCard);
return deck;
}

/**
Expand All @@ -75,7 +77,7 @@ export function twoIsSpecial(deck) {
* @returns {number[]} ordered deck
*/
export function perfectlyOrdered(deck) {
return deck.sort();
return deck.sort((a, b) => a - b);
}

/**
Expand Down