Skip to content

Add hamming_distance algorithm#213

Merged
vzvu3k6k merged 1 commit intoTheAlgorithms:masterfrom
ignasius-j-s:hamming_distance
Jan 3, 2024
Merged

Add hamming_distance algorithm#213
vzvu3k6k merged 1 commit intoTheAlgorithms:masterfrom
ignasius-j-s:hamming_distance

Conversation

@ignasius-j-s
Copy link
Copy Markdown
Contributor

No description provided.

Copy link
Copy Markdown
Contributor

@vzvu3k6k vzvu3k6k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@i-gnasius Apologies for the delayed review. Thank you for your contribution!

def hamming_distance(str1, str2)
abort 'Strings must be of the same length' unless str1.length == str2.length

str1.chars.zip(str2.chars).sum { |chr1, chr2| chr1 == chr2 ? 0 : 1 }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I think this code can be written a bit shorter as follows:

Suggested change
str1.chars.zip(str2.chars).sum { |chr1, chr2| chr1 == chr2 ? 0 : 1 }
str1.chars.zip(str2.chars).count { |chr1, chr2| chr1 != chr2 }

@vzvu3k6k vzvu3k6k merged commit b976ea0 into TheAlgorithms:master Jan 3, 2024
@ignasius-j-s ignasius-j-s deleted the hamming_distance branch February 2, 2024 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants