-
-
Notifications
You must be signed in to change notification settings - Fork 219
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
Add practice exercise Zebra Puzzle #788
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a programmatic way of solving this puzzle.
You can define enums, functions and other more complicated testing scenarios to achieve this goal.
Okay I will work on that👍 |
Thanks for the update! Do you think we could have the details of the story in the test file? So the result is not always the same and students cannot just answer "Norwegian"? So that we have a custom test case, where we give another story? I will ask the other maintainers if they did that. What do you think @siebenschlaefer ? It is possible a bigger rewrite and I think you should get more than the standard 12 reputation for that, if you want to continue. |
Hej @rajatnai49, I'm a little bit torn. The example solution still contains a lot of "human reasoning", e.g. why the first house is yellow is deduced from facts 6, 15, and 2 and then hard-coded ( For example, you could use five nested loops (for the nationality, the color, ...), try all permutations, and skip a particular permutation as soon as possible when it doesn't match the facts. Or you could try to implement a completely different approach, that's up to you. And two small things:
|
Sorry @siebenschlaefer for disappointment😞, and I know that all permutation solution but I think it will be good if we just first remove that and after that try every possible cases for perticular conditions, and it is much efficient than trying all permutation |
No worries, please! As vaeng wrote earlier this is not an easy exercise. And maybe we maintainer should have communicated more clearly what kind of example solutions we're looking for. And if you need any help, let us know. |
@rajatnai49 Are you still working on this? Is there anything we can do to help you? |
Hey, @siebenschlaefer sorry for delay but we are organizing the Hackathon in the university so I didn't have much time to work on the issue so I will give you update in next week for sure!! |
@siebenschlaefer I added the solution as you explained earlier, review that and if any other requirements are there then let me know ! |
Much better, this is now an algorithmic solution. But it takes a really long time to run because it tries all potential solutions and finds the correct solution after more than 171 million permutations. On my computer it runs in a a little bit more than one minute. Students wouldn't be able to submit this example solution because it would not complete the tests within the allowed time (I think the test runner has a limit of 20 or 30 seconds.) There are five nested loops (for the Instead you could assign assign the values to the houses as soon as possible, i.e. the Instead of checking the complete solution in the innermost loop you could check each of the 14 clauses as soon as possible and And some smaller nitpicks: Four of the five As far as I can see the And finally a more general question where @vaeng might also want to weigh in: Currently the tests require two separate and free functions that answer the two questions. But is that really the best structure for this problem? It basically forces the students to write some helper function that solves the problem and gets called by the two "public" functions, and it will force the test runner to solve the puzzle twice (unless the students implement some caching) without a clear benefit. How about a single function that returns both answers at the same time? That's how the Go track does it. |
We could also go the class way and generate the puzzle solution on construction and have the answers in variables ... Might be ugly :D |
Right, that's another option. (although personally I'm not a fan) |
@siebenschlaefer totally understood |
a single function that returns both answers at the same time. |
problem has been solved, new review needed with all the changes.
@vaeng @siebenschlaefer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good!
There are a few smaller things that I'd like to improve before merging.
The only potential problem that I can see is that the tests use SECTION
which does not map directly to the two tests in the problem specification. The test name "drinks water" and UUID does not reflect that combined nature of the tests. I'd rather have two separate and independent tests.
@vaeng what do you think?
config.json
Outdated
"uuid": "2badf89b-efc4-46f1-af8d-b7d2440e61c0", | ||
"practices": [], | ||
"prerequisites": [], | ||
"difficulty": 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would give this a higher difficulty, maybe 7 or 8.
@vaeng What do you think?
@@ -0,0 +1,244 @@ | |||
#include <algorithm> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please #include the associated .h
file in your .cpp
file, in this case #include "zebra_puzzle.h"
.
That helps keeping these two files in sync, and you don't need to re-define any types that are already defined in the .h
file.
{ | ||
string drinksWater; | ||
string ownsZebra; | ||
} Solution; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you '#includethe
.h` file you won't need to repeat this definition (see above).
{ | ||
std::string drinksWater; | ||
std::string ownsZebra; | ||
} Solution; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typedef struct { ... } type_name;
is a C idiom.
In C++ you can write struct type_name { ... };
, just like you did with House
in the .cpp
file.
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[16efb4e4-8ad7-4d5e-ba96-e5537b66fd42] | ||
description = "solve the zebra puzzle and answer who drinks water and who owns zebra" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should describe which tests from the language-independent problem specification are implemented in this track, the UUID and description should match those from the JSON file.
The zebra_puzze_tests.cpp
implements both tests ("resident who drinks water" and "resident who owns zebra") so this TOML file should reflect that.
|
||
#include <string> | ||
|
||
using namespace std; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While even some C++ programmers like to have this using namespace std;
in their .cpp
files most C++ experts recommend not to do that. I'd suggest removing that line, we don't want to promote bad practices.
@@ -0,0 +1,21 @@ | |||
#include "zebra_puzzle.h" | |||
|
|||
#include <string> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessary, <string>
is already included in the .h
file.
{ | ||
std::string drinksWater; | ||
std::string ownsZebra; | ||
} Solution; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*If* you want to define that type for our students, do that in the .h
file where it is required.
(and without the typedef
idiom, see above.)
Solution solve() | ||
{ | ||
// IMPLEMENT YOUR LOGIC HERE | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to provide a function stub for the students as a hint which function they must implement, and what it returns, declare it in the .h
file, like in the other exercises that do this.
@rajatnai49 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Thank you very much!
Clang is not happy with the formatting |
This is it. Thaks a lot for all the work in this exercise @siebenschlaefer and especially @rajatnai49! |
issue number: #779