-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate tests from algorithm code files
- Loading branch information
Showing
12 changed files
with
489 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.