Add anagram generator#464
Conversation
| detector = Anagram.new('diaper') | ||
| assert_equal [], detector.match(%w(hello world zombies pants)) | ||
| # skip | ||
| assert_equal [], Anagram.new("diaper").match(["hello", "world", "zombies", "pants"]) |
There was a problem hiding this comment.
indentation should be 2 spaces, you've got 3 here.
| end | ||
|
|
||
| def work_load | ||
| actual = "Anagram.new(\"#{subject}\").match(#{candidates})" |
There was a problem hiding this comment.
If you use single quotes around #{subject} you don't need to escape the quotes.
| end | ||
|
|
||
| def show_comment | ||
| comment.nil? ? '' : "# #{comment}\n\t\t" |
There was a problem hiding this comment.
\t is the wrong character to be using.
Ideally there would be no formatting characters in this file.
Perhaps you can find a solution that leaves the formatting in the template file and the logic in this file?
There was a problem hiding this comment.
I was enforcing this because I didn't want a blank line in the generated file if there are no comments for the test case. But it makes sense that no formatting should be done here.
There was a problem hiding this comment.
There are other ways to achieve the same effect.
| def test_detects_unicode_anagrams | ||
| skip | ||
| # These words don't make sense, they're just greek letters cobbled together. | ||
| assert_equal ["ΒΓΑ", "γβα"], Anagram.new("ΑΒΓ").match(["ΒΓΑ", "ΒΓΔ", "γβα"]) |
There was a problem hiding this comment.
Use spaces rather than tabs for indentation.
| assert_equal [], detector.match(['eagle']) | ||
| def test_detects_multiple_anagrams | ||
| skip | ||
| assert_equal ["stream", "maters"], Anagram.new("master").match(["stream", "pigeon", "maters"]) |
There was a problem hiding this comment.
You're enforcing an ordering here which is not specified in the readme.
['matters','stream'] would also be a valid result.
| assert_equal ['tan'], anagrams | ||
| def test_detects_simple_anagram | ||
| skip | ||
| assert_equal ["tan"], Anagram.new("ant").match(["tan", "stand", "at"]) |
There was a problem hiding this comment.
Why have you changed from the old 3 line "detector, anagrams, assertion" format?
| assert_equal [], detector.match(['last']) | ||
| def test_detects_anagram | ||
| skip | ||
| assert_equal ["inlets"], Anagram.new("listen").match(["enlists", "google", "inlets", "banana"]) |
There was a problem hiding this comment.
This is a personal preference of mine and not something that needs changing if you have a different opinion, but I'd rather see single quotes around strings that don't require interpolation.
There was a problem hiding this comment.
Agreed. But both the expected and matched lists come from the canonical json file.
|
Travis CI is failing because the example solution does not pass all the tests: |
|
Thanks @fredrb ❤️ It's great to see people contributing new generators. Great work. |
I see you've raised an issue about this in x-common. |
|
Thanks for the review @Insti. I fixed most of the things you pointed out and left a comment on others. I'll check the issue with the greek letters in x-common then, if it gets fixed, I'll re-generate the files here. |
| assert_equal ["gallery", "regally", "largely"], Anagram.new("allergy").match(["gallery", "ballerina", "regally", "clergy", "largely", "leading"]) | ||
| skip | ||
|
|
||
| assert_equal ["gallery", "regally", "largely"].sort, Anagram.new('allergy').match(["gallery", "ballerina", "regally", "clergy", "largely", "leading"]).sort |
There was a problem hiding this comment.
You didn't address my other question:
Why have you changed from the old 3 line "detector, anagrams, assertion" format?
That would help avoid situations like this where you end up with one really long line.
|
|
||
| def show_comment | ||
| comment.nil? ? '' : "# #{comment}\n\t\t" | ||
| comment.nil? ? '' : "# #{comment}" |
There was a problem hiding this comment.
Try moving the comment outputing into the work_load, this should allow you to avoid empty lines.
| def work_load | ||
| actual = "Anagram.new('#{subject}').match(#{candidates})" | ||
| "assert_equal #{expected}.sort, #{actual}.sort" | ||
| end |
There was a problem hiding this comment.
A suggestion:
def work_load
expectation = "expected = #{expected}"
actual = "actual = Anagram.new('#{subject}').match(#{candidates})"
assertion = "assert_equal expected.sort, actual.sort"
indent_lines(
[
show_comment,
expectation,
actual,
assertion
].compact )
end
def indent_lines(lines, indent_level = 2)
lines.join("\n" + ' '*2*indent_level)
endAlthough the formatting of the indent_lines method call could be improved.
|
@Insti, I implemented the changes you suggested, could you please review again? |
|
|
||
| private | ||
|
|
||
| def ident_lines(code, ident = 2) |
There was a problem hiding this comment.
"indent" is missing an 'n'
Insti
left a comment
There was a problem hiding this comment.
This is really nice.
I think the multi-line work_load makes it much clearer what is going on.
I really like what you've done in anagram_cases using small methods and simplifying the whole thing.
Great work.
|
exercism/problem-specifications#414 has now been merged. |
| anagrams = detector.match(%w(tan stand at)) | ||
| assert_equal ['tan'], anagrams | ||
| anagrams = detector.match(["tan", "stand", "at"]) | ||
| assert_equal ["tan"].sort, anagrams.sort |
There was a problem hiding this comment.
(This is an 'in an ideal world' type issue please don't think that you have to do this.)
Based on the principle that we want to make the test cases as nice as possible for people and would prefer generator complexity over test case complexity.
It could be nice to omit the .sort when there is only one element, and pre-sort the 'expected' arrays that have multiple elements.
| end | ||
|
|
||
| def assert | ||
| "assert_equal #{expected}.sort, anagrams.sort" |
There was a problem hiding this comment.
"ideal world" suggestion: (see my other comment)
assert_equal #{expected.sort}, anagrams#{expected.size > 1 ? '.sort' : ''}
or something more elegant.
|
|
||
| # Test data version: | ||
| # <%= sha1 %> | ||
| class AnagramTest < Minitest::Test<% test_cases.each do |test_case| %> |
There was a problem hiding this comment.
Can we move the <% .. %> down to the next line?
|
Good idea omiting the |
|
I think this is ready to go now, I'll just wait a bit and give @kotp a chance to look over it before merging. Thanks for your work on this and your responsiveness to feedback, I think we've ended up with a really nice generator AND nice test cases. Great work. ❤️ |
|
@Insti It was a pleasure! Thanks for the code reviews and suggestions |
|
@fredrb can you squash these commits into one or (if you think it makes sense) 2 commits. It makes it easier to review, and gives structure to the commit message when it comes in, as to what you would like communicate in the log. I end up doing it locally, to review, but it is nice to give you a chance to cultivate the commit, as I may miss something. |
4604fe3 to
af734a7
Compare
|
Squashed them into one, @kotp. Also made the small fix for the trailing whitespace. |
|
Thank you @fredrb |
#396 - Anagram exercise generator