For this challenge, the task is to read a text file containing lists of filenames with extensions and determine which names are unique per row, ignoring the file extensions.
For example, consider the following text file:
foo.mp3|bar.txt|baz.mp3wub.mp3|wub.mp3|wub.mp3|wub.txt|wub.pngquux.mp3|quux.txt|thud.mp3
The expected output for this file is
foo.mp3|bar.txt|baz.mp3thud.mp3
After removing extensions, all three names are unique on line 1 so the entire line is unchanged.
However, after removing extensions on line two, all wub files aren't unique and therefore are not included in the output at all.
For line three, after removing extensions, files with the name quux are non-unique and are removed from the output. thud.mp3 is unique and is included in the output.Notes
Filenames in the text file are strictly alphanumeric with a single period. No paths are involved. The delimiter is always a pipe. Each line should be operated on independently from all others; no logic carries forward from line to line. Files won't be more than 500 lines and lines will never be longer than 100 characters.
I wasn't able to fix it using python.
My code:
def find_unique_filenames(text):
result = []
for line in text.split('\n'):
unique_names = set()
filenames = line.strip().split('|')
for filename in filenames:
name_without_extension = filename.split('.')[0]
unique_names.add(name_without_extension)
result_line = '|'.join(unique_names)
result.append(result_line)
return '\n'.join(result)
# Uncomment the next line if you want to test this module independently
# print(find_unique_filenames("foo.mp3|bar.txt|baz.mp3\nwub.mp3|wub.mp3|wub.mp3|wub.txt|wub.png\nquux.mp3|quux.txt|thud.mp3"))
Such usage of
setwill result in repeated element resulting in single one, whilst you are expected to find elements which appear exactly once.Consider following simple example if you are working with following data
if you are tasked with finding distinct elements answer is
if you are tasked with finding elements appearing exactly once answer is
You might use
collections.Counterfor counting elements.