mercredi 24 mai 2017

Implement a group_by_owners dictionary

I made a successful attempt on the TestDome.com Fileowners problem and wanted to see if anyone had suggestions to simplify my answer. The online IDE uses Python 3.5.1. If you are trying to do the problem yourself and are just looking for an answer, here's one. I am by know means a Python expert so this took me quite a while to produce with lots of tinkering. Any comments at all would be helpful even if its about syntax or general cleanliness. THANKS!

Implement a group_by_owners function that:

Accepts a dictionary containing the file owner name for each file name. Returns a dictionary containing a list of file names for each owner name, in any order. For example, for dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt': 'Randy'} the group_by_owners function should return {'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}.

class FileOwners:

@staticmethod
def group_by_owners(files):
    val = (list(files.values()))                    #get values from dict
    val = set(val)                                  #make values a set to remove duplicates
    val = list(val)                                 #make set a list so we can work with it
    keyst = (list(files.keys()))                    #get keys from dict
    result = {}                                     #creat empty dict for output
    for i in range(len(val)):                       #loop over values(owners)
        for j in range(len(keyst)):                 #loop over keys(files)
            if val[i]==list(files.values())[j]:     #boolean to pick out files for current owner loop
                dummylist = [keyst[j]]              #make string pulled from dict a list so we can add it to the output in the correct format
                if val[i] in result:                #if the owner is already in the output add the new file to the existing dictionary entry
                    result[val[i]].append(keyst[j]) #add the new file
                else:                               #if the owner is NOT already in the output make a new entry 
                    result[val[i]] = dummylist      #make a new entry
    return result

files = {
'Input.txt': 'Randy',
'Code.py': 'Stan',
'Output.txt': 'Randy'
}

print(FileOwners.group_by_owners(files))

Output:

{'Stan': ['Code.py'], 'Randy': ['Output.txt', 'Input.txt']} 

Aucun commentaire:

Enregistrer un commentaire