Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
6
Indexable
const readline = require('readline');
const process = require('process');
const { type } = require('os');

function commonAttr(obj1, obj2) {
  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);
  const commonKeys = keys1.filter(key => keys2.includes(key));
  return commonKeys;
}

function LongestCommonPathImpl(obj1, obj2) {
  if (typeof obj1 !== 'object' || typeof obj2 !== 'object') {
    return [];
  }
  let result = [];
  const commonKeys = commonAttr(obj1, obj2);
  for (let key of commonKeys) {
    const attr1 = obj1[key];
    const attr2 = obj2[key];
    const candidate = [key].concat(LongestCommonPathImpl(attr1, attr2));
    if (candidate.length > result.length) {
      result = candidate;
    }
  }
  return result;
}

// implement the function to merge configs
// customConf takes priority
function longestCommonPath(conf1, conf2) {
    // YOUR CODE GOES HERE   
    const result = LongestCommonPathImpl(conf1, conf2);
    if (result.length == 0) return "...";
    return result.join('.');
}

// this is provided function to read test cases 
// from stdin and write output to stdout 
// you don't have to modify it 
async function processCases() {
    const reader = readline.createInterface(
     process.stdin, process.stdout);
    let confs = [];
    for await (const line of reader) {
        const conf = JSON.parse(line);
        if (typeof conf === 'object') {
            confs.push(conf);
            if (confs.length === 2) {
                const common = longestCommonPath(confs[0], confs[1]);
                console.log(common);
                confs = [];
            }
        }
    }
}

processCases();
Editor is loading...
Leave a Comment