-
-
Notifications
You must be signed in to change notification settings - Fork 398
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
Conversation
Thank you for contributing to Based on the files changed in this PR, it would be good to pay attention to the following details when reviewing the PR:
Automated comment created by PR Commenter 🤖. |
config.json
Outdated
"pattern-matching", | ||
"if", | ||
"cond", | ||
"match", |
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 isn't a concept 🤔 what did you mean by "match"?
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 mean case
, I was pretty tired when I wrote that -_-
config.json
Outdated
"practices": [ | ||
"atoms", | ||
"integers" | ||
], |
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.
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
?
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 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.
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.
Oh I remember now, copy paste :(
Sorry about that!
# 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)} |
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.
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.
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.
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:
- There is enough room in the second bucket for all the water => result is 0 in the first bucket
- There is not enough room in the second bucket => result is current fill
fill_one
minus the remaining room in the second bucketsize_two - fill two
.
In general, I can calculatefill_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. Hencemax(0, fill_one - (size_two - fill_two))
. If the same asif 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?
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.
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) |
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 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?
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.
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 ).
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.
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 😁
@@ -0,0 +1,55 @@ | |||
defmodule TwoBucket do | |||
defstruct [:goal, :moves, :other_bucket] | |||
@type t :: %TwoBucket{goal: :one | :two, moves: integer, other_bucket: integer} |
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.
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}
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.
It's the interface suggested in the tests. But you are right it's not super intuitive, I'll change it. Thank you!
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.
Cool, I think things like this are good reasons to break continuity with the problem specs repo
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.
Agreed! It reads much better now
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.
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? 🥳
No worries, I'm a problem solver, in this case quite literally :) |
One more.