Skip to content

Instantly share code, notes, and snippets.

@Calamari
Forked from 140bytes/LICENSE.txt
Created August 4, 2011 09:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Calamari/1124804 to your computer and use it in GitHub Desktop.
Save Calamari/1124804 to your computer and use it in GitHub Desktop.
140byt.es -- Click ↑↑ fork ↑↑ to play!

Hamming Distance

Calculates the hammind distance of two words with equal length. (atm: 63 bytes length)

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(
a, // first word to test
b, // second word to test
c, // result count
d // looping variable
) {
for(
c = // set sum to 0
d = 0; // both strings should have the same length for calculating the hamming distance
a[d]; // check if the char exists
++d // loop through the whole length
) {
c+=a[d]!=b[d]; // add all letters that are not the same
}
return c; // return the hamming distance
}
function(a,b,c,d){for(c=d=0;a[d];++d)c+=a[d]!=b[d];return c}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "hammingDistance",
"description": "This function calculates the hamming distance of two words with equal length.",
"keywords": [
"hamming",
"hammingDistance",
"distance",
"informationTheory"
]
}
<!DOCTYPE html>
<title>Calculate the Hamming Distance</title>
<div>Expected value: <b>2</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(a,b,c,d){for(c=d=0;a[d];++d)c+=a[d]!=b[d];return c};
document.getElementById( "ret" ).innerHTML = myFunction("abcd", "acbd");
</script>
@tsaniel
Copy link

tsaniel commented Aug 4, 2011

function(a,b,c,d){for(c=d=0;a[d++];)c+=a[d]!=b[d];return c} save 3 bytes.

@Calamari
Copy link
Author

Calamari commented Aug 4, 2011

Thanks, used that, now it is down to 61 bytes.

@p01
Copy link

p01 commented Aug 4, 2011

tsaniel: incrementing d in the condition makes the function skip the first character. Better move it inside the test updating c

59 bytes
function(a,b,c,d){for(c=d=0;a[d];c+=a[d]!=b[d++]);return c}

@Calamari
Copy link
Author

Calamari commented Aug 4, 2011

nice touch!

@tsaniel
Copy link

tsaniel commented Aug 5, 2011

Ah, I made a very careless mistake, thanks for reminding @p01.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment