Skip to content

Instantly share code, notes, and snippets.

@thunder775
Last active June 2, 2022 10:10

Revisions

  1. thunder775 revised this gist Jun 2, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions grantit.js
    Original file line number Diff line number Diff line change
    @@ -60,10 +60,10 @@ const merge = (array1: Entry[], array2: Entry[]): Entry[] => {
    entriesMapByLabel[label] = entriesMapByLabel[label] && entriesMapByLabel[label] + value || value;
    }

    const resultEntries: Entry[] = Object.keys(entriesMapByLabel).map(key => ({label: key, value: entriesMapByLabel[key]}));
    resultEntries.sort(entriesCompareFunction)
    const mergedEntries: Entry[] = Object.keys(entriesMapByLabel).map(key => ({label: key, value: entriesMapByLabel[key]}));
    mergedEntries.sort(entriesCompareFunction)

    return resultEntries
    return mergedEntries
    };

    function entriesCompareFunction(a: Entry, b: Entry): number {
  2. thunder775 revised this gist May 26, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions grantit.js
    Original file line number Diff line number Diff line change
    @@ -61,12 +61,12 @@ const merge = (array1: Entry[], array2: Entry[]): Entry[] => {
    }

    const resultEntries: Entry[] = Object.keys(entriesMapByLabel).map(key => ({label: key, value: entriesMapByLabel[key]}));
    resultEntries.sort(entryCompareFunction)
    resultEntries.sort(entriesCompareFunction)

    return resultEntries
    };

    function entryCompareFunction(a: Entry, b: Entry): number {
    function entriesCompareFunction(a: Entry, b: Entry): number {
    const [label1, label2]: Array<string> = [a.label, b.label]
    if (label1 < label2) return -1
    if (label2 < label1) return 1
  3. thunder775 created this gist May 26, 2022.
    107 changes: 107 additions & 0 deletions grantit.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    /**
    * You are given two non-empty arrays `a` and `b`, each consisting of objects of
    * type Entry (see the type declaration below)
    * Each 'label' is unique within an array (e.g. you cannot have two entries
    * with `label` 'a' within the same array).
    *
    * Write a function `merge` which does the following:
    * 1) Merges the contents of the two arrays together and returns a new array.
    * When two entries have the same `label`, the result should have
    * their `value` property summed up.
    *
    * Each `label` must remain unique within the resulting array.
    * E.g. { label: 'a', value: 1 } and { label: 'a', value: 2 }
    * should be combined into { label: 'a', value: 3 }
    *
    * 2) Sorts the resulting array by the `label` property in ascending order
    * (you can use standard JS string comparison)
    *
    * For example:
    * [
    * { label: 'Abc', value: 1 },
    * { label: 'b', value: 1 },
    * { label: 'a', value: 1 }
    * ]
    * should become
    * [
    * { label: 'Abc', value: 1 },
    * { label: 'a', value: 1 },
    * { label: 'b', value: 1 }
    * ]
    *
    * To simplify the solition, you can assume the following:
    * - Both arrays length 1 =< length <= 10000
    * - `label` is a non-empty string consinsting of letters of English alphabet (a-z A-Z)
    * - `value` is a non-negative number (value >= 0)
    * - The order of elements' `labels` within the array is random (see example)
    * - Each 'label' is unique within an array (you cannot have two entries
    * with `label` 'a' within the same array).
    *
    * For any questions & submissions - please send an email to develop@grantit.io
    *
    * Requirements for submission:
    * - Please specify your name in the email subject
    * - Attach your submission. You can make a .txt / .js file attachment to your email
    * or provide a link to a public codesandbox / codepen / replit / codeshare.
    *
    * If your code has several iterations - feel free to leave all of them within
    * the same file. That will help us to understand your thought process better.
    */

    type Entry = {
    label: string;
    value: number;
    };

    const merge = (array1: Entry[], array2: Entry[]): Entry[] => {
    const entriesMapByLabel: {[key: string]: number} = {};

    for (let {label, value} of [...array1, ...array2]) {
    entriesMapByLabel[label] = entriesMapByLabel[label] && entriesMapByLabel[label] + value || value;
    }

    const resultEntries: Entry[] = Object.keys(entriesMapByLabel).map(key => ({label: key, value: entriesMapByLabel[key]}));
    resultEntries.sort(entryCompareFunction)

    return resultEntries
    };

    function entryCompareFunction(a: Entry, b: Entry): number {
    const [label1, label2]: Array<string> = [a.label, b.label]
    if (label1 < label2) return -1
    if (label2 < label1) return 1
    return 0
    }

    // /** Use the following code to test your submission.

    const a: Entry[] = [
    {label: 'a', value: 1},
    {label: 'b', value: 1},
    {label: 'c', value: 1},
    {label: 'e', value: 5},
    ];
    const b: Entry[] = [
    {label: 'Abc', value: 100},
    {label: 'e', value: 0},
    {label: 'a', value: 2},
    {label: 'c', value: 1},
    {label: 'd', value: 1},
    ];


    const result = merge(a, b);
    console.log(result);

    // console.log('A' < 'a')

    // The result should be
    // [
    // { label: 'Abc', value: 100 },
    // { label: 'a', value: 3 },
    // { label: 'b', value: 1 },
    // { label: 'c', value: 2 },
    // { label: 'd', value: 1 },
    // { label: 'e', value: 5 },
    // ]
    // */