yghern / pwp-capstones

0 stars 0 forks source link

A loop could reduce redundancy in calculating similarity scores #4

Open karl-project-review opened 6 years ago

karl-project-review commented 6 years ago

For reference, here is your code that calculates the similarity scores:

results = {}
results[lily_sample.author] = find_text_similarity(murderer_sample, lily_sample)
results[myrtle_sample.author] = find_text_similarity(murderer_sample, myrtle_sample)
results[gregg_sample.author] = find_text_similarity(murderer_sample, gregg_sample)
print(results)

The redundant lines in this code could be condensed using a loop, like so:

results = {}
samples = [lily_sample, myrtle_sample, gregg_sample]
for sample in samples:
    results[sample.author] = find_text_similarity(murderer_sample, sample)
print(results)

While this technique doesn't save much code when only working with three samples, it could save you more significant time if you were working with a larger sample set, so it is worth keeping this kind of approach in mind.

yghern commented 6 years ago

Well, if we'd caught just one more suspect, I would consider to just use for loop. :) For three, with relatively easy function definition and limited argument list - I just use my method for readibility sake. Few almost-identical lines makes obvious for me what is different (I can easily ignore "constant" part of line and focus on "variable" parts). But the again, agreed, if there would be more lines or more complicated function, I would also use for loop as described.