Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc1c0c94

Browse files
authored
add hamming distance algorithm (abranhe#30)
add hamming distance algorithm
2 parents2819d2b +3daa675 commitc1c0c94

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_checkimport*
2-
2+
from .hamming_distimport*
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+
defhamming_dist(seq1:str,seq2:str)->int:
11+
"""Compare hamming distance of two strings"""
12+
iflen(seq1)!=len(seq2):
13+
raiseValueError("length of strings are not the same")
14+
returnsum(c1!=c2forc1,c2inzip(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)

[8]ページ先頭

©2009-2025 Movatter.jp