Skip to content

Commit c1c0c94

Browse files
authored
add hamming distance algorithm (#30)
add hamming distance algorithm
2 parents 2819d2b + 3daa675 commit c1c0c94

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

allalgorithms/string/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .palindrome_check import *
2-
2+
from .hamming_dist import *

allalgorithms/string/hamming_dist.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Binary search works for a sorted array.
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: ninexball
7+
# Github: @ninexball
8+
#
9+
10+
def hamming_dist(seq1: str, seq2: str) -> int:
11+
"""Compare hamming distance of two strings"""
12+
if len(seq1) != len(seq2):
13+
raise ValueError("length of strings are not the same")
14+
return sum(c1 != c2 for c1, c2 in zip(seq1, seq2))

docs/string/hamming-dist.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Hamming Distance
2+
3+
In informatics, Hamming distance is the number of positions where the characters differ between two strings of equal length
4+
5+
## Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
## Usage
12+
13+
```
14+
>>> from allalgorithms.sorting import hamming_dist
15+
16+
>>> hamming_dist("hello world", "hello wario")
17+
3
18+
```
19+
20+
## API
21+
22+
### hamming_dist(seq1, seq2)
23+
24+
> Returns an integer
25+
26+
> Raises a ValueError if strings are of unequal length
27+
28+
##### Params:
29+
30+
- `seq1`: first string to compare
31+
- `seq2`: second string to compare
32+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy