Skip to content

Implement alphametics exercise - #1188

Merged
FridaTveit merged 3 commits into
exercism:masterfrom
Zhiyuan-Amos:alphametics
Jan 6, 2018
Merged

FridaTveit merged 3 commits into
exercism:masterfrom
Zhiyuan-Amos:alphametics

Conversation

@Zhiyuan-Amos

@Zhiyuan-Amos Zhiyuan-Amos commented Jan 3, 2018 •

Copy link
Copy Markdown
Contributor

Hello, I've implemented the alphametics exercise. I hope I didn't miss out anything else! ><

Reviewer Resources:

Track Policies

@sjwarner-bp sjwarner-bp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

@sjwarner-bp

Copy link
Copy Markdown
Contributor

You can also run tests locally before pushing back to the PR if you want to save yourself a little time 🙂

@Zhiyuan-Amos

Copy link
Copy Markdown
Contributor Author

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 hints.md and squashed the commits.

@sjwarner-bp sjwarner-bp left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks alright to me! I'll wait until @FridaTveit looks over it too before merging 🙂

@sjwarner-bp

Copy link
Copy Markdown
Contributor

Actually, on a closer look can we add an unlocked_by? As we get closer to nextercism, it will be good to suggest an exercise to unlock this one.

What do you think this exercise could be unlocked by?

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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("==");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation seems to have gone a bit wrong here :) Also could the two inner classes be private?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

@Zhiyuan-Amos Zhiyuan-Amos Jan 4, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see! ^^ Currently I see 3 options:

  1. Throwing a checked Exception e.g. BankAccountActionInvalidException.
  2. Throwing an unchecked Exception e.g. IllegalArgumentException.
  3. 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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@FridaTveit FridaTveit Jan 4, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. As @sjwarner-bp said, it would be more consistent with other exercises.
  2. 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 :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) :)

Comment thread config.json Outdated
"logic",
"conditionals"
],
"unlocked_by": null,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @sjwarner-bp, I didn't see that you'd already put a comment about this! :P

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about it, your feedback is a bit more detailed 😉

@Zhiyuan-Amos

Zhiyuan-Amos commented Jan 4, 2018 •

Copy link
Copy Markdown
Contributor Author

Thanks for your thorough reviews! :) I've made the changes requested except for the one on exceptions.

@FridaTveit

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

On a side note, I noticed that the issues I've raised in problem-specifications are not addressed by the developers there :(

issue 1, issue 2.

Is there anything that I can do about it? :)

@sjwarner-bp

Copy link
Copy Markdown
Contributor

@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 🙂

@FridaTveit FridaTveit left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🙂

@FridaTveit
FridaTveit merged commit 2eddc44 into exercism:master Jan 6, 2018
@Zhiyuan-Amos

Copy link
Copy Markdown
Contributor Author

Thank you for both of your reviews :)

@Zhiyuan-Amos
Zhiyuan-Amos deleted the alphametics branch January 17, 2018 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants