Skip to content

Commit

Permalink
Restructure code and files
Browse files Browse the repository at this point in the history
Isolate tests from algorithm code files
  • Loading branch information
oboukli committed Jun 17, 2023
1 parent 89c7a0b commit bceb256
Show file tree
Hide file tree
Showing 13 changed files with 408 additions and 329 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
root = true

[*.{c,cpp,h}]
[*.{c,cpp,h,hpp}]
charset = utf-8
end_of_line = lf
indent_size = 4
Expand Down
45 changes: 45 additions & 0 deletions src/fizzbuzz.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.

// SPDX-License-Identifier: MIT

/// Problem source:
/// https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
///
/// Original problem text:
/// Write a program that prints the numbers from 1 to 100. But for multiples of
/// three print "Fizz" instead of the number and for the multiples of five
/// print "Buzz". For numbers which are multiples of both three and five print
/// "FizzBuzz".

#ifndef FIZZBUZZ_HPP_
#define FIZZBUZZ_HPP_

#include <sstream>
#include <string>

std::string fizzbuzz(int const n) noexcept {
std::ostringstream buffer;
for (int i{1}; i <= n; ++i) {
bool f{true};

if (i % 3 == 0) {
buffer << "Fizz";
f = false;
}

if (i % 5 == 0) {
buffer << "Buzz";
continue;
}

if (f) {
buffer << i;
}
}

return buffer.str();
}

#endif // FIZZBUZZ_HPP_
132 changes: 4 additions & 128 deletions src/lc-lru-cache/main.cpp → src/lc-lru-cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/// https://simontoth.substack.com/p/daily-bite-of-c-lru-cache
/// https://leetcode.com/problems/lru-cache/

#ifndef LC_LRU_CACHE_HPP_
#define LC_LRU_CACHE_HPP_

#include <cassert>
#include <cstdint>
#include <iterator>
Expand Down Expand Up @@ -175,131 +178,4 @@ class LRUCache final : public LRUCacheBase {

} // namespace forfun::lrucache

template <typename T>
std::enable_if_t<std::is_base_of_v<forfun::lrucache::LRUCacheBase, T>, void>
test() {
int volatile val{};

{
T cache{2};

cache.put(1, 1); // Cache is {{1, 1}}.

cache.put(2, 2); // Cache is {{1, 1, {2, 2}}.

val = cache.get(1);
assert(val == 1);

cache.put(
3, 3); // LRU key was 2. Evicts key 2. Cache is {{1, 1}, {3, 3}}.

val = cache.get(2); // Returns -1 (key not found).
assert(val == -1);

cache.put(
4, 4); // LRU key was 1; evicts key 1, cache is {{4, 4}, {3, 3}}.

val = cache.get(1);
assert(val == -1); // Returns -1 (key not found).

val = cache.get(3);
assert(val == 3);

val = cache.get(4);
assert(val == 4);
}

{
T cache(2);

cache.put(1, 1);

val = cache.get(1);
assert(val == 1);

cache.put(2, 2);

val = cache.get(2);
assert(val == 2);

cache.put(3, 3); // Evicts key 1.

val = cache.get(1);
assert(val == -1);

cache.put(2, 4);

val = cache.get(2);
assert(val == 4);

cache.put(4, 4); // Evicts key 3.

val = cache.get(3);
assert(val == -1);

val = cache.get(5);
assert(val == -1);
}

{
T cache(1);

cache.put(1, 1);

val = cache.get(1);
assert(val == 1);

cache.put(2, 2); // Evicts key 1.

val = cache.get(1);
assert(val == -1);

val = cache.get(2);
assert(val == 2);

cache.put(2, 3);

val = cache.get(2);
assert(val == 3);
}

{
T cache(3);

cache.put(1, 1);
cache.put(2, 2);
cache.put(3, 3);

val = cache.get(1);
assert(val == 1);

val = cache.get(2);
assert(val == 2);

val = cache.get(3);
assert(val == 3);

cache.put(4, 4); // Evicts key 1.

val = cache.get(1);
assert(val == -1);

val = cache.get(4);
assert(val == 4);

val = cache.get(2);
assert(val == 2);

cache.put(5, 5); // Evicts key 3, as key 2 was accessed recently.

val = cache.get(3);
assert(val == -1);
}
}

int main() {
test<forfun::lrucache::solution1::LRUCache>();
test<forfun::lrucache::solution1::LRUCache>();

return 0;
}
#endif // LC_LRU_CACHE_HPP_
36 changes: 36 additions & 0 deletions src/palindrome.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.

// SPDX-License-Identifier: MIT

// Problem source:
// https://en.wikipedia.org/wiki/Palindrome

#ifndef PALINDROME_HPP_
#define PALINDROME_HPP_

#include <cctype>
#include <concepts>
#include <string>

template <typename T = std::string, std::integral Tsize = typename T::size_type>
[[nodiscard]] bool is_palindrome(T const& s) noexcept {
Tsize const len = (s.length()) / Tsize{2};
auto begin = s.cbegin();
auto end = --(s.cend());

for (Tsize i{0}; i < len; ++i) {
if (std::tolower(static_cast<unsigned char>(*begin))
!= std::tolower(static_cast<unsigned char>(*end))) {
return false;
}

++begin;
--end;
}

return true;
}

#endif // PALINDROME_HPP_
31 changes: 31 additions & 0 deletions src/palindromic-number.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.

// SPDX-License-Identifier: MIT

// Problem source:
// https://en.wikipedia.org/wiki/Palindromic_number

#ifndef PALINDROMIC_NUMBER_HPP_
#define PALINDROMIC_NUMBER_HPP_

#include <cstdlib>

[[nodiscard]] bool is_palindrome(int const n) noexcept {
if (n < 0) {
return false;
}

int nn{0};
std::div_t d{.quot = n, .rem = 0};
while (d.quot > 0) {
nn *= 10;
d = std::div(d.quot, 10);
nn += d.rem;
}

return n == nn;
}

#endif // PALINDROMIC_NUMBER_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
///
/// Find the sum of all the multiples of 3 or 5 below 1000.

#ifndef PROJECT_EULER_P0001_MULTIPLES_OF_3_OR_5_HPP_
#define PROJECT_EULER_P0001_MULTIPLES_OF_3_OR_5_HPP_

#include <algorithm>
#include <cassert>

namespace {
[[nodiscard]] inline constexpr int sum_2x(int const n, int const q) noexcept {
Expand All @@ -30,36 +32,4 @@ namespace {
return (sum_2x(n, 3) + sum_2x(n, 5) - sum_2x(n, 15)) / 2;
}

int main() {
{
int const s{find_sum_mult_three_five(1)};
assert(s == 0);
}

{
int const s{find_sum_mult_three_five(4)};
assert(s == 3);
}

{
int s{find_sum_mult_three_five(6)};
assert(s == 8);
}

{
int const s{find_sum_mult_three_five(10)};
assert(s == 23);
}

{
int const s{find_sum_mult_three_five(11)};
assert(s == 33);
}

{
int s{find_sum_mult_three_five(1000)};
assert(s == 233168);
}

return 0;
}
#endif // PROJECT_EULER_P0001_MULTIPLES_OF_3_OR_5_HPP_
Loading

0 comments on commit bceb256

Please sign in to comment.