0

Given a list of employees and their bosses as a csv file , write a function that will print out a hierarchy tree of the employees.

Sample input from csv file

Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/ Fred, Sam,developer, 2010

The format is name, supervisor, designation, year of joining.

The output should be

Ian CEO 2007

-Sam Technical lead 2009

--Fred Developer 2010

I am not sure but I have tried it as below. Please suggest changes to this code or any other solutions you have.

strq = "Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/Fred, Sam, developer, 2010"
def treeEmployee(infoStr):
    str1 = infoStr.split("/")
    s2 = []
    for i in str1:
        s2.append(i.split(","))
    for i in range(len(s2)):
        for j in range(1, len(s2)):
            if s2[i][1] == s2[j][0]:
                s2[i], s2[j] = s2[j], s2[i]
            return s2

print treeEmployee(strq)

I want the output to be

Ian CEO 2007

-Sam Technical lead 2009

--Fred Developer 2010
Pranati G
  • 11
  • 5
  • Will a solution in javascript do? Or you are looking only in python? I am saying this as you attached the tag of JS – Ayan Jul 20 '16 at 03:47
  • 3
    fix your indentation please. – Julien Jul 20 '16 at 03:52
  • 1
    also defining a function is useless if you don't actually call it. – Julien Jul 20 '16 at 03:53
  • Code looks python to me. Probably consider removing the JS tag to clear things up. – Samuel Toh Jul 20 '16 at 03:54
  • Indentation is incredibly important in python. Different indentation creates different functionality. Make sure the formatting in the code you share on stack overflow matches the indentation you use to run your code. – dmlicht Jul 20 '16 at 03:56
  • I have indented the code. A solution in javascript or python would do. That is the reason I have added the both the tags. Please can anyone help solving this problem? – Pranati G Jul 20 '16 at 04:04
  • @PranatiG Your indentation is still broken, and you have a `return` outside of any function, and `str1` is not defined. – melpomene Jul 20 '16 at 04:17
  • @PranatiG Fix your indentation. As it is right now, `s2=[]` is defined outside the function, and all the lines after that are outside the function, including your `return` statement. That's why you're getting the error that return is outside the function. – ChaoticTwist Jul 20 '16 at 04:33
  • thanks got it. But still I dont get the correct output – Pranati G Jul 20 '16 at 04:37
  • Can you update your code so we can see the new indentation? – Božo Stojković Jul 20 '16 at 04:38
  • @PranatiG, I tried to run your code and currently what you've done is make every line a list. Can you update the question with the exact output that you are expecting. Are you expecting a list or a string? This is the current output: `[[' Ian', ' NULL', ' CEO', '2007'], ['Sam', ' Ian', ' technical lead', ' 2009 '], ['Fred', ' Sam', ' developer', ' 2010']]` – ChaoticTwist Jul 20 '16 at 04:41
  • Yes I am getting the same output. What am I doing wrong? – Pranati G Jul 20 '16 at 04:44

2 Answers2

0

Fixing the indentation like this should work. Indentation is extremely important in python.

strq = "Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/Fred, Sam, developer, 2010"
def treeEmployee(infoStr):
    str1 = infoStr.split("/")
    s2 = []
    for i in str1:
        s2.append(i.split(","))
    for i in range(len(s2)):
        for j in range(1, len(s2)):
            if s2[i][1] == s2[j][0]:
                s2[i], s2[j] = s2[j], s2[i]
                return s2
ChaoticTwist
  • 544
  • 7
  • 25
0

This generates a hierarchy tree from your inputs in JS.

function wrapper(str) {
  var elem;
  str = str.split('/');

  function tree(parent, arr, level) {
    if (!parent) {
      parent = 'NULL';
    }
    if (!arr) {
      arr = [];
    }
    if (!level) {
      level = 0;
    }
    var obj,
      children = findChild(parent);

    for (var i = 0, len = children.length; i < len; i += 1) {
      elem = children[i];
      obj = {
        name: elem[0],
        supervisor: parent,
        designation: elem[2],
        yearOfJoining: elem[3],
        children: [],
        level: level
      };
      arr.push(obj);
      console.log(dashGenerator(level) + obj.name + ' ' + obj.designation + ' ' + obj.yearOfJoining);
      tree(elem[0], obj.children, level + 1);
    }
    return arr;
  }

  function dashGenerator(level) {
    var str = '';
    for (var i = 0; i < level; i += 1) {
      str += '-';
    }
    return str;
  }

  function findChild(parent) {
    var child = [];
    for (var i = 0, len = str.length; i < len; i += 1) {
      elem = str[i].split(',')
      if (elem[1].trim() === parent.trim()) {
        child.push(elem);
      }
    }
    return child;
  }
  tree();
}
wrapper("Sam, Ian, technical lead, 2009 / Ian, NULL, CEO,2007/ Fred, Sam,developer, 2010");
Ayan
  • 2,300
  • 1
  • 13
  • 28
  • I will update the display part to make it compatible with the output given. – Ayan Jul 20 '16 at 04:44
  • @PranatiG You are welcome buddy. I have updated the displaying part. Please run the snippet, you get your results. – Ayan Jul 20 '16 at 04:50
  • There is still one glitch, as I am trimming all the space, the technicallead is getting trimmed. Need improvement there... – Ayan Jul 20 '16 at 04:51
  • Also if we are getting the input from csv file then? – Pranati G Jul 20 '16 at 04:55
  • Yea the space trimming is solved. I have made a wrapper function for you. It takes the raw CSV string and operates the data and logs it in console. http://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript might be useful for you to guide how to read the data. @PranatiG – Ayan Jul 20 '16 at 05:01
  • You are welcome. I loved to solve the problem. Please recognise the answer with an upvote/acceptance if you found it useful. Or let me know of the further improvements we can have here for the question. Thanks in advance. – Ayan Jul 20 '16 at 05:05