Skip to content

add hamming distance algorithm #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion allalgorithms/string/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .palindrome_check import *

from .hamming_dist import *
14 changes: 14 additions & 0 deletions allalgorithms/string/hamming_dist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: UTF-8 -*-
#
# Binary search works for a sorted array.
# The All ▲lgorithms library for python
#
# Contributed by: ninexball
# Github: @ninexball
#

def hamming_dist(seq1: str, seq2: str) -> int:
"""Compare hamming distance of two strings"""
if len(seq1) != len(seq2):
raise ValueError("length of strings are not the same")
return sum(c1 != c2 for c1, c2 in zip(seq1, seq2))
32 changes: 32 additions & 0 deletions docs/string/hamming-dist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Hamming Distance

In informatics, Hamming distance is the number of positions where the characters differ between two strings of equal length

## Install

```
pip install allalgorithms
```

## Usage

```
>>> from allalgorithms.sorting import hamming_dist

>>> hamming_dist("hello world", "hello wario")
3
```

## API

### hamming_dist(seq1, seq2)

> Returns an integer

> Raises a ValueError if strings are of unequal length

##### Params:

- `seq1`: first string to compare
- `seq2`: second string to compare