mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-22 14:43:10 -05:00
Compare commits
5 commits
3f28e82a4e
...
b5023e5a2a
Author | SHA1 | Date | |
---|---|---|---|
b5023e5a2a | |||
1a014f10dd | |||
8a1efbaf0f | |||
659cce59ff | |||
8fbf44fecb |
34 changed files with 19143 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -67,6 +67,7 @@ dkms.conf
|
||||||
|
|
||||||
## CMake
|
## CMake
|
||||||
|
|
||||||
|
build/
|
||||||
CMakeLists.txt.user
|
CMakeLists.txt.user
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
CMakeFiles
|
CMakeFiles
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
18
cpp/making-the-grade/.exercism/config.json
Normal file
18
cpp/making-the-grade/.exercism/config.json
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"vaeng"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"making_the_grade.cpp"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"making_the_grade_test.cpp"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/exemplar.cpp"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"icon": "grep",
|
||||||
|
"blurb": "Learn about arrays and vectors by grading and organizing your students exam scores."
|
||||||
|
}
|
1
cpp/making-the-grade/.exercism/metadata.json
Normal file
1
cpp/making-the-grade/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"cpp","exercise":"making-the-grade","id":"9ffd875e6d974169a7bac874164ad010","url":"https://exercism.org/tracks/cpp/exercises/making-the-grade","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
67
cpp/making-the-grade/CMakeLists.txt
Normal file
67
cpp/making-the-grade/CMakeLists.txt
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
# Basic CMake project
|
||||||
|
cmake_minimum_required(VERSION 3.5.1)
|
||||||
|
|
||||||
|
# Get the exercise name from the current directory
|
||||||
|
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
|
||||||
|
|
||||||
|
# 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 $<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)
|
||||||
|
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 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
|
||||||
|
# added "-Wno-unused-parameter" to remove compiler warnings
|
||||||
|
# should make it easier for students to run their first real code
|
||||||
|
# ignore sign compare, students don't know unsigned yet
|
||||||
|
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror -Wno-unused-parameter -Wno-sign-compare"
|
||||||
|
)
|
||||||
|
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})
|
61
cpp/making-the-grade/HELP.md
Normal file
61
cpp/making-the-grade/HELP.md
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
# 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
|
||||||
|
|
||||||
|
When you start a new exercise locally, the files are configured so that only the first test is performed.
|
||||||
|
Get that 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 line `if defined(EXERCISM_RUN_ALL_TESTS)` past the next test.
|
||||||
|
|
||||||
|
See the example below from the Bob exercise (file `bob_test.cpp`, line 15):
|
||||||
|
|
||||||
|
```diff
|
||||||
|
-#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||||
|
TEST_CASE("shouting")
|
||||||
|
{
|
||||||
|
REQUIRE("Whoa, chill out!" == bob::hey("WATCH OUT!"));
|
||||||
|
}
|
||||||
|
+#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||||
|
```
|
||||||
|
|
||||||
|
Moving this line past the next test 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++17 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 making_the_grade.cpp` 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)
|
||||||
|
- The [C++ track's programming category on the forum](https://forum.exercism.org/c/programming/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
|
51
cpp/making-the-grade/HINTS.md
Normal file
51
cpp/making-the-grade/HINTS.md
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
# Hints
|
||||||
|
|
||||||
|
## General
|
||||||
|
|
||||||
|
- `while` loops are used for _indefinite_ (uncounted) iteration
|
||||||
|
- `for` loops are used for _definite_, (counted) iteration.
|
||||||
|
- The keywords `break` and `continue` help customize loop behavior.
|
||||||
|
- `std::to_string()` from `string` library can be used to convert a integer to string
|
||||||
|
|
||||||
|
Also being familiar with the following can help with completing the tasks:
|
||||||
|
|
||||||
|
- [`vectors`][vectors]: indexing, size, [`<vector>.emplace_back`][emplace], [`<vector>.pop_back()`][pop].
|
||||||
|
- [`string`][string]: using the `+` to concatenate strings, integer to string conversion, [`to_string`][to\_string].
|
||||||
|
|
||||||
|
## 1. Rounding Scores
|
||||||
|
|
||||||
|
- `While` loops will continue to execute until their test condition evaluates to `False`.
|
||||||
|
- `<vector>.back()` will return the last item in a `vector`.
|
||||||
|
- `<vector>.pop_back()` will remove the last item in a `vector`.
|
||||||
|
|
||||||
|
## 2. Non-Passing Students
|
||||||
|
|
||||||
|
- A results counter does need to be set up and _incremented_ -- you'll want to `return` the count of non-passing students when the loop terminates.
|
||||||
|
|
||||||
|
## 3. The "Best"
|
||||||
|
|
||||||
|
- - Having an empty `vector` to add the "best" marks to is helpful here.
|
||||||
|
- `<vector>.emplace_back()` can help add things to the results `vector`.
|
||||||
|
|
||||||
|
## 4. Calculating Letter Grades
|
||||||
|
|
||||||
|
- These are _lower thresholds_. The _lower threshold_ for a "D" is a score of **41**, since an "F" is **<= 40**.
|
||||||
|
- `static_cast<int>` without parameters should round off increments nicely.
|
||||||
|
- You are expected to return an array, not a vector.
|
||||||
|
|
||||||
|
## 5. Matching Names to Scores
|
||||||
|
|
||||||
|
- If both containers are the same length and sorted the same way, could you use the `index` from one to retrieve a `value` from the other?
|
||||||
|
- `std::to_string(int)` can be used to convert a number to string.
|
||||||
|
- Don't forget the follow the format of the example's output.
|
||||||
|
|
||||||
|
## 6. A "Perfect" Score
|
||||||
|
|
||||||
|
- There may be or may not be a student with a score of 100, and you can't return an empty string without checking **all** scores.
|
||||||
|
- The `control flow` statements `continue` and `break` may be useful here to move past unwanted values.
|
||||||
|
|
||||||
|
[vectors]: https://en.cppreference.com/w/cpp/container/vector
|
||||||
|
[string]: https://en.cppreference.com/w/cpp/string/basic_string
|
||||||
|
[to\_string]: https://en.cppreference.com/w/cpp/string/basic_string/to_string
|
||||||
|
[emplace]: https://en.cppreference.com/w/cpp/container/vector/emplace_back
|
||||||
|
[pop]: https://en.cppreference.com/w/cpp/container/vector/pop_back
|
202
cpp/making-the-grade/README.md
Normal file
202
cpp/making-the-grade/README.md
Normal file
|
@ -0,0 +1,202 @@
|
||||||
|
# Making the Grade
|
||||||
|
|
||||||
|
Welcome to Making the Grade on Exercism's C++ Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## Arrays and Vectors
|
||||||
|
|
||||||
|
C++ offers different containers to store elements of the same type in an ordered way.
|
||||||
|
There is `std::array` for containers of a fixed size and `std::vector`, which comes with dynamic resizing capabilities.
|
||||||
|
|
||||||
|
### Construction
|
||||||
|
|
||||||
|
When you declare an array or a vector you need to specify the type of elements, that container will hold.
|
||||||
|
Arrays also need a size.
|
||||||
|
Look at these examples to see the two container types' initializations:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// std::array<element_type, size> variable_name {list of elements}
|
||||||
|
std::array<std::string, 3> indie_rock {"yeah", "yeah", "yeah"};
|
||||||
|
// indie_rock contains the elements "yeah" three times
|
||||||
|
```
|
||||||
|
|
||||||
|
Vectors usually need more space, as they allocate memory for further growth.
|
||||||
|
You do not need to specify a size:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// std::vector<element_type> variable_name {list of elements}
|
||||||
|
std::vector<int> countdown {3, 2, 1};
|
||||||
|
// countdown contains the elements 3, 2 and 1
|
||||||
|
```
|
||||||
|
|
||||||
|
> Note: You do not need to know the exact mechanics behind the template concept yet, it will have its own concept further down the syllabus tree.
|
||||||
|
|
||||||
|
### Element access
|
||||||
|
|
||||||
|
Vectors and arrays share the same functions to access their elements.
|
||||||
|
You can use the member functions `front` and `back` to get the first and last elements of the container.
|
||||||
|
There is also `at` and the `[]` operator to access specific elements.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
countdown[0];
|
||||||
|
// => 3
|
||||||
|
countdown.at(2);
|
||||||
|
// => 1
|
||||||
|
countdown[1] = 4;
|
||||||
|
// countdown now contains 3, 4 and 1
|
||||||
|
indie_rock.back() = "yeahs";
|
||||||
|
// indie_rock is now "yeah", "yeah", "yeahs"
|
||||||
|
```
|
||||||
|
|
||||||
|
> Note: If you pick a position that is not in the range of your container, `[]` might lead to undefined behavior.
|
||||||
|
> The `at` function would raise an exception, which might make your life easier in the long term.
|
||||||
|
|
||||||
|
### Vector modifiers
|
||||||
|
|
||||||
|
As a `vector` is not fixed in size, it is possible to add or remove elements.
|
||||||
|
Two common functions for that purpose are `emplace_back` and `pop_back`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::vector<std::string> witches {"Holly", "Alyssa", "Shannen"};
|
||||||
|
witches.pop_back();
|
||||||
|
// Shannen is no longer with the witches
|
||||||
|
witches.emplace_back("Rose");
|
||||||
|
// Rose has joined the team
|
||||||
|
```
|
||||||
|
|
||||||
|
### Capacity
|
||||||
|
|
||||||
|
You can check the containers for emptiness with the member function `empty`.
|
||||||
|
If you want to know the number of elements, you can use `size`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
You're a teaching assistant correcting student exams.
|
||||||
|
Keeping track of results manually is getting both tedious and mistake-prone.
|
||||||
|
You decide to make things a little more interesting by putting together some functions to count and calculate results for the class.
|
||||||
|
|
||||||
|
## 1. Rounding Scores
|
||||||
|
|
||||||
|
While you can give "partial credit" on exam questions, overall exam scores have to be `int`s.
|
||||||
|
So before you can do anything else with the class scores, you need to go through the grades and turn any `double` scores into `int`s.
|
||||||
|
|
||||||
|
Create the function `round_down_scores()` that takes a `vector` of `student_scores`.
|
||||||
|
The schools' requested rounding is a truncation.
|
||||||
|
Lucky for you, C++ can cast one type into another with `static_cast`.
|
||||||
|
The `static_cast` conversion from `double` to `int` cuts off any digit after the decimal.
|
||||||
|
This function should take the input `vector` and `return` a new vector with all the scores converted to `int`s.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::vector<double> student_scores {90.33, 40.5, 55.44, 70.05, 30.55, 25.45, 80.45, 95.3, 38.7, 40.3};
|
||||||
|
round_down_scores(student_scores)
|
||||||
|
// => {90, 40, 55, 70, 30, 25, 80, 95, 38, 40}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Non-Passing Students
|
||||||
|
|
||||||
|
As you were grading the exam, you noticed some students weren't performing as well as you'd hoped.
|
||||||
|
But you were distracted, and forgot to note exactly _how many_ students.
|
||||||
|
|
||||||
|
Create the function `count_failed_students()` that takes a `vector` of `student_scores`.
|
||||||
|
This function should count up the number of students who don't have passing scores and return that count as an integer.
|
||||||
|
A student needs a score greater than **40** to achieve a passing grade on the exam.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
count_failed_students({90,40,55,70,30,25,80,95,38,40});
|
||||||
|
// => 5
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. The "Best"
|
||||||
|
|
||||||
|
The teacher you're assisting wants to find the group of students who've performed "the best" on this exam.
|
||||||
|
What qualifies as "the best" fluctuates, so you need to find the student scores that are **greater than or equal to** the current threshold.
|
||||||
|
|
||||||
|
Create the function `above_threshold()` taking `student_scores` (a `vector` of grades), and `threshold` (an `int`, the "top score" threshold) as parameters.
|
||||||
|
This function should return a `vector` of all scores that are `>=` to `threshold`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
above_threshold({90,40,55,70,30,68,70,75,83,96}, 75);
|
||||||
|
// => {90,75,83,96}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. Calculating Letter Grades
|
||||||
|
|
||||||
|
The teacher you're assisting likes to assign letter grades as well as numeric scores.
|
||||||
|
Since students rarely score 100 on an exam, the "letter grade" lower thresholds are calculated based on the highest score achieved, and increment evenly between the high score and the failing threshold of **<= 40**.
|
||||||
|
|
||||||
|
Create the function `letter_grades()` that takes the "highest" score on the exam as a parameter, and returns an `array` (not a vector) of lower score thresholds for each "American style" grade interval: `["D", "C", "B", "A"]`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
/*Where the highest score is 100, and failing is <= 40.
|
||||||
|
"F" <= 40
|
||||||
|
41 <= "D" <= 55
|
||||||
|
56 <= "C" <= 70
|
||||||
|
71 <= "B" <= 85
|
||||||
|
86 <= "A" <= 100
|
||||||
|
*/
|
||||||
|
|
||||||
|
letter_grades(100);
|
||||||
|
// => {41, 56, 71, 86}
|
||||||
|
|
||||||
|
|
||||||
|
/*Where the highest score is 88, and failing is <= 40.
|
||||||
|
"F" <= 40
|
||||||
|
41 <= "D" <= 52
|
||||||
|
53 <= "C" <= 64
|
||||||
|
65 <= "B" <= 76
|
||||||
|
77 <= "A" <= 88
|
||||||
|
*/
|
||||||
|
|
||||||
|
letter_grades(88);
|
||||||
|
// => {41, 53, 65, 77}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 5. Matching Names to Scores
|
||||||
|
|
||||||
|
You have exam scores in descending order, and the respective student names (sorted in the order of their exam scores).
|
||||||
|
You would like to match each student's name with their exam score and print out an overall class ranking.
|
||||||
|
|
||||||
|
Create the function `student_ranking()` with parameters `student_scores` and `student_names`.
|
||||||
|
Match each student name on the student_names `vector` with their score from the student_scores `vector`.
|
||||||
|
You can assume each argument `vector` is sorted from highest score(er) to lowest score(er).
|
||||||
|
The function should return a `vector` of strings with the format `<rank>. <student name>: <student score>`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::vector<int> student_scores {100, 99, 90, 84, 66, 53, 47};
|
||||||
|
std::vector<std::string> student_names {"Joci", "Sara","Kora","Jan","Indra","Bern", "Fred"};
|
||||||
|
student_ranking(student_scores, student_names)
|
||||||
|
// =>
|
||||||
|
// {"1. Joci: 100", "2. Sara: 99", "3. Kora: 90", "4. Jan: 84", "5. Indra: 66", "6. Bern: 53", "7. Fred: 47"}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6. A "Perfect" Score
|
||||||
|
|
||||||
|
Although a "perfect" score of 100 is rare on an exam, it is interesting to know if at least one student has achieved it.
|
||||||
|
|
||||||
|
Create the function `perfect_score()` with parameters `student_scores` and `student_names`.
|
||||||
|
The lists are the same as in task 5.
|
||||||
|
The function should `return` _the first_ `<name>` (as a string) of the student who scored 100 on the exam.
|
||||||
|
|
||||||
|
If no 100 scores are found, an empty string `""` should be returned.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
perfect_score({"Nur", "Tony", "Fatima"}, {90, 80, 100});
|
||||||
|
// => "Fatima"
|
||||||
|
|
||||||
|
perfect_score({"Nur", "Tony"}, {90, 80});
|
||||||
|
// => ""
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @vaeng
|
81
cpp/making-the-grade/making_the_grade.cpp
Normal file
81
cpp/making-the-grade/making_the_grade.cpp
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
#include <array>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// Round down all provided student scores.
|
||||||
|
std::vector<int> round_down_scores(std::vector<double> student_scores) {
|
||||||
|
return std::vector<int>(student_scores.begin(), student_scores.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count the number of failing students out of the group provided.
|
||||||
|
int count_failed_students(std::vector<int> student_scores) {
|
||||||
|
|
||||||
|
int count{0};
|
||||||
|
|
||||||
|
for (auto score : student_scores) {
|
||||||
|
if (score <= 40)
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine how many of the provided student scores were 'the best' based on
|
||||||
|
// the provided threshold.
|
||||||
|
std::vector<int> above_threshold(std::vector<int> student_scores,
|
||||||
|
int threshold) {
|
||||||
|
|
||||||
|
std::vector<int> res;
|
||||||
|
|
||||||
|
for (auto score : student_scores) {
|
||||||
|
if (score >= threshold)
|
||||||
|
res.emplace_back(score);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a list of grade thresholds based on the provided highest grade.
|
||||||
|
std::array<int, 4> letter_grades(int highest_score) {
|
||||||
|
|
||||||
|
int step{(highest_score - 40) / 4};
|
||||||
|
std::array<int, 4> grades{41, 0, 0, 0};
|
||||||
|
|
||||||
|
for (int i{1}; i < 4; ++i) {
|
||||||
|
grades[i] += grades[i - 1] + step;
|
||||||
|
}
|
||||||
|
|
||||||
|
return grades;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Organize the student's rank, name, and grade information in ascending order.
|
||||||
|
std::vector<std::string>
|
||||||
|
student_ranking(std::vector<int> student_scores,
|
||||||
|
std::vector<std::string> student_names) {
|
||||||
|
|
||||||
|
std::vector<std::string> res;
|
||||||
|
int len = student_scores.size();
|
||||||
|
|
||||||
|
for (int i{0}; i < len; i++) {
|
||||||
|
res.emplace_back(std::to_string(i + 1) + "." + " " + student_names[i] +
|
||||||
|
": " + std::to_string(student_scores[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a string that contains the name of the first student to make a perfect
|
||||||
|
// score on the exam.
|
||||||
|
std::string perfect_score(std::vector<int> student_scores,
|
||||||
|
std::vector<std::string> student_names) {
|
||||||
|
|
||||||
|
int len = student_scores.size();
|
||||||
|
for (int i{0}; i < len; i++) {
|
||||||
|
if (student_scores[i] == 100) {
|
||||||
|
return student_names[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "";
|
||||||
|
}
|
159
cpp/making-the-grade/making_the_grade_test.cpp
Normal file
159
cpp/making-the-grade/making_the_grade_test.cpp
Normal file
|
@ -0,0 +1,159 @@
|
||||||
|
#include "making_the_grade.cpp"
|
||||||
|
#ifdef EXERCISM_TEST_SUITE
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
#else
|
||||||
|
#include "test/catch.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
TEST_CASE("Check correct mark conversion (empty)", "[task_1]") {
|
||||||
|
vector<double> input{};
|
||||||
|
vector<int> expected{};
|
||||||
|
vector<int> actual = round_down_scores(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Check correct mark conversion (all < 0.5)", "[task_1]") {
|
||||||
|
vector<double> input{5.2, 77.1, 91.0};
|
||||||
|
vector<int> expected{5, 77, 91};
|
||||||
|
vector<int> actual = round_down_scores(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Check correct mark conversion (all mixed)", "[task_1]") {
|
||||||
|
vector<double> input{5.2, 77.1, 91.0, 42.9, 11.6544};
|
||||||
|
vector<int> expected{5, 77, 91, 42, 11};
|
||||||
|
vector<int> actual = round_down_scores(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Count failed students (none failed)", "[task_2]") {
|
||||||
|
vector<int> input{89, 85, 42, 57, 90, 100, 95, 48, 70, 96};
|
||||||
|
int expected{0};
|
||||||
|
int actual = count_failed_students(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Count failed students (some failed)", "[task_2]") {
|
||||||
|
vector<int> input{40, 40, 35, 70, 30, 41, 90};
|
||||||
|
int expected{4};
|
||||||
|
int actual = count_failed_students(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test threshold (empty)", "[task_3]") {
|
||||||
|
vector<int> input{};
|
||||||
|
int threshold{98};
|
||||||
|
vector<int> expected{};
|
||||||
|
vector<int> actual = above_threshold(input, threshold);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test threshold (some above threshold)", "[task_3]") {
|
||||||
|
vector<int> input{88, 29, 91, 64, 78, 43, 41, 77, 36, 50};
|
||||||
|
int threshold{80};
|
||||||
|
vector<int> expected{88, 91};
|
||||||
|
vector<int> actual = above_threshold(input, threshold);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test threshold (none above threshold)", "[task_3]") {
|
||||||
|
vector<int> input{88, 29, 91, 64, 78, 43, 41, 77, 36, 50};
|
||||||
|
int threshold{99};
|
||||||
|
vector<int> expected{};
|
||||||
|
vector<int> actual = above_threshold(input, threshold);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test threshold (some on threshold)", "[task_3]") {
|
||||||
|
vector<int> input{88, 29, 91, 64, 78, 43, 41, 77, 80, 80};
|
||||||
|
int threshold{80};
|
||||||
|
vector<int> expected{88, 91, 80, 80};
|
||||||
|
vector<int> actual = above_threshold(input, threshold);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test letter grades: 100", "[task_4]") {
|
||||||
|
int input{100};
|
||||||
|
array<int, 4> expected{41, 56, 71, 86};
|
||||||
|
array<int, 4> actual = letter_grades(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
TEST_CASE("Test letter grades: 97", "[task_4]") {
|
||||||
|
int input{97};
|
||||||
|
array<int, 4> expected{41, 55, 69, 83};
|
||||||
|
array<int, 4> actual = letter_grades(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
TEST_CASE("Test letter grades: 81", "[task_4]") {
|
||||||
|
int input{81};
|
||||||
|
array<int, 4> expected{41, 51, 61, 71};
|
||||||
|
array<int, 4> actual = letter_grades(input);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Rank one student", "[task_5]") {
|
||||||
|
vector<int> grades{82};
|
||||||
|
vector<string> names{"Betty"};
|
||||||
|
vector<string> expected{"1. Betty: 82"};
|
||||||
|
vector<string> actual = student_ranking(grades, names);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Rank several student", "[task_5]") {
|
||||||
|
vector<int> grades{100, 98, 92, 86, 70, 68, 67, 60};
|
||||||
|
vector<string> names{"Rui", "Betty", "Joci", "Yoshi",
|
||||||
|
"Kora", "Bern", "Jan", "Rose"};
|
||||||
|
vector<string> expected{"1. Rui: 100", "2. Betty: 98", "3. Joci: 92",
|
||||||
|
"4. Yoshi: 86", "5. Kora: 70", "6. Bern: 68",
|
||||||
|
"7. Jan: 67", "8. Rose: 60"};
|
||||||
|
vector<string> actual = student_ranking(grades, names);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("No perfect score", "[task_6]") {
|
||||||
|
vector<int> grades{11, 34, 92, 23, 70, 76, 67, 60};
|
||||||
|
vector<string> names{"Rui", "Betty", "Joci", "Yoshi",
|
||||||
|
"Kora", "Bern", "Jan", "Rose"};
|
||||||
|
string expected{""};
|
||||||
|
string actual = perfect_score(grades, names);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("One perfect score", "[task_6]") {
|
||||||
|
vector<int> grades{11, 34, 92, 23, 70, 76, 67, 100};
|
||||||
|
vector<string> names{"Rui", "Betty", "Joci", "Yoshi",
|
||||||
|
"Kora", "Bern", "Jan", "Rose"};
|
||||||
|
string expected{"Rose"};
|
||||||
|
string actual = perfect_score(grades, names);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Several perfect scores", "[task_6]") {
|
||||||
|
vector<int> grades{11, 100, 92, 23, 70, 100, 67, 100};
|
||||||
|
vector<string> names{"Rui", "Betty", "Joci", "Yoshi",
|
||||||
|
"Kora", "Bern", "Jan", "Rose"};
|
||||||
|
string expected{"Betty"};
|
||||||
|
string actual = perfect_score(grades, names);
|
||||||
|
|
||||||
|
REQUIRE(expected == actual);
|
||||||
|
}
|
||||||
|
#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||||
|
#endif
|
17937
cpp/making-the-grade/test/catch.hpp
Normal file
17937
cpp/making-the-grade/test/catch.hpp
Normal file
File diff suppressed because it is too large
Load diff
2
cpp/making-the-grade/test/tests-main.cpp
Normal file
2
cpp/making-the-grade/test/tests-main.cpp
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "catch.hpp"
|
Binary file not shown.
Binary file not shown.
19
zig/armstrong-numbers/.exercism/config.json
Normal file
19
zig/armstrong-numbers/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"ee7"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"armstrong_numbers.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_armstrong_numbers.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Determine if a number is an Armstrong number.",
|
||||||
|
"source": "Wikipedia",
|
||||||
|
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number"
|
||||||
|
}
|
1
zig/armstrong-numbers/.exercism/metadata.json
Normal file
1
zig/armstrong-numbers/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"armstrong-numbers","id":"1ca0d9a30cd74f64a84eb3638bbd3ef3","url":"https://exercism.org/tracks/zig/exercises/armstrong-numbers","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/armstrong-numbers/HELP.md
Normal file
53
zig/armstrong-numbers/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit armstrong_numbers.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [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.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-guide]: https://zig.guide/
|
||||||
|
[ziglings]: https://codeberg.org/ziglings/exercises
|
29
zig/armstrong-numbers/README.md
Normal file
29
zig/armstrong-numbers/README.md
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# Armstrong Numbers
|
||||||
|
|
||||||
|
Welcome to Armstrong Numbers on Exercism's Zig 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
|
||||||
|
|
||||||
|
- @ee7
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Wikipedia - https://en.wikipedia.org/wiki/Narcissistic_number
|
17
zig/armstrong-numbers/armstrong_numbers.zig
Normal file
17
zig/armstrong-numbers/armstrong_numbers.zig
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn isArmstrongNumber(num: u128) bool {
|
||||||
|
var digits: u8 = 0;
|
||||||
|
var digits_rem = num;
|
||||||
|
while (digits_rem != 0) : (digits_rem /= 10) {
|
||||||
|
digits += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum: u128 = 0;
|
||||||
|
var num_rem = num;
|
||||||
|
while (num_rem != 0) : (num_rem /= 10) {
|
||||||
|
sum += std.math.pow(u128, num_rem % 10, digits);
|
||||||
|
}
|
||||||
|
|
||||||
|
return num == sum;
|
||||||
|
}
|
56
zig/armstrong-numbers/test_armstrong_numbers.zig
Normal file
56
zig/armstrong-numbers/test_armstrong_numbers.zig
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const isArmstrongNumber = @import("armstrong_numbers.zig").isArmstrongNumber;
|
||||||
|
|
||||||
|
test "zero is an armstrong number" {
|
||||||
|
try testing.expect(isArmstrongNumber(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "single-digit numbers are armstrong numbers" {
|
||||||
|
try testing.expect(isArmstrongNumber(5));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "there are no two-digit armstrong numbers" {
|
||||||
|
try testing.expect(!isArmstrongNumber(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "three-digit number that is an armstrong number" {
|
||||||
|
try testing.expect(isArmstrongNumber(153));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "three-digit number that is not an armstrong number" {
|
||||||
|
try testing.expect(!isArmstrongNumber(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "four-digit number that is an armstrong number" {
|
||||||
|
try testing.expect(isArmstrongNumber(9_474));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "four-digit number that is not an armstrong number" {
|
||||||
|
try testing.expect(!isArmstrongNumber(9_475));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "seven-digit number that is an armstrong number" {
|
||||||
|
try testing.expect(isArmstrongNumber(9_926_315));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "seven-digit number that is not an armstrong number" {
|
||||||
|
try testing.expect(!isArmstrongNumber(9_926_314));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "33-digit number that is an armstrong number" {
|
||||||
|
try testing.expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "38-digit number that is not an armstrong number" {
|
||||||
|
try testing.expect(!isArmstrongNumber(99_999_999_999_999_999_999_999_999_999_999_999_999));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the largest and last armstrong number" {
|
||||||
|
try testing.expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the largest 128-bit unsigned integer value is not an armstrong number" {
|
||||||
|
try testing.expect(!isArmstrongNumber(340_282_366_920_938_463_463_374_607_431_768_211_455));
|
||||||
|
}
|
19
zig/pangram/.exercism/config.json
Normal file
19
zig/pangram/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"massivelivefun"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"pangram.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_pangram.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Determine if a sentence is a pangram.",
|
||||||
|
"source": "Wikipedia",
|
||||||
|
"source_url": "https://en.wikipedia.org/wiki/Pangram"
|
||||||
|
}
|
1
zig/pangram/.exercism/metadata.json
Normal file
1
zig/pangram/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"pangram","id":"8ca6b789549d4f8fb857e7b5d0ecb09f","url":"https://exercism.org/tracks/zig/exercises/pangram","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/pangram/HELP.md
Normal file
53
zig/pangram/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit pangram.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [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.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-guide]: https://zig.guide/
|
||||||
|
[ziglings]: https://codeberg.org/ziglings/exercises
|
40
zig/pangram/README.md
Normal file
40
zig/pangram/README.md
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# Pangram
|
||||||
|
|
||||||
|
Welcome to Pangram on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
You work for a company that sells fonts through their website.
|
||||||
|
They'd like to show a different sentence each time someone views a font on their website.
|
||||||
|
To give a comprehensive sense of the font, the random sentences should use **all** the letters in the English alphabet.
|
||||||
|
|
||||||
|
They're running a competition to get suggestions for sentences that they can use.
|
||||||
|
You're in charge of checking the submissions to see if they are valid.
|
||||||
|
|
||||||
|
~~~~exercism/note
|
||||||
|
Pangram comes from Greek, παν γράμμα, pan gramma, which means "every letter".
|
||||||
|
|
||||||
|
The best known English pangram is:
|
||||||
|
|
||||||
|
> The quick brown fox jumps over the lazy dog.
|
||||||
|
~~~~
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Your task is to figure out if a sentence is a pangram.
|
||||||
|
|
||||||
|
A pangram is a sentence using every letter of the alphabet at least once.
|
||||||
|
It is case insensitive, so it doesn't matter if a letter is lower-case (e.g. `k`) or upper-case (e.g. `K`).
|
||||||
|
|
||||||
|
For this exercise, a sentence is a pangram if it contains each of the 26 letters in the English alphabet.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @massivelivefun
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Wikipedia - https://en.wikipedia.org/wiki/Pangram
|
18
zig/pangram/pangram.zig
Normal file
18
zig/pangram/pangram.zig
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
const ascii = @import("std").ascii;
|
||||||
|
|
||||||
|
pub fn isPangram(str: []const u8) bool {
|
||||||
|
var alphabet = [_]u8{0} ** 26;
|
||||||
|
|
||||||
|
for (str) |c| {
|
||||||
|
if (!ascii.isAlphabetic(c)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
alphabet[ascii.toLower(c) - 97] += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (alphabet) |count| {
|
||||||
|
if (count == 0) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
48
zig/pangram/test_pangram.zig
Normal file
48
zig/pangram/test_pangram.zig
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const pangram = @import("pangram.zig");
|
||||||
|
|
||||||
|
test "empty sentence" {
|
||||||
|
try testing.expect(!pangram.isPangram(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "perfect lower case" {
|
||||||
|
try testing.expect(pangram.isPangram("abcdefghijklmnopqrstuvwxyz"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "only lower case" {
|
||||||
|
try testing.expect(pangram.isPangram("the quick brown fox jumps over the lazy dog"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "missing the letter 'x'" {
|
||||||
|
try testing.expect(!pangram.isPangram("a quick movement of the enemy will jeopardize five gunboats"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "missing the letter 'h'" {
|
||||||
|
try testing.expect(!pangram.isPangram("five boxing wizards jump quickly at it"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "with underscores" {
|
||||||
|
try testing.expect(pangram.isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "with numbers" {
|
||||||
|
try testing.expect(pangram.isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "missing letters replaced by numbers" {
|
||||||
|
try testing.expect(!pangram.isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "mixed case and punctuation" {
|
||||||
|
try testing.expect(pangram.isPangram("\"Five quacking Zephyrs jolt my wax bed.\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a-m and A-M are 26 different characters but not a pangram" {
|
||||||
|
try testing.expect(!pangram.isPangram("abcdefghijklm ABCDEFGHIJKLM"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "non-alphanumeric printable ASCII" {
|
||||||
|
try testing.expect(!pangram.isPangram(" !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
|
||||||
|
}
|
19
zig/scrabble-score/.exercism/config.json
Normal file
19
zig/scrabble-score/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"ee7"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"scrabble_score.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_scrabble_score.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Given a word, compute the Scrabble score for that word.",
|
||||||
|
"source": "Inspired by the Extreme Startup game",
|
||||||
|
"source_url": "https://github.com/rchatley/extreme_startup"
|
||||||
|
}
|
1
zig/scrabble-score/.exercism/metadata.json
Normal file
1
zig/scrabble-score/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"scrabble-score","id":"bb07d3f27fd543c2875b5f72e0b0316e","url":"https://exercism.org/tracks/zig/exercises/scrabble-score","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/scrabble-score/HELP.md
Normal file
53
zig/scrabble-score/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit scrabble_score.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [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.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-guide]: https://zig.guide/
|
||||||
|
[ziglings]: https://codeberg.org/ziglings/exercises
|
48
zig/scrabble-score/README.md
Normal file
48
zig/scrabble-score/README.md
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
# Scrabble Score
|
||||||
|
|
||||||
|
Welcome to Scrabble Score on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
[Scrabble][wikipedia] is a word game where players place letter tiles on a board to form words.
|
||||||
|
Each letter has a value.
|
||||||
|
A word's score is the sum of its letters' values.
|
||||||
|
|
||||||
|
[wikipedia]: https://en.wikipedia.org/wiki/Scrabble
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Your task is to compute a word's Scrabble score by summing the values of its letters.
|
||||||
|
|
||||||
|
The letters are valued as follows:
|
||||||
|
|
||||||
|
| Letter | Value |
|
||||||
|
| ---------------------------- | ----- |
|
||||||
|
| A, E, I, O, U, L, N, R, S, T | 1 |
|
||||||
|
| D, G | 2 |
|
||||||
|
| B, C, M, P | 3 |
|
||||||
|
| F, H, V, W, Y | 4 |
|
||||||
|
| K | 5 |
|
||||||
|
| J, X | 8 |
|
||||||
|
| Q, Z | 10 |
|
||||||
|
|
||||||
|
For example, the word "cabbage" is worth 14 points:
|
||||||
|
|
||||||
|
- 3 points for C
|
||||||
|
- 1 point for A
|
||||||
|
- 3 points for B
|
||||||
|
- 3 points for B
|
||||||
|
- 1 point for A
|
||||||
|
- 2 points for G
|
||||||
|
- 1 point for E
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @ee7
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup
|
18
zig/scrabble-score/scrabble_score.zig
Normal file
18
zig/scrabble-score/scrabble_score.zig
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn score(s: []const u8) u32 {
|
||||||
|
var total: u32 = 0;
|
||||||
|
for (s) |c| {
|
||||||
|
total += switch (std.ascii.toLower(c)) {
|
||||||
|
'a', 'e', 'i', 'o', 'u', 'l', 'n', 'r', 's', 't' => 1,
|
||||||
|
'd', 'g' => 2,
|
||||||
|
'b', 'c', 'm', 'p' => 3,
|
||||||
|
'f', 'h', 'v', 'w', 'y' => 4,
|
||||||
|
'k' => 5,
|
||||||
|
'j', 'x' => 8,
|
||||||
|
'q', 'z' => 10,
|
||||||
|
else => unreachable,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
70
zig/scrabble-score/test_scrabble_score.zig
Normal file
70
zig/scrabble-score/test_scrabble_score.zig
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const score = @import("scrabble_score.zig").score;
|
||||||
|
|
||||||
|
test "lowercase letter" {
|
||||||
|
const expected: u32 = 1;
|
||||||
|
const actual = score("a");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "uppercase letter" {
|
||||||
|
const expected: u32 = 1;
|
||||||
|
const actual = score("A");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valuable letter" {
|
||||||
|
const expected: u32 = 4;
|
||||||
|
const actual = score("f");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "short word" {
|
||||||
|
const expected: u32 = 2;
|
||||||
|
const actual = score("at");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "short, valuable word" {
|
||||||
|
const expected: u32 = 12;
|
||||||
|
const actual = score("zoo");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "medium word" {
|
||||||
|
const expected: u32 = 6;
|
||||||
|
const actual = score("street");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "medium, valuable word" {
|
||||||
|
const expected: u32 = 22;
|
||||||
|
const actual = score("quirky");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "long, mixed-case word" {
|
||||||
|
const expected: u32 = 41;
|
||||||
|
const actual = score("OxyphenButazone");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "english-like word" {
|
||||||
|
const expected: u32 = 8;
|
||||||
|
const actual = score("pinata");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "empty input" {
|
||||||
|
const expected: u32 = 0;
|
||||||
|
const actual = score("");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "entire alphabet available" {
|
||||||
|
const expected: u32 = 87;
|
||||||
|
const actual = score("abcdefghijklmnopqrstuvwxyz");
|
||||||
|
try testing.expectEqual(expected, actual);
|
||||||
|
}
|
Loading…
Reference in a new issue