Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ loops #171

Merged
merged 11 commits into from
Jul 17, 2023
4 changes: 4 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
- [vehicle-purchase](./vehicle-purchase/README.md)
- [leap](./leap/README.md)
- [darts](./darts/README.md)
- [interest-is-interesting](./interest-is-interesting/README.md)
- [reverse-string](./reverse-string/README.md)
- [armstrong-numbers](./armstrong-numbers/README.md)
- [bob](./bob/README.md)
26 changes: 26 additions & 0 deletions cpp/armstrong-numbers/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authors": [
"marvelou-s"
],
"contributors": [
"elyashiv",
"KevinWMatthews",
"patricksjackson"
],
"files": {
"solution": [
"armstrong_numbers.cpp",
"armstrong_numbers.h"
],
"test": [
"armstrong_numbers_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Determine if a number is an Armstrong number",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number"
}
1 change: 1 addition & 0 deletions cpp/armstrong-numbers/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"cpp","exercise":"armstrong-numbers","id":"941909d338374a82b67adee2646e5cbc","url":"https://exercism.org/tracks/cpp/exercises/armstrong-numbers","handle":"vpayno","is_requester":true,"auto_approve":false}
68 changes: 68 additions & 0 deletions cpp/armstrong-numbers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.hpp
$<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already includes a
# pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.hpp)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different header,
# we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.hpp
test/tests-main.cpp)
endif()

set_target_properties(
${exercise}
PROPERTIES CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(
${exercise} PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror")
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES COMPILE_DEFINITIONS_DEBUG
_SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(
test_${exercise} ALL
DEPENDS ${exercise}
COMMAND ${exercise})
55 changes: 55 additions & 0 deletions cpp/armstrong-numbers/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Help

## Running the tests

Running the tests involves running `cmake -G` and then using the build command appropriate for your platform.
Detailed instructions on how to do this can be found on the [Running the Tests](https://exercism.io/tracks/cpp/tests) page for C++ on exercism.io.

## Passing the Tests

Get the first test compiling, linking and passing by following the [three
rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
Create just enough structure by declaring namespaces, functions, classes,
etc., to satisfy any compiler errors and get the test to fail. Then write
just enough code to get the test to pass. Once you've done that,
uncomment the next test by moving the following line past the next test.

```C++
#if defined(EXERCISM_RUN_ALL_TESTS)
```

This may result in compile errors as new constructs may be invoked that
you haven't yet declared or defined. Again, fix the compile errors minimally
to get a failing test, then change the code minimally to pass the test,
refactor your implementation for readability and expressiveness and then
go on to the next test.

Try to use standard C++11 facilities in preference to writing your own
low-level algorithms or facilities by hand.

## Submitting your solution

You can submit your solution using the `exercism submit armstrong_numbers.cpp armstrong_numbers.h` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [C++ track's documentation](https://exercism.org/docs/tracks/cpp)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [`c++-faq` tag on StackOverflow](https://stackoverflow.com/tags/c%2b%2b-faq/info)
- [C++ FAQ from isocpp.com](https://isocpp.org/faq)
- [CppReference](http://en.cppreference.com/) is a wiki reference to the C++ language and standard library
- [C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers) is useful if you are new to C++, but have programmed in C
23 changes: 23 additions & 0 deletions cpp/armstrong-numbers/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.PHONY: clean
clean:
rm -rf ./build

.PHONY: test
test: clean
mkdir -pv ./build

@printf "\n"
@# each line is executed in a subshell, we have to daisy chain them so they
@# run in the build directory
cd ./build; export LDFLAGS="-lgcov --coverage" CXXFLAGS="--coverage"; cmake -DEXERCISM_RUN_ALL_TESTS=1 -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G 'Unix Makefiles' ../; if make; then printf "\n=== All Tests Passed ===\n"; else printf "\n=== Test Failure ===\n"; false; fi

.PHONY: coverage
coverage: test
@printf "\n"
find . -regextype posix-egrep -regex "^.*(tests-main|CompilerId).*[.](gcda|gcno)$$" -print -delete

@printf "\n"
find . -regextype posix-egrep -regex "^.*[.](gcda|gcno)27066"

@printf "\n"
gcovr --print-summary
40 changes: 40 additions & 0 deletions cpp/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Armstrong Numbers

Welcome to Armstrong Numbers on Exercism's C++ Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`

Write some code to determine whether a number is an Armstrong number.

[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number

## Source

### Created by

- @marvelou-s

### Contributed to by

- @elyashiv
- @KevinWMatthews
- @patricksjackson

### Based on

Wikipedia - https://en.wikipedia.org/wiki/Narcissistic_number

### My Solution

- [my solution]()
- [run-tests](./run-tests-cpp.txt)
28 changes: 28 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "armstrong_numbers.hpp"

namespace armstrong_numbers {

bool is_armstrong_number(int candidate) {
if (candidate < 10) {
return true;
}

const int digit_count = (int)std::log10(candidate) + 1;
int num = candidate;
int pow_total = 0;

while (num > 0) {
const int pow_temp = num % 10;
int pow_temp_total = 1;

for (int i = 0; i < digit_count; i++) {
pow_temp_total *= pow_temp;
}
pow_total += pow_temp_total;
num /= 10;
}

return pow_total == candidate;
}

} // namespace armstrong_numbers
12 changes: 12 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#if !defined(ARMSTRONG_NUMBERS_H)
#define ARMSTRONG_NUMBERS_H

#include <cmath>

namespace armstrong_numbers {

bool is_armstrong_number(int candidate);

} // namespace armstrong_numbers

#endif // ARMSTRONG_NUMBERS_H
14 changes: 14 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<key>diagnostics</key>
<array>
</array>
<key>files</key>
<array>
</array>
</dict>
</plist>
46 changes: 46 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif
#include "armstrong_numbers.hpp"

// Armstrong-numbers exercise test case data version 1.1.0

TEST_CASE("zero_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(0));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("single_digit_numbers_are_armstrong_numbers") {
REQUIRE(armstrong_numbers::is_armstrong_number(5));
}

TEST_CASE("there_are_no_2_digit_armstrong_numbers") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(10));
}

TEST_CASE("three_digit_number_that_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(153));
}

TEST_CASE("three_digit_number_that_is_not_an_armstrong_number") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(100));
}

TEST_CASE("four_digit_number_that_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(9474));
}

TEST_CASE("four_digit_number_that_is_not_an_armstrong_number") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(9475));
}

TEST_CASE("seven_digit_number_that_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(9926315));
}

TEST_CASE("seven_digit_number_that_is_not_an_armstrong_number") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(9926314));
}
#endif // !EXERCISM_RUN_ALL_TESTS
14 changes: 14 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers_test.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<key>diagnostics</key>
<array>
</array>
<key>files</key>
<array>
</array>
</dict>
</plist>
17 changes: 17 additions & 0 deletions cpp/armstrong-numbers/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/armstrong-numbers.dir/armstrong_numbers_test.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers_test.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers_test.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/armstrong-numbers.dir/armstrong_numbers.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/armstrong-numbers.dir/test/tests-main.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/test/tests-main.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/test/tests-main.cpp"
}
]
8 changes: 8 additions & 0 deletions cpp/armstrong-numbers/compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-std=c++17
-stdlib=libstdc++
-g
-Wall
-Wextra
-Wpedantic
-Werror
-Wno-unused-parameter
Loading