Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get unique name from a list of names

Given a list of names

names = [
         "bob john", 
         "billy james",
         "bob joe",
         "bob joe henry",
         "bob", 
         "bob john martin", 
         "billy james phillip",
         "billy james phillip mark"
]

How can I return a list of all the full names? i.e.

full_names = [
              "bob joe henry",
              "bob john martin", 
              "billy James phillip mark" 
]

Would using a trie be appropriate in this scenario?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think this is what you are looking for. based on the output it looks like they are asking for you to remove the entries where the names are the beginnings of another name. If you look at the full_names list, it is absent any names that had only the first portion of a name already in the list.

This short script will give you the same output as you requsted

names = [
         "bob john",
         "billy james",
         "bob joe",
         "bob joe henry",
         "bob",
         "bob john martin",
         "billy james phillip"
]

end = len(names) - 1

while end >= 0:
    name = names[end]
    for i,x in enumerate(names):
        if i != end and name in x:
            del names[end]
            break
    end -= 1

print(names)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading