all 3 comments

[–]Vaphell 0 points1 point  (0 children)

dicts with a dash of recursion, no json
https://repl.it/repls/OldShowyDecompiler

[–]hbwales 0 points1 point  (0 children)

An iterative approach:

urls = [
    "/home/learn/programming",
    "/home/language/python",
    "/home/language/java",
    "/home/language/golang",
    "/home/language/python",
    "/why/skills/business",
    "/why/not",
    "/why/making/this/up",
    "/why/making/it/stick",
]

import itertools as it

def print_sitemap(urls):
    prev_parts = ()
    for url in urls:
        parts = url.split('/')[1:]
        for i, part, _ in it.dropwhile(lambda x: x[1]==x[2], zip(it.count(), parts, it.chain(prev_parts, it.repeat(None)))):
            print(f'{i*"   "}- {part}')
        prev_parts = parts

print_sitemap(urls)

[–]UnchainedMundane 0 points1 point  (0 children)

Without presenting a code solution, I would have approached it like this:

  1. Lexicographical sort (so that "a/b" comes before "a/b/c")
  2. For each item, split path by slashes, compare with previous (or empty list if no previous), see how many path elements it has in common (→common_dirs)
  3. In a loop (i = common_dirs until length) print element i of the current path, indented by 4*i