Skip to content
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

[#792] New practice exercise two-bucket #808

Merged
merged 6 commits into from
Jul 3, 2021

Conversation

jiegillet
Copy link
Contributor

One more.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2021

Thank you for contributing to exercism/elixir 💜 🎉. This is an automated PR comment 🤖 for the maintainers of this repository that helps with the PR review process. You can safely ignore it and wait for a maintainer to review your changes.

Based on the files changed in this PR, it would be good to pay attention to the following details when reviewing the PR:

  • General steps

    • 🏆 Does this PR need to receive a label with a reputation modifier (x:size/{tiny,small,medium,large,massive})? (A medium reputation amount is awarded by default, see docs)
  • Any exercise changed

    • 👤 Does the author of the PR need to be added as an author or contributor in <exercise>/.meta/config.json (see docs)?
    • 🔬 Do the analyzer and the analyzer comments exist for this exercise? Do they need to be changed?
    • 📜 Does the design file (<exercise>/.meta/design.md) need to be updated to document new implementation decisions?
  • Practice exercise changed

    • 🌲 Do prerequisites, practices, and difficulty in config.json need to be updated?
    • 🧑‍🏫 Are the changes in accordance with the community-wide problem specifiations?
  • Practice exercise tests changed

    • ⚪️ Are all tests except the first one skipped?
    • 📜 Does <exercise>/.meta/tests.toml need updating?

Automated comment created by PR Commenter 🤖.

config.json Outdated
"pattern-matching",
"if",
"cond",
"match",
Copy link
Member

Choose a reason for hiding this comment

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

This isn't a concept 🤔 what did you mean by "match"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I mean case, I was pretty tired when I wrote that -_-

config.json Outdated
Comment on lines 2716 to 2719
"practices": [
"atoms",
"integers"
],
Copy link
Member

Choose a reason for hiding this comment

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

Those are some pretty basic concepts for a rather complex exercise 🤔 and the check is failing for integers... maybe we could leave it completely empty? Or we say this is specifically a tail call recursion exercise and it practices and requires tail-call-recursion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not really a tail call recursion, it's more of a breadth first search algorithm. I don't remember why I wrote these two. I think it's best to leave empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I remember now, copy paste :(
Sorry about that!

Comment on lines 49 to 51
# Pour one into the other
{min(size_one, fill_one + fill_two), max(0, fill_two - size_one + fill_one)},
{max(0, fill_one - size_two + fill_two), min(size_two, fill_one + fill_two)}
Copy link
Member

Choose a reason for hiding this comment

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

Normally I don't comment on the example solution, but I'm actually struggling to understand this exercise. Could you explain to me why min and max are necessary? I would think that when you pour from one bucket into the other until the first one is full, there is only one possible outcome.

Copy link
Contributor Author

@jiegillet jiegillet Jul 3, 2021

Choose a reason for hiding this comment

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

No worries, I'm happy to explain. There is only one possible outcome indeed, but you have to be careful how to calculate it and I'm using min/max as a shortcut not to use an if statement. For example, imagine we want to pour the first bucket in the other one (line 51). There are 2 cases:

  1. There is enough room in the second bucket for all the water => result is 0 in the first bucket
  2. There is not enough room in the second bucket => result is current fill fill_one minus the remaining room in the second bucket size_two - fill two.
    In general, I can calculate fill_one - (size_two - fill_two) and if it's negative, it means that there was extra room in the second bucket but since columns can't be negative, I need to cap that to 0. Hence max(0, fill_one - (size_two - fill_two)). If the same as if fill_one < size_two - fill_two, do: 0, else: fill_one - (size_two - fill_two) .

It's a similar process for the other bucket, but I use min to prevent over-fullness.
Does that make sense? Should I use an if statement instead?

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense now, thanks! No, there's no need to change it to an if :)

|> Enum.reject(&MapSet.member?(forbidden_states, &1))
|> Enum.map(&{&1, moves + 1})

pour(states ++ next, MapSet.put(forbidden_states, state), next_moves, goal)
Copy link
Member

Choose a reason for hiding this comment

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

This is just a generic worry, I haven't analyzed the solution in detail, so it might be misguided, but:

Appending to a list in a loop? Are you sure this is the best way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, because this is a breadth first search. I need to look at states with a lower number of moves first, and the next states I just created last. Otherwise I would need to generate all paths and check for the lowest after.
A queue is the best data structure for this, but it's not worth implementing in this solution I think (and also the standard FP queue is made out of two lists and use that infamous Enum.reverse that you don't like :p ).

Copy link
Member

Choose a reason for hiding this comment

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

All good. My comment about two lists and Enum.reverse was purely in the context of double linked lists 👻.

If you need a queue, use Erlang's (http://erlang.org/doc/man/queue.html). It has functions for adding and removing from both ends. And yes, it's two lists 😁

@angelikatyborska angelikatyborska added the x:size/large Large amount of work label Jul 2, 2021
@@ -0,0 +1,55 @@
defmodule TwoBucket do
defstruct [:goal, :moves, :other_bucket]
@type t :: %TwoBucket{goal: :one | :two, moves: integer, other_bucket: integer}
Copy link
Contributor

@neenjaw neenjaw Jul 2, 2021

Choose a reason for hiding this comment

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

Is this the clearest interface?

Would it not be clearer for a student studying the problem to return their answer using a struct %TwoBucket{bucket_one: x, bucket_two: y, moves: z}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the interface suggested in the tests. But you are right it's not super intuitive, I'll change it. Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, I think things like this are good reasons to break continuity with the problem specs repo

Copy link
Member

Choose a reason for hiding this comment

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

Agreed! It reads much better now

Copy link
Member

@angelikatyborska angelikatyborska left a comment

Choose a reason for hiding this comment

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

Did I mention how happy I am that you're adding all of those complex exercises so I don't have to solve them myself? 🥳

@angelikatyborska angelikatyborska merged commit 0c41cbd into exercism:main Jul 3, 2021
@jiegillet
Copy link
Contributor Author

No worries, I'm a problem solver, in this case quite literally :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
x:size/large Large amount of work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants