Skip to content

Instantly share code, notes, and snippets.

@congnt24
Last active May 30, 2019 01:46

Revisions

  1. congnt24 revised this gist May 30, 2019. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions excel.js
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ function polishPostfix(str) {
    * @param tmp: temp array to store chain of reference. So we can detect the root of chain to throw circular dependency.
    * @returns {string|*}
    */
    function deRef(key, str, obj, tmp = []) {
    function deRef(key, cur_key, str, obj, tmp = []) {
    // console.log(str);
    if (!isNaN(str)) { //If the value is number
    return str
    @@ -60,10 +60,11 @@ function deRef(key, str, obj, tmp = []) {
    throw `Circular dependency between ${key} and ${tmp[0]} detected`;
    }
    tmp.push(item);
    ar2.push(deRef(key, obj[item], obj, tmp));
    ar2.push(deRef(key, item, obj[item], obj, tmp));
    tmp.pop();
    }
    }
    obj[cur_key] = ar2.join(' ');
    return ar2.join(' ');
    }

    @@ -80,7 +81,7 @@ function excel(n, obj) {
    for (let key of keys) {
    let value = obj[key];
    if (isNaN(value)) {// is string
    obj[key] = polishPostfix(deRef(key, value, obj));
    obj[key] = polishPostfix(deRef(key, key, value, obj));
    }
    }
    let sorted_keys = keys.sort();
  2. congnt24 revised this gist May 29, 2019. 1 changed file with 24 additions and 7 deletions.
    31 changes: 24 additions & 7 deletions excel.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    /**
    * calculating from a string of polish postfix notation
    * @param str: String of polish postfix notation
    * @returns {*}
    */
    function polishPostfix(str) {
    let arr = str.split(' ');
    let st = [];
    @@ -26,12 +31,21 @@ function polishPostfix(str) {
    }
    return st.pop();
    }

    /**
    * Convert reference string to all number and operations
    * @param key
    * @param str: String need to de reference
    * @param obj: The object that converted from the input
    * @param tmp: temp array to store chain of reference. So we can detect the root of chain to throw circular dependency.
    * @returns {string|*}
    */
    function deRef(key, str, obj, tmp = []) {
    // console.log(str);
    if (!isNaN(str)) {
    if (!isNaN(str)) { //If the value is number
    return str
    }
    if (!str){
    if (!str) {
    throw `${tmp.pop()} is not declared`
    }
    let arr = str.split(' ');
    @@ -53,26 +67,29 @@ function deRef(key, str, obj, tmp = []) {
    return ar2.join(' ');
    }

    /**
    * The main function
    * @param n: number of line
    * @param obj: the object that was converted from input
    */
    function excel(n, obj) {
    try {
    console.log('---Input---');
    console.log(obj);

    let keys = Object.keys(obj);
    for (let key of keys) {
    let value = obj[key];
    if (isNaN(value)) {//string
    if (isNaN(value)) {// is string
    obj[key] = polishPostfix(deRef(key, value, obj));
    }
    }
    let sorted_keys = keys.sort();
    for (let key of sorted_keys){
    for (let key of sorted_keys) {
    console.log(key);
    console.log(obj[key]);
    }
    } catch (e) {
    console.log(e);

    }
    }

    @@ -139,7 +156,7 @@ console.log('---------------');
    excel(5, {
    A1: 'B2 2 * C1 +',
    A2: 'B2 2 *',
    B2:1,
    B2: 1,
    B1: 'A1',
    C1: 'A1 A2 + B1 -'
    });
  3. congnt24 revised this gist May 29, 2019. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions excel.js
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,7 @@ function deRef(key, str, obj, tmp = []) {
    }
    tmp.push(item);
    ar2.push(deRef(key, obj[item], obj, tmp));
    tmp.pop();
    }
    }
    return ar2.join(' ');
    @@ -134,3 +135,11 @@ excel(4, {
    B1: 6,
    A1: 5
    });
    console.log('---------------');
    excel(5, {
    A1: 'B2 2 * C1 +',
    A2: 'B2 2 *',
    B2:1,
    B1: 'A1',
    C1: 'A1 A2 + B1 -'
    });
  4. congnt24 revised this gist May 29, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion excel.js
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,12 @@ function polishPostfix(str) {
    }
    function deRef(key, str, obj, tmp = []) {
    // console.log(str);
    if (!isNaN(str) || !str) {
    if (!isNaN(str)) {
    return str
    }
    if (!str){
    throw `${tmp.pop()} is not declared`
    }
    let arr = str.split(' ');
    let len = arr.length;
    let ar2 = [];
    @@ -114,6 +117,7 @@ excel(4, {
    console.log('---------------');
    excel(1, {
    A1: 'B2 2 *',
    A2: 'B2 2 *',
    });

    console.log('---------------');
  5. congnt24 revised this gist May 29, 2019. 1 changed file with 41 additions and 7 deletions.
    48 changes: 41 additions & 7 deletions excel.js
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ function polishPostfix(str) {
    }
    function deRef(key, str, obj, tmp = []) {
    // console.log(str);
    if (!isNaN(str)) {
    if (!isNaN(str) || !str) {
    return str
    }
    let arr = str.split(' ');
    @@ -51,22 +51,37 @@ function deRef(key, str, obj, tmp = []) {

    function excel(n, obj) {
    try {
    console.log('---Input---');
    console.log(obj);

    let keys = Object.keys(obj);
    for (let key of keys) {
    let value = obj[key];
    if (isNaN(value)) {//string
    obj[key] = polishPostfix(deRef(key, value, obj));
    }
    }
    for (let key of keys){
    let sorted_keys = keys.sort();
    for (let key of sorted_keys){
    console.log(key);
    console.log(obj[key]);
    }
    } catch (e) {
    console.log(e);


    }
    }

    /*function arrayToObj(arr){
    let obj = {};
    for (let i = 0; i < n; i++) {
    obj[arr[i*2]] = arr[i*2+1];
    }
    return obj
    }
    let n=3;
    let data = ['A1', '5', 'A2', 'A1 5 * B1 +', 'B1', '6'];
    excel(n, arrayToObj(data));*/

    excel(3, {
    A1: 5,
    @@ -75,11 +90,11 @@ excel(3, {
    });
    console.log('---------------');
    excel(3, {
    B2: 'B1 C1 /',
    A1: 5,
    A2: 'A1 5 * B2 +',
    B1: 6,
    B2: 'B1 C1 /',
    C1: 2
    C1: 0
    });

    console.log('---------------');
    @@ -88,11 +103,30 @@ excel(2, {
    A2: 'A1 5 +'
    });


    console.log('---------------');
    excel(4, {
    A1: 'B2 2 *',
    A2: 'B1 5 +',
    B1: 'B2 2 +',
    B2: 'A1 1 *'
    });
    });

    console.log('---------------');
    excel(1, {
    A1: 'B2 2 *',
    });

    console.log('---------------');
    excel(4, {
    B2: 'A1 2 *',
    A2: 'B1 5 +',
    B1: 'B2 2 +',
    A1: 'B2 1 *'
    });
    console.log('---------------');
    excel(4, {
    B2: 'A2 2 *',
    A2: 'A1 5 * B1 + B2 3 +',
    B1: 6,
    A1: 5
    });
  6. congnt24 revised this gist May 29, 2019. 1 changed file with 15 additions and 13 deletions.
    28 changes: 15 additions & 13 deletions excel.js
    Original file line number Diff line number Diff line change
    @@ -26,19 +26,6 @@ function polishPostfix(str) {
    }
    return st.pop();
    }

    let x = 4;
    let y = {
    A1: 5,
    A2: 'A1 5 * B2 +',
    B1: 6,
    B2: 'A1 B1 +'
    };
    let y2 = {
    A1: 'A2 2 *',
    A2: 'A1 5 +'
    };

    function deRef(key, str, obj, tmp = []) {
    // console.log(str);
    if (!isNaN(str)) {
    @@ -93,4 +80,19 @@ excel(3, {
    B1: 6,
    B2: 'B1 C1 /',
    C1: 2
    });

    console.log('---------------');
    excel(2, {
    A1: 'A2 2 *',
    A2: 'A1 5 +'
    });


    console.log('---------------');
    excel(4, {
    A1: 'B2 2 *',
    A2: 'B1 5 +',
    B1: 'B2 2 +',
    B2: 'A1 1 *'
    });
  7. congnt24 created this gist May 29, 2019.
    96 changes: 96 additions & 0 deletions excel.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    function polishPostfix(str) {
    let arr = str.split(' ');
    let st = [];
    for (let item of arr) {
    // console.log(st, item);
    let a;
    switch (item) {
    case '+':
    st.push(st.pop() * 1 + st.pop() * 1);
    break;
    case '-':
    a = st.pop();
    st.push(st.pop() - a);
    break;
    case '*':
    st.push(st.pop() * st.pop());
    break;
    case '/':
    a = st.pop();
    st.push(st.pop() / a);
    break;
    default:
    st.push(item);
    break;
    }
    }
    return st.pop();
    }

    let x = 4;
    let y = {
    A1: 5,
    A2: 'A1 5 * B2 +',
    B1: 6,
    B2: 'A1 B1 +'
    };
    let y2 = {
    A1: 'A2 2 *',
    A2: 'A1 5 +'
    };

    function deRef(key, str, obj, tmp = []) {
    // console.log(str);
    if (!isNaN(str)) {
    return str
    }
    let arr = str.split(' ');
    let len = arr.length;
    let ar2 = [];
    for (let i = 0; i < len; i++) {
    let item = arr[i];
    if (['+', '-', '*', '/'].includes(item) || !isNaN(item)) {
    ar2.push(item);
    } else {
    if (key === item) {
    throw `Circular dependency between ${key} and ${tmp[0]} detected`;
    }
    tmp.push(item);
    ar2.push(deRef(key, obj[item], obj, tmp));
    }
    }
    return ar2.join(' ');
    }

    function excel(n, obj) {
    try {
    let keys = Object.keys(obj);
    for (let key of keys) {
    let value = obj[key];
    if (isNaN(value)) {//string
    obj[key] = polishPostfix(deRef(key, value, obj));
    }
    }
    for (let key of keys){
    console.log(key);
    console.log(obj[key]);
    }
    } catch (e) {
    console.log(e);

    }
    }

    excel(3, {
    A1: 5,
    A2: 'A1 5 * B1 +',
    B1: 6
    });
    console.log('---------------');
    excel(3, {
    A1: 5,
    A2: 'A1 5 * B2 +',
    B1: 6,
    B2: 'B1 C1 /',
    C1: 2
    });