Thread overview | |||||
---|---|---|---|---|---|
|
June 23, 2019 Using std.algorithm.iteration to Calculate Hamming Distance | ||||
---|---|---|---|---|
| ||||
D already has a function to calculate the Levenshtein distance[1]. I am trying to come up with a function to calculate the Hamming distance[2] between two strings, `a` and `b`. So far, this seems to work: foreach (i, j; zip(a, b)) { if (i != j) ++hammingDistance; } Is there a way to use any of the std.algorithm.iteration[3] algorithms such as `filter` or `map` to do this as well? [1] https://dlang.org/phobos/std_algorithm_comparison.html#levenshteinDistance [2] https://en.wikipedia.org/wiki/Hamming_distance [3] https://dlang.org/phobos/std_algorithm_iteration.html |
June 23, 2019 Re: Using std.algorithm.iteration to Calculate Hamming Distance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Samir | On Sunday, 23 June 2019 at 13:10:51 UTC, Samir wrote:
> D already has a function to calculate the Levenshtein distance[1]. I am trying to come up with a function to calculate the Hamming distance[2] between two strings, `a` and `b`. So far, this seems to work:
>
> foreach (i, j; zip(a, b)) {
> if (i != j)
> ++hammingDistance;
> }
zip( "hello world", "Hello World" ).map!"a[0] != a[1]".sum
|
June 23, 2019 Re: Using std.algorithm.iteration to Calculate Hamming Distance | ||||
---|---|---|---|---|
| ||||
Posted in reply to KnightMare | On Sunday, 23 June 2019 at 13:29:25 UTC, KnightMare wrote:
> zip( "hello world", "Hello World" ).map!"a[0] != a[1]".sum
Excellent! Thank you!
|
Copyright © 1999-2021 by the D Language Foundation