Skip to content

Commit

Permalink
Solve isbn list
Browse files Browse the repository at this point in the history
  • Loading branch information
jorg-vr committed Aug 2, 2024
1 parent 9a8dde2 commit ef40fd8
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/exercises/isbn-list/solution/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <string>
#include <vector>
#include <any>
#include <cctype>

bool is_isbn10(const std::string &code) {
// Helper function for computing ISBN-10 check digit
auto check_digit = [](const std::string &code) -> char {
int check = 0;
for (size_t i = 0; i < 9; ++i) {
check += (i + 1) * (code[i] - '0');
}
check %= 11;
return (check == 10) ? 'X' : (check + '0');
};

// Check whether given code contains 10 characters
if (code.length() != 10) return false;

// Check whether first nine characters of given code are digits
for (size_t i = 0; i < 9; ++i) {
if (!isdigit(code[i])) return false;
}

// Check the check digit
return check_digit(code) == code[9];
}

bool is_isbn13(const std::string &code) {
// Helper function for computing ISBN-13 check digit
auto check_digit = [](const std::string &code) -> char {
int check = 0;
for (size_t i = 0; i < 12; ++i) {
check += ((i % 2 == 0) ? 1 : 3) * (code[i] - '0');
}
check = (10 - (check % 10)) % 10;
return check + '0';
};

// Check whether given code contains 13 characters
if (code.length() != 13) return false;

// Check whether first twelve characters of given code are digits
for (size_t i = 0; i < 12; ++i) {
if (!isdigit(code[i])) return false;
}

// Check the check digit
return check_digit(code) == code[12];
}

template <typename T>
bool is_isbn(const T &code, bool isbn13 = true) {
return isbn13 ? is_isbn13(code) : is_isbn10(code);
}


template <typename T>
std::vector<bool> are_isbn(const std::vector<T> &codes) {
std::vector<bool> checks;
for (const auto &code : codes) {
if(code.type() != typeid(std::string)){
checks.push_back(false);
continue;
}
std::string isbn = std::any_cast<std::string>(code);
bool isbn13 = isbn.length() == 13;

checks.push_back(is_isbn(code, isbn13));
}
return checks;
}

template <typename T>
std::vector<bool> are_isbn(const std::vector<T> &codes, bool isbn13) {
std::vector<bool> checks;
for (const auto &code : codes) {
checks.push_back(is_isbn(code, isbn13));
}
return checks;
}

0 comments on commit ef40fd8

Please sign in to comment.