Repository navigation
Implement alphametics exercise - #1188
Conversation
sjwarner-bp
left a comment
There was a problem hiding this comment.
Looks as though you have given this exercise a difficulty of '6', but have added a hints.md explaining why it does not have a starter implementation.
As you can see in POLICIES, this is only required for difficulty '5' exercises 🙂
|
You can also run tests locally before pushing back to the PR if you want to save yourself a little time 🙂 |
Thanks for the tip! :) Also, I've removed |
There was a problem hiding this comment.
Looks alright to me! I'll wait until @FridaTveit looks over it too before merging 🙂
|
Actually, on a closer look can we add an What do you think this exercise could be unlocked by? |
FridaTveit
left a comment
There was a problem hiding this comment.
This looks great @Zhiyuan-Amos! :)
I've put some comments but they're pretty minor things. Feel free to ask questions or disagree with me :)
| private final String wordResult; | ||
|
|
||
| Alphametics(String userInput) { | ||
| String[] array = userInput.split("=="); |
There was a problem hiding this comment.
array isn't a very descriptive name. Maybe naming it something like questionAndAnswer or similar will make it more clear what it is? :)
| } | ||
| } | ||
|
|
||
| class StringUtil { |
There was a problem hiding this comment.
Indentation seems to have gone a bit wrong here :) Also could the two inner classes be private?
There was a problem hiding this comment.
This isn't an inner class :P I've shifted the method inside StringUtil to simply be a private helper method in Alphametics instead.
| @Ignore("Remove to run test") | ||
| @Test | ||
| public void testUniqueValue() { | ||
| assertNull(new Alphametics("A == B").solve()); |
There was a problem hiding this comment.
In cases like this I would expect it to throw an exception instead of returning null. For error handling it's okay to diverge a bit from the canonical data (I believe they're trying to remove most error cases from the canonical data for this reason) since it can vary a lot from language to language how you'd want to handle invalid input. In my opinion it's better to throw an exception in Java than to return null since that forces to user to deal with the error instead of relying on them checking if the output is null :)
There was a problem hiding this comment.
For an example where we've used custom exceptions in cases like this see the bank account tests. You could also use IllegalArgumentException if you would prefer not to use a custom one :)
There was a problem hiding this comment.
Ah I see! ^^ Currently I see 3 options:
- Throwing a checked Exception e.g.
BankAccountActionInvalidException. - Throwing an unchecked Exception e.g.
IllegalArgumentException. - Returning an
Optional<Map<Character, Integer>>.
I don't quite like option 2 because the user isn't supposed to catch an unchecked Exception according to the javadoc.
Comparing between option 1 & 3, I prefer option 3 because not having a result doesn't seem to be that exceptional. Or perhaps, I guess Optional does the job well enough, so we don't have to write an additional Exception class.
/**
* Returns the solution to the alphametics puzzle if it exists.
*/
Optional<Map<Character, Integer>> solve() {
AlphameticsRecursion solver = new AlphameticsRecursion(getDistinctCharacters());
solver.generate();
return solver.get();
}Is this ok?
There was a problem hiding this comment.
I'm not sure that would be the best solution. It's an error case, the user (in this case the tests) has provided invalid input, so I would expect it to throw an exception. It would also be more consistent with the other exercises and won't require the person implementing the exercise to learn about optionals.
We aren't expecting the anything to catch the exception, so I think it's fine to use an unchecked exception. We only want the tests to confirm that an exception has been thrown (by using ExpectedException). I normally prefer using a custom exception because I think that's neater and makes it more clear what's going on since you can give it a relevant name :) In that case we would provide the implementation of the exception as starter implementation. Does that make sense?
Those are just my preferences, feel free to disagree :)
There was a problem hiding this comment.
I'd agree - in most of the existing test suites that require this kind of thing, it isn't uncommon for us to use ExpectedException.
For consistency (and ease of the users) I probably prefer option 2? But I option 1 sounds good to me too!
There was a problem hiding this comment.
Having thought about it a bit more, I do see your point @Zhiyuan-Amos that this isn't really a very exceptional case. However, I would still argue for using an exception because:
- As @sjwarner-bp said, it would be more consistent with other exercises.
- Exceptions are very well implemented in Java in my opinion, they're one of my favourite parts of the language actually :) Whereas optionals, while very useful in other languages, are a bit of a pain in Java. If we changed the solve method to return an optional we would have to change all the other tests to check whether it's present, or live with the compiler warnings telling us we haven't checked whether it's present.
However, since it's not really a case of an illegal argument, and it's something we would want the user to be able to recover from, you're probably right that it should be a checked exception. Let me know what you think @Zhiyuan-Amos and @sjwarner-bp :)
There was a problem hiding this comment.
Yes, I agree that using an Exception instead is more consistent with other exercises. ^^ I've updated it accordingly. Thanks :)
| expected.put('S', 4); | ||
| expected.put('T', 9); | ||
|
|
||
| assertEquals(expected, new Alphametics("THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + ROOFS + FOR + THEIR + SAFE == FORTRESSES").solve()); |
There was a problem hiding this comment.
It would be great if you could put some line breaks in this line so it isn't too long (less than 120 characters is a common limit) :)
| "logic", | ||
| "conditionals" | ||
| ], | ||
| "unlocked_by": null, |
There was a problem hiding this comment.
If you'd like to suggest an exercise this could be unlocked by, that would be great :) It has to be a core exercise and we normally choose a core exercise which has similar topics. Maybe secret-handshake could be a good suggestion for this one? It's sort of to do with logic and unlocks some other exercises to do with logic: robot-simulator, wordy, and forth. If you don't want to fill this in now then that's fine, we can open a separate issue for it :)
In case you're not familiar with the exercism structure, users will first only have a few easy exercises plus the first core exercise available to them. Once they complete that core exercise the next core exercise will become available to them plus any exercises that the first core exercise unlocks :)
There was a problem hiding this comment.
Sorry @sjwarner-bp, I didn't see that you'd already put a comment about this! :P
There was a problem hiding this comment.
Don't worry about it, your feedback is a bit more detailed 😉
|
Thanks for your thorough reviews! :) I've made the changes requested except for the one on exceptions. |
|
Your new changes look great @Zhiyuan-Amos, thanks! Once we've agreed on an approach for error handling I think this is ready to merge :) |
|
@Zhiyuan-Amos, it could be worth posting in here to ask about that. Sometimes problem-specifications moves slower than the language tracks, so I wouldn't worry too much as it is a busy time of year 🙂 |
There was a problem hiding this comment.
This looks great! Thank you so much for all your hard work on this exercise @Zhiyuan-Amos! 🙂
Like @sjwarner-bp said, issues in problem-specifications can take a while to get resolved and naturally most people have less time to respond to issues over Christmas 🙂 So I would give it a bit longer, and if people still don't respond you could maybe put a comment on the issue asking if anyone has had time to consider it 🙂 And posting in the gitter chat room like @sjwarner-bp suggested is also a good idea 🙂
|
Thank you for both of your reviews :) |
Hello, I've implemented the alphametics exercise. I hope I didn't miss out anything else! ><
Reviewer Resources:
Track Policies