Postgres – Hamming distance in plpython

CREATE OR REPLACE FUNCTION  util.hamming_distance (s1 text, s2 text)
  RETURNS integer
/*
  select * from util.hamming_distance ('hella3', 'hillo2')
*/
AS $$
   return sum([ch1 != ch2 for ch1, ch2 in zip(s1, s2)])
$$ LANGUAGE plpythonu;


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *