-
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.
c++/prime-factors: download exercise
- Loading branch information
Showing
10 changed files
with
18,250 additions
and
0 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,29 @@ | ||
{ | ||
"authors": [ | ||
"LegalizeAdulthood" | ||
], | ||
"contributors": [ | ||
"cyborgsphinx", | ||
"elyashiv", | ||
"jackhughesweb", | ||
"KevinWMatthews", | ||
"kytrinyx", | ||
"patricksjackson" | ||
], | ||
"files": { | ||
"solution": [ | ||
"prime_factors.cpp", | ||
"prime_factors.h" | ||
], | ||
"test": [ | ||
"prime_factors_test.cpp" | ||
], | ||
"example": [ | ||
".meta/example.cpp", | ||
".meta/example.h" | ||
] | ||
}, | ||
"blurb": "Compute the prime factors of a given natural number.", | ||
"source": "The Prime Factors Kata by Uncle Bob", | ||
"source_url": "http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata" | ||
} |
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 @@ | ||
{"track":"cpp","exercise":"prime-factors","id":"3f5d8c377d0b4c86a3d442e280264426","url":"https://exercism.org/tracks/cpp/exercises/prime-factors","handle":"vpayno","is_requester":true,"auto_approve":false} |
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,64 @@ | ||
# 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}.h $<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}.h) | ||
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}.h 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}) |
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,52 @@ | ||
# 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][cpp-tests-instructions] page for C++ on exercism.org. | ||
|
||
## Passing the Tests | ||
|
||
Get the first test compiling, linking and passing by following the [three rules of test-driven development][three-laws-of-tdd]. | ||
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. | ||
[cpp-tests-instructions]: https://exercism.org/docs/tracks/cpp/tests | ||
[three-laws-of-tdd]: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd | ||
## Submitting your solution | ||
You can submit your solution using the `exercism submit prime_factors.cpp prime_factors.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 |
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,54 @@ | ||
# Prime Factors | ||
|
||
Welcome to Prime Factors on Exercism's C++ Track. | ||
If you need help running the tests or submitting your code, check out `HELP.md`. | ||
|
||
## Instructions | ||
|
||
Compute the prime factors of a given natural number. | ||
|
||
A prime number is only evenly divisible by itself and 1. | ||
|
||
Note that 1 is not a prime number. | ||
|
||
## Example | ||
|
||
What are the prime factors of 60? | ||
|
||
- Our first divisor is 2. 2 goes into 60, leaving 30. | ||
- 2 goes into 30, leaving 15. | ||
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3. | ||
- 3 goes cleanly into 15, leaving 5. | ||
- 3 does not go cleanly into 5. The next possible factor is 4. | ||
- 4 does not go cleanly into 5. The next possible factor is 5. | ||
- 5 does go cleanly into 5. | ||
- We're left only with 1, so now, we're done. | ||
|
||
Our successful divisors in that computation represent the list of prime | ||
factors of 60: 2, 2, 3, and 5. | ||
|
||
You can check this yourself: | ||
|
||
- 2 * 2 * 3 * 5 | ||
- = 4 * 15 | ||
- = 60 | ||
- Success! | ||
|
||
## Source | ||
|
||
### Created by | ||
|
||
- @LegalizeAdulthood | ||
|
||
### Contributed to by | ||
|
||
- @cyborgsphinx | ||
- @elyashiv | ||
- @jackhughesweb | ||
- @KevinWMatthews | ||
- @kytrinyx | ||
- @patricksjackson | ||
|
||
### Based on | ||
|
||
The Prime Factors Kata by Uncle Bob - http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata |
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,5 @@ | ||
#include "prime_factors.h" | ||
|
||
namespace prime_factors { | ||
|
||
} // namespace prime_factors |
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,8 @@ | ||
#if !defined(PRIME_FACTORS_H) | ||
#define PRIME_FACTORS_H | ||
|
||
namespace prime_factors { | ||
|
||
} // namespace prime_factors | ||
|
||
#endif // PRIME_FACTORS_H |
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,98 @@ | ||
#include "prime_factors.h" | ||
#ifdef EXERCISM_TEST_SUITE | ||
#include <catch2/catch.hpp> | ||
#else | ||
#include "test/catch.hpp" | ||
#endif | ||
|
||
TEST_CASE("_1_yields_empty") | ||
{ | ||
const std::vector<int> expected{}; | ||
|
||
const std::vector<int> actual{prime_factors::of(1)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
#if defined(EXERCISM_RUN_ALL_TESTS) | ||
TEST_CASE("_2_yields_2") | ||
{ | ||
const std::vector<int> expected{2}; | ||
|
||
const std::vector<int> actual{prime_factors::of(2)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_3_yields_3") | ||
{ | ||
const std::vector<int> expected{3}; | ||
|
||
const std::vector<int> actual{prime_factors::of(3)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_4_yields_2_2") | ||
{ | ||
const std::vector<int> expected{2, 2}; | ||
|
||
const std::vector<int> actual{prime_factors::of(4)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_6_yields_2_3") | ||
{ | ||
const std::vector<int> expected{2, 3}; | ||
|
||
const std::vector<int> actual{prime_factors::of(6)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_8_yields_2_2_2") | ||
{ | ||
const std::vector<int> expected{2, 2, 2}; | ||
|
||
const std::vector<int> actual{prime_factors::of(8)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_9_yields_3_3") | ||
{ | ||
const std::vector<int> expected{3, 3}; | ||
|
||
const std::vector<int> actual{prime_factors::of(9)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_27_yields_3_3_3") | ||
{ | ||
const std::vector<int> expected{3, 3, 3}; | ||
|
||
const std::vector<int> actual{prime_factors::of(27)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_625_yields_5_5_5_5") | ||
{ | ||
const std::vector<int> expected{5, 5, 5, 5}; | ||
|
||
const std::vector<int> actual{prime_factors::of(625)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
|
||
TEST_CASE("_901255_yields_5_17_23_461") | ||
{ | ||
const std::vector<int> expected{5, 17, 23, 461}; | ||
|
||
const std::vector<int> actual{prime_factors::of(901255)}; | ||
|
||
REQUIRE(expected == actual); | ||
} | ||
#endif |
Oops, something went wrong.