{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "// Like the naive algorithm but we break out of the inner loop as soon as our\n", "// mismatch budget exceeds the maximum allowed Hamming distance.\n", "func naive_approx_hamming(p string, t string, maxHammingDistance int) map[int]int {\n", " occurrences := make(map[int]int)\n", " for i := 0; i < len(t) - len(p) + 1; i++ { // for all alignments\n", " nmm := 0\n", " for j := 0; j < len(p); j++ { // for all characters\n", " if t[i+j] != p[j] { // does it match?\n", " nmm += 1 // mismatch\n", " if nmm > maxHammingDistance {\n", " break // exceeded maximum distance\n", " }\n", " }\n", " }\n", " if nmm <= maxHammingDistance {\n", " // approximate match; return pair where first element is the\n", " // offset of the match and second is the Hamming distance\n", " occurrences[i] = nmm\n", " }\n", " }\n", " return occurrences\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "map[0:0 7:2]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "naive_approx_hamming(\"needle\", \"needle noodle nargle\", 2)" ] } ], "metadata": { "kernelspec": { "display_name": "Go", "language": "go", "name": "gophernotes" }, "language_info": { "codemirror_mode": "", "file_extension": ".go", "mimetype": "", "name": "go", "nbconvert_exporter": "", "pygments_lexer": "", "version": "go1.11" } }, "nbformat": 4, "nbformat_minor": 2 }