Skip to content

Instantly share code, notes, and snippets.

@rosemichaele
Last active November 29, 2017 18:56
Show Gist options
  • Save rosemichaele/e407e8708e8d0777c14b76cb02ada274 to your computer and use it in GitHub Desktop.
Save rosemichaele/e407e8708e8d0777c14b76cb02ada274 to your computer and use it in GitHub Desktop.
Parses an input list of directory paths and returns a list of strings with information about the paths: See https://codefights.com/fight/iM6TDRMYJcbrTv23j/ for more details.
from collections import OrderedDict
def parse_directory_paths(calls):
calls_dict = OrderedDict()
paths = []
for c in calls:
s = c.split("/")[1:]
paths.append(s)
for p in paths:
if p[0] not in calls_dict:
calls_dict[p[0]] = {}
calls_dict[p[0]]["count"] = 1
calls_dict[p[0]]["children"] = OrderedDict()
else:
calls_dict[p[0]]["count"] += 1
if p[1] not in calls_dict[p[0]]["children"]:
calls_dict[p[0]]["children"][p[1]] = {}
calls_dict[p[0]]["children"][p[1]]["count"] = 1
calls_dict[p[0]]["children"][p[1]]["children"] = OrderedDict()
else:
calls_dict[p[0]]["children"][p[1]]["count"] += 1
if p[2] not in calls_dict[p[0]]["children"][p[1]]["children"]:
calls_dict[p[0]]["children"][p[1]]["children"][p[2]] = {}
calls_dict[p[0]]["children"][p[1]]["children"][p[2]]["count"] = 1
else:
calls_dict[p[0]]["children"][p[1]]["children"][p[2]]["count"] += 1
output = []
for project in calls_dict:
p_obj = calls_dict[project]
output.append("--{project} ({count})".format(project=project, count=p_obj["count"]))
for subproject in p_obj["children"]:
sp_obj = p_obj["children"][subproject]
output.append("----{subproject} ({count})".format(subproject=subproject, count=sp_obj["count"]))
for method in sp_obj["children"]:
m_obj = sp_obj["children"][method]
output.append("------{method} ({count})".format(method=method, count=m_obj["count"]))
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment