more tests
This commit is contained in:
58
codility/binarygap.cpp
Normal file
58
codility/binarygap.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <cstdio>
|
||||
#include <bitset>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
auto bin2str(int num) -> std::string {
|
||||
constexpr std::size_t max_position = sizeof(int) * 8;
|
||||
std::string str{};
|
||||
for (std::size_t i = 0; i < max_position; ++i) {
|
||||
str += (num & (1 << i)) ? '1' : '0';
|
||||
if ((i + 1) % 8 == 0 && i < max_position - 1) str += "'";
|
||||
}
|
||||
std::reverse(std::begin(str), std::end(str));
|
||||
return str;
|
||||
}
|
||||
|
||||
int solution(int N) {
|
||||
constexpr std::size_t max_position = sizeof(int) * 8;
|
||||
std::size_t start = 0;
|
||||
std::size_t end = 0;
|
||||
|
||||
std::size_t final_start = 0;
|
||||
std::size_t final_end = 0;
|
||||
|
||||
for (std::size_t i = 0; i < max_position; ++i) {
|
||||
bool const is_one_start = (N & (1 << i)) >> i;
|
||||
if (is_one_start) {
|
||||
start = i;
|
||||
for (std::size_t j = start + 1; j < max_position; ++j) {
|
||||
bool const is_one_end = (N & (1 << j)) >> j;
|
||||
if (is_one_end) {
|
||||
end = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else continue;
|
||||
|
||||
if (end - start > final_end - final_start) {
|
||||
final_start = start;
|
||||
final_end = end - 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::intptr_t size = final_end - final_start;
|
||||
if (size > 0) return size;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
int input = 32;
|
||||
auto gap = solution(input);
|
||||
std::printf("Input: %d, [%s]\n", input, bin2str(input).c_str());
|
||||
std::printf("Gap: %d\n", gap);
|
||||
std::printf("%s\n", bin2str(1 << 0).c_str());
|
||||
std::printf("%s\n", bin2str(0x2&(1 << 0)).c_str());
|
||||
return 0;
|
||||
}
|
||||
40
futureskills/linebreaks.cpp
Normal file
40
futureskills/linebreaks.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
|
||||
std::string level1BreakIntoLines(std::string text, int width) {
|
||||
std::string str{};
|
||||
std::size_t col_count = 0;
|
||||
std::size_t space_index_text = 0;
|
||||
std::size_t space_index = 0;
|
||||
for (std::size_t i = 0; i < text.size(); ++i) {
|
||||
if (text[i] == ' ') {
|
||||
space_index_text = i;
|
||||
space_index = str.size();
|
||||
}
|
||||
str += text[i];
|
||||
if (col_count++ >= static_cast<std::size_t>(width)) {
|
||||
i = space_index_text;
|
||||
str.erase(space_index);
|
||||
col_count = 0;
|
||||
str += "\n";
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string level2BreakIntoLines(std::string text, int numLines) {
|
||||
std::size_t const max_column = std::size_t(std::ceil(double(text.size()) / double(numLines)));
|
||||
return level1BreakIntoLines(text, max_column);
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
auto str = level1BreakIntoLines("Hello, World! This is a very goodtest and we are not here to try it out.", 32);
|
||||
std::printf("%s\n", str.c_str());
|
||||
|
||||
std::printf("\n");
|
||||
|
||||
auto str1 = level2BreakIntoLines("The quick brown fox jumps over the lazy dog", 3);
|
||||
std::printf("%s\n", str1.c_str());
|
||||
return 0;
|
||||
}
|
||||
@@ -18,27 +18,26 @@ char* longestPalindrome(char * input_str) {
|
||||
|
||||
size_t c_start = 0;
|
||||
size_t c_end = 0;
|
||||
bool is_first = true;
|
||||
|
||||
while (start < len) {
|
||||
bool is_palindrome = false;
|
||||
end = start;
|
||||
DEBUG_LOG("start: %llu\n", start);
|
||||
DEBUG_LOG("start: %lu\n", start);
|
||||
for (size_t i = start; i < len; ++i) {
|
||||
const size_t dist = end - start + 1;
|
||||
is_palindrome = false;
|
||||
DEBUG_LOG(" dist: %llu:\n", dist);
|
||||
DEBUG_LOG(" dist: %lu:\n", dist);
|
||||
for (size_t j = 0; j < dist; ++j) {
|
||||
const size_t start_it = start + j;
|
||||
const size_t end_it = end - j;
|
||||
DEBUG_LOG(" [%llu]%c = [%llu]%c: %s\n",start_it, input_str[start_it], end_it, input_str[end_it], input_str[start_it] == input_str[end_it] ? "true" : "false");
|
||||
DEBUG_LOG(" [%lu]%c = [%lu]%c: %s\n",start_it, input_str[start_it], end_it, input_str[end_it], input_str[start_it] == input_str[end_it] ? "true" : "false");
|
||||
if (input_str[start_it] == input_str[end_it]) {
|
||||
is_palindrome = true;
|
||||
} else {
|
||||
is_palindrome = false;
|
||||
}
|
||||
}
|
||||
if (is_palindrome && end - start > c_end - c_start) break;
|
||||
if (is_palindrome) break;
|
||||
++end;
|
||||
}
|
||||
DEBUG_LOG("extracted: ");
|
||||
@@ -46,19 +45,18 @@ char* longestPalindrome(char * input_str) {
|
||||
DEBUG_LOG("%c", input_str[start + i]);
|
||||
}
|
||||
DEBUG_LOG("\n");
|
||||
DEBUG_LOG("[%llu] -> [%llu], c: [%llu] -> [%llu]\n", start, end, c_start, c_end);
|
||||
DEBUG_LOG("[%lu] -> [%lu], c: [%lu] -> [%lu]\n", start, end, c_start, c_end);
|
||||
DEBUG_LOG("\n");
|
||||
if (is_palindrome && (end - start > c_end - c_start || is_first)) {
|
||||
is_first = false;
|
||||
if (is_palindrome && end - start > c_end - c_start) {
|
||||
c_start = start;
|
||||
c_end = end;
|
||||
DEBUG_LOG("candidate: %llu -> %llu\n", c_start, c_end);
|
||||
DEBUG_LOG("candidate: %lu -> %lu\n", c_start, c_end);
|
||||
}
|
||||
++start;
|
||||
}
|
||||
|
||||
DEBUG_LOG("wow\n");
|
||||
DEBUG_LOG("[%llu] -> [%llu]: size %llu\n", c_start, c_end, c_end - c_start + 1);
|
||||
DEBUG_LOG("[%lu] -> [%lu]: size %lu\n", c_start, c_end, c_end - c_start + 1);
|
||||
char* str = (char*)malloc((c_end - c_start + 2) * sizeof(char));
|
||||
memcpy(str, input_str + start, (c_end - c_start + 1) * sizeof(char));
|
||||
str[c_end - c_start + 1] = '\0';
|
||||
|
||||
Reference in New Issue
Block a user