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++ numbers #172

Merged
merged 16 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# Check if the files are present in the changed file list (added, modified, deleted) then output to stdout.
HAS_DIFF=false
printf "=== Which C++ files changed? ===\n"
if printf "%s\n" "${GIT_DIFF}" | grep -E '^(.*[.](cpp|h|hpp)|.*/[Ma]akefile|.github/workflows/cpp.yml)$'; then
if printf "%s\n" "${GIT_DIFF}" | grep -E '^(.*[.](cpp|h|hpp)|.*/[Ma]akefile|.*/CMakeLists.txt|.github/workflows/cpp.yml)$'; then
HAS_DIFF=true
fi
printf "\n"
Expand Down
2 changes: 2 additions & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
- [reverse-string](./reverse-string/README.md)
- [armstrong-numbers](./armstrong-numbers/README.md)
- [bob](./bob/README.md)
- [difference-of-squares](./difference-of-squares/README.md)
- [luhn](./luhn/README.md)
3 changes: 2 additions & 1 deletion cpp/darts/darts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace darts {

// NOLINTNEXTLINE(readability-identifier-length)
int score(double x, double y) {
double distance = std::hypot(x, y);
const double distance = std::hypot(x, y);

if (distance <= 1.0) {
return 10;
Expand Down
2 changes: 1 addition & 1 deletion cpp/darts/darts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace darts {

int score(double x, double y);
int score(double x, double y); // NOLINT(readability-identifier-length)

} // namespace darts

Expand Down
80 changes: 32 additions & 48 deletions cpp/darts/run-tests-cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Running automated test file(s):
Running: make clean
rm -rf ./build

real 0m0.006s
user 0m0.002s
sys 0m0.004s
real 0m0.017s
user 0m0.003s
sys 0m0.014s

===============================================================================

Expand Down Expand Up @@ -78,17 +78,17 @@ TOTAL 68 63 92%
lines: 92.6% (63 out of 68)
branches: 38.6% (85 out of 220)

real 0m10.058s
user 0m9.218s
sys 0m0.835s
real 0m9.722s
user 0m8.891s
sys 0m0.827s

===============================================================================

cmake-format --in-place CMakeLists.txt

real 0m0.157s
user 0m0.111s
sys 0m0.048s
real 0m0.144s
user 0m0.107s
sys 0m0.038s

===============================================================================

Expand All @@ -110,66 +110,50 @@ found lint:
Convention: 4


real 0m0.161s
user 0m0.121s
sys 0m0.041s
real 0m0.169s
user 0m0.123s
sys 0m0.046s

===============================================================================

Running: clang-format-16 -style=file -i ./darts.cpp ./darts_test.cpp

real 0m0.022s
user 0m0.010s
real 0m0.018s
user 0m0.006s
sys 0m0.012s

===============================================================================

Running: ../../.github/citools/cpp/clang-check
clang-check-16 --analyze ./darts.cpp ./darts_test.cpp ./darts.hpp

real 0m0.553s
user 0m0.517s
sys 0m0.036s
real 0m0.555s
user 0m0.526s
sys 0m0.029s


real 0m0.555s
user 0m0.519s
sys 0m0.036s
real 0m0.557s
user 0m0.526s
sys 0m0.031s

===============================================================================

Running: ../../.github/citools/cpp/clang-tidy | head -n 100
8744 warnings generated.
44098 warnings generated.
52839 warnings generated.
Suppressed 52839 warnings (52834 in non-user code, 5 NOLINT).
8739 warnings generated.
44091 warnings generated.
52830 warnings generated.
Suppressed 52843 warnings (52830 in non-user code, 13 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

real 0m8.772s
user 0m8.698s
sys 0m0.074s
Running: clang-tidy-16 ./darts.cpp ./darts_test.cpp ./darts.hpp
/home/vpayno/git_vpayno/exercism-workspace/cpp/darts/darts.cpp:5:18: warning: parameter name 'x' is too short, expected at least 3 characters [readability-identifier-length]
int score(double x, double y) {
^
/home/vpayno/git_vpayno/exercism-workspace/cpp/darts/darts.cpp:5:28: warning: parameter name 'y' is too short, expected at least 3 characters [readability-identifier-length]
int score(double x, double y) {
^
/home/vpayno/git_vpayno/exercism-workspace/cpp/darts/darts.cpp:6:5: warning: variable 'distance' of type 'double' can be declared 'const' [misc-const-correctness]
double distance = std::hypot(x, y);
^
const
/home/vpayno/git_vpayno/exercism-workspace/cpp/darts/darts.hpp:8:22: warning: parameter name 'x' is too short, expected at least 3 characters [readability-identifier-length]
int score(double x, double y);
^
/home/vpayno/git_vpayno/exercism-workspace/cpp/darts/darts.hpp:8:32: warning: parameter name 'y' is too short, expected at least 3 characters [readability-identifier-length]
int score(double x, double y);
^


real 0m8.774s
user 0m8.699s
real 0m8.648s
user 0m8.571s
sys 0m0.076s
Running: clang-tidy-16 ./darts.cpp ./darts_test.cpp ./darts.hpp


real 0m8.650s
user 0m8.571s
sys 0m0.079s

===============================================================================

30 changes: 30 additions & 0 deletions cpp/difference-of-squares/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"authors": [
"LegalizeAdulthood"
],
"contributors": [
"cyborgsphinx",
"elyashiv",
"jackhughesweb",
"KevinWMatthews",
"kytrinyx",
"patricksjackson",
"Scientifica96"
],
"files": {
"solution": [
"difference_of_squares.cpp",
"difference_of_squares.h"
],
"test": [
"difference_of_squares_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
"source": "Problem 6 at Project Euler",
"source_url": "http://projecteuler.net/problem=6"
}
1 change: 1 addition & 0 deletions cpp/difference-of-squares/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"cpp","exercise":"difference-of-squares","id":"1d6ee9dc443a4708a1986d9c1d94f29d","url":"https://exercism.org/tracks/cpp/exercises/difference-of-squares","handle":"vpayno","is_requester":true,"auto_approve":false}
68 changes: 68 additions & 0 deletions cpp/difference-of-squares/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/difference-of-squares/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 difference_of_squares.cpp difference_of_squares.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/difference-of-squares/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)12186"

@printf "\n"
gcovr --print-summary
46 changes: 46 additions & 0 deletions cpp/difference-of-squares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Difference of Squares

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

## Instructions

Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is
(1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is
1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first
ten natural numbers and the sum of the squares of the first ten
natural numbers is 3025 - 385 = 2640.

You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged.
Finding the best algorithm for the problem is a key skill in software engineering.

## Source

### Created by

- @LegalizeAdulthood

### Contributed to by

- @cyborgsphinx
- @elyashiv
- @jackhughesweb
- @KevinWMatthews
- @kytrinyx
- @patricksjackson
- @Scientifica96

### Based on

Problem 6 at Project Euler - http://projecteuler.net/problem=6

### My Solution

- [my solution]()
- [run-tests](./run-tests-cpp.txt)
Loading