From 055ae4242fdf1e06e4eedfe830ebc8d9c01cfb7f Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Thu, 25 Feb 2016 09:54:27 +0300 Subject: [PATCH] hamming: inner_product version --- cpp/hamming/hamming.cpp | 18 ++++++++++++++++-- cpp/hamming/hamming.h | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cpp/hamming/hamming.cpp b/cpp/hamming/hamming.cpp index 6d32766..9f90183 100644 --- a/cpp/hamming/hamming.cpp +++ b/cpp/hamming/hamming.cpp @@ -1,10 +1,24 @@ #include "hamming.h" +#include +#include +#include +#include + namespace hamming { -int compute(const char *first, const char *second) { +using namespace std; - return 0; +int compute(const char *cstr1, const char *cstr2) { + + const string str1(cstr1); + const string str2(cstr2); + + if (str1.size() != str2.size()) + throw domain_error("Strings have different length"); + + return inner_product(str1.begin(), str1.end(), str2.begin(), 0, + plus(), not_equal_to()); } } diff --git a/cpp/hamming/hamming.h b/cpp/hamming/hamming.h index b848196..a72d932 100644 --- a/cpp/hamming/hamming.h +++ b/cpp/hamming/hamming.h @@ -1,5 +1,7 @@ #pragma once +#define EXERCISM_RUN_ALL_TESTS + namespace hamming { int compute(const char *first, const char *second);