So given input as a txt file, we have to create an XML Tree or any hierarchical structure for easier parsing. And then find the last employee of the last CEO.
The txt gives the structure of a company where the columns are in the following order: name, salary, employer. Those with 'NOBODY' as employer are the CEOS in the company. The ones with the employer name work under the mentioned employer. The txt looks something like this:
Vineel Phatak, 520, NOBODY
Ajay Joshi, 250, Vineel Phatak
Abhishek Chauhan, 120, Ajay Joshi
Jayesh Godse, 500, NOBODY
Vijaya Mundada, 60, Abhishek Chauhan
Shital Tuteja, 45, Jayesh Godse
Rajan Gawli, 700, Vineel Phatak
Zeba Khan, 300, Jayesh Godse
Chaitali Sood, 100, Zeba Khan
Sheila Rodrigues, 35, Vineel Phatak
Given this, we have to something like this is to be accomplished:
Company
->Vineel Phatak
-->Ajay Joshi
--->Abhishek Chauhan
---->Vijaya Mundada
-->Rajan Gawli
-->Sheila Rodrigues
->Jayesh Godse
-->Shital Tuteja
-->Zeba Khan
--->Chaitali Sood
In XML Format:
<company>
<Vineel Phatak>
<Ajay Joshi>
<Abhishek Chauhan>
<Vijaya Mundada />
</Abhishek Chauhan>
</Ajay Joshi>
<Rajan Gawli />
<Sheila Rodrigues />
</Vineel Phatak>
<Jayesh Godse>
<Shital Tuteja />
<Zeba Khan>
<Chaitali Sood />
</Zeba Khan>
</Jayesh Godse>
</company>
What I tried doing is that after creating an element named company, since we need to add subelements to the root(company), I tried generating those and appending into a list. And then parsing through the list and comparing to get the values.
# Find last employee of the last introduced CEO
import xml.etree.ElementTree as ET
# Reading Input
inD = open('input.txt', 'r')
data = inD.readlines()
inD.close()
# Creating an element and saving all subelement to list
all_element = []
company = ET.Element('Company')
ceos = []
for i in data:
t = i.strip().split(',')
if(t[2].strip() == 'NOBODY'):
ceos.append(t[0])
all_element.append(ET.SubElement(company, t[0]))
# company.clear()
# Creating a function to add subelements
def findChilds(name, emp):
global all_element
for i in all_element:
if emp == i.tag:
name = ET.SubElement(i, name)
# If it is CEO hence no emplyer then directly add subelement to company or else add to the previous subelement
for j in data:
t = j.strip().split(',')
if t[2].strip() == 'NOBODY':
e = ET.SubElement(company, t[0])
elif t[2].strip() != 'NOBODY':
findChilds(t[0].strip(), t[2].strip())
ET.dump(company)
And the result is below:
<Company><Vineel Phatak><Ajay Joshi /><Rajan Gawli /><Sheila Rodrigues /></Vineel Phatak><Ajay Joshi><Abhishek Chauhan /></Ajay Joshi><Abhishek Chauhan><Vijaya Mundada /></Abhishek Chauhan><Jayesh Godse><Shital Tuteja /><Zeba Khan /></Jayesh Godse><Vijaya Mundada /><Shital Tuteja /><Rajan Gawli /><Zeba Khan><Chaitali Sood /></Zeba Khan><Chaitali Sood /><Sheila Rodrigues /><Vineel Phatak /><Jayesh Godse /></Company>
Which you can see isn't entirely correct. Also deleting the elements (line 18) isn't working as it refuses to add subelements other than the ceos
So at the end, we need to create this hierarchy and then print out the name of the last employee of the last CEO, which in this case is:
Last CEO: Jayesh Godse
Last Employee of CEO(direct or indirect, last to be introduced from the input): Chaitali Sood
Output:
Chaitali Sood
Also the number of CEO and number of Children and GrandChildren under them are not definite, neither are the names.
Im new to the ElementTree, so there might be some predefined functions that I may not have knowledge of, so please pardon my ignorance. Insights and suggestions are much appreciated. Thanks in advance!