Skip to content

feat: add string/base/distances/hamming #1166

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 25 commits into from
Dec 15, 2023
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
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