zhp098 / pwp-capstones

0 stars 0 forks source link

Cleaning up the frequency_comparison function #5

Open jmcrey opened 6 years ago

jmcrey commented 6 years ago
            if table1[key]>=table2[key]:
                appearances=appearances+table1[key]
                mutual_appearances=mutual_appearances+table2[key]
            if table2[key]>table1[key]:
                appearances=appearances+table2[key]
                mutual_appearances=mutual_appearances+table1[key]

We can clean this up a little bit using the += and else operators. Also, we want to continue to make use of spaces in our code to enhance user readability. Here is the code just a little bit more cleaned up.

            if table1[key] >= table2[key]:
                appearances += table1[key]
                mutual_appearances += table2[key]
            else:
                appearances += table2[key]
                mutual_appearances += table1[key]

Try to practice this same approach by cleaning up the last for loop of the function!

One last note: we only want to add the value should table1's value be greater than table2's value. So, we want to change our operator from >= to >. All those values that are equal to will get added to appearances at the end. So, our finished produce will be:

            if table1[key] > table2[key]:
                appearances += table1[key]
                mutual_appearances += table2[key]
            else:
                appearances += table2[key]
                mutual_appearances += table1[key]
zhp098 commented 6 years ago

That all makes sense! I'm always so nervous about using += incorrectly write it explicitly it out with Thing= Thing+New thing. But += is so much cleaner. Thank you for the clarification of >= verses = based on the problem statement!