0

I'm doing some exercises where I need have a list of characters, and I need to calculate the "hamming distance" between all adjacent elements. THis means that if I have this list:

l = ["a","b","c","d"]

Then my output list should look like this:

list = [hamming("a","b"),hamming("b","c"),hamming("c","d")]

And then sum the values.

And I would like to do something nice looking and functional for this. I started by just using reduce, but I quickly realized that I would need some way of either "looking back" and the previous element, or "look forward" to the next element:

sum = functools.reduce(lambda acc , elem : acc + hammingDistance(elem, previous element???))

Trying to use mapping I of course run into the same issue. My idea is that there must be some clever way of using list comprehensions to do with. Perhaps it's not possible to also use it for summing in one go, but I can always do that after with sum()

Any good ideas?

0 Answers0