Skip to content

Commit

Permalink
feat: add string/base/distances/hamming (#1166)
Browse files Browse the repository at this point in the history
PR-URL: 	#1166
Co-authored-by: Athan Reines <kgryte@gmail.com>
Reviewed-by: Athan Reines <kgryte@gmail.com>
Ref: #836
Ref: #151
  • Loading branch information
rgizz and kgryte authored Dec 15, 2023
1 parent 330fef2 commit 165ab63
Show file tree
Hide file tree
Showing 10 changed files with 664 additions and 0 deletions.
115 changes: 115 additions & 0 deletions lib/node_modules/@stdlib/string/base/distances/hamming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!--
@license Apache-2.0
Copyright (c) 2023 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# hammingDistance

> Calculate the [Hamming distance][hamming-distance] between two equal-length strings.
<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var hammingDistance = require( '@stdlib/string/base/distances/hamming' );
```

#### hammingDistance( s1, s2 )

Calculates the [Hamming distance][hamming-distance] between two equal-length strings.

```javascript
var dist = hammingDistance( 'frog', 'from' );
// returns 1

dist = hammingDistance( 'tooth', 'froth' );
// returns 2

dist = hammingDistance( 'cat', 'cot' );
// returns 1

dist = hammingDistance( '', '' );
// returns 0

dist = hammingDistance( '1638452297', '2311638451' );
// returns 10
```

</section>

<!-- /.usage -->

<!-- Package notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If the two strings differ in length, the [Hamming distance][hamming-distance] is not defined. Consequently, when provided two input strings of unequal length, the function returns a sentinel value of `-1`.
- As the function calculates the [Hamming distance][hamming-distance] by comparing UTF-16 code units, the function should behave as expected for strings composed of most characters. However, the function is likely to not behave as expected if strings contain visual characters composed of multiple Unicode code points, such as certain mathematical symbols and grapheme clusters (e.g., emojis).

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var hammingDistance = require( '@stdlib/string/base/distances/hamming' );

var dist = hammingDistance( 'algorithms', 'altruistic' );
// returns 7

dist = hammingDistance( 'elephant', 'Tashkent' );
// returns 6

dist = hammingDistance( 'javascript', 'typescript' );
// returns 4

dist = hammingDistance( 'hamming', 'ladybug' );
// returns 5
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[hamming-distance]: https://en.wikipedia.org/wiki/Hamming_distance

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pkg = require( './../package.json' ).name;
var hammingDistance = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var value;
var out;
var i;

values = [
[ 'algorithms', 'altruistic' ],
[ '1638452297', '4444884447' ],
[ '', '' ],
[ 'z', 'a' ],
[ 'aaappppk', 'aardvark' ],
[ 'frog', 'flog' ],
[ 'fly', 'ant' ],
[ 'elephant', 'hippopod' ],
[ 'hippopod', 'elephant' ],
[ 'hippo', 'zzzzz' ],
[ 'hello', 'hallo' ],
[ 'congratulations', 'conmgeautlatins' ]
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
value = values[ i%values.length ];
out = hammingDistance( value[0], value[1] );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

{{alias}}( s1, s2 )
Calculates the Hamming distance between two equal-length input strings.

The function returns a sentinel value of -1 if the input string lengths
differ.

Parameters
----------
s1: string
First input string.

s2: string
Second input string.

Returns
-------
out: number
Hamming distance.

Examples
--------
> var d = {{alias}}( 'algorithms', 'altruistic' )
7
> d = {{alias}}( 'elephant', 'hippopod' )
7
> d = {{alias}}( 'javascript', 'typescript' )
4
> d = {{alias}}( 'levenshtein', 'levitations' )
8
> d = {{alias}}( 'sacrifice', 'paradisal' )
8

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Calculates the Hamming distance between two equal-length strings.
*
* ## Notes
*
* - The function returns a sentinel value of `-1` if the input string lengths differ.
*
* @param str1 - first input string
* @param str2 - second input string
* @returns Hamming distance
*
* @example
* var dist = hammingDistance( 'fly', 'ant' );
* // returns 3
*
* @example
* var dist = hammingDistance( 'algorithms', 'altruistic' );
* // returns 7
*
* @example
* var dist = hammingDistance( 'hippopod', 'elephant' );
* // returns 7
*/
declare function hammingDistance( str1: string, str2: string ): number;


// EXPORTS //

export = hammingDistance;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import hammingDistance = require( './index' );


// TESTS //

// The function returns a number...
{
hammingDistance( '', '' ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a string...
{
hammingDistance( true, '' ); // $ExpectError
hammingDistance( false, '' ); // $ExpectError
hammingDistance( null, '' ); // $ExpectError
hammingDistance( undefined, '' ); // $ExpectError
hammingDistance( 5, '' ); // $ExpectError
hammingDistance( [], '' ); // $ExpectError
hammingDistance( {}, '' ); // $ExpectError
hammingDistance( ( x: number ): number => x, '' ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a string...
{
hammingDistance( '', true ); // $ExpectError
hammingDistance( '', false ); // $ExpectError
hammingDistance( '', null ); // $ExpectError
hammingDistance( '', undefined ); // $ExpectError
hammingDistance( '', 5 ); // $ExpectError
hammingDistance( '', [] ); // $ExpectError
hammingDistance( '', {} ); // $ExpectError
hammingDistance( '', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
hammingDistance(); // $ExpectError
hammingDistance( '' ); // $ExpectError
hammingDistance( '', '', 3 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var hammingDistance = require( './../lib' );

console.log( hammingDistance( 'algorithms', 'altruistic' ) );
// => 7

console.log( hammingDistance( 'elephant', 'hippopod' ) );
// => 7

console.log( hammingDistance( 'javascript', 'typescript' ) );
// => 4

console.log( hammingDistance( 'hamming', 'hamster' ) );
// => 4

console.log( hammingDistance( 'a', 'abcissa' ) );
// => -1
Loading

1 comment on commit 165ab63

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

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

Coverage Report

Package Statements Branches Functions Lines
string/base/distances/hamming $\color{green}118/118$
$\color{green}+100.00\%$
$\color{green}12/12$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}118/118$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please sign in to comment.