-
-
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
Changes from 3 commits
f625dfe
7fefb66
4245289
c2ab88d
9268b04
33d781f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2695,6 +2695,29 @@ | |
"integers" | ||
], | ||
"difficulty": 3 | ||
}, | ||
{ | ||
"slug": "two-bucket", | ||
"name": "Two Bucket", | ||
"uuid": "07b4679a-3d9f-42bc-ad1e-b9ba272a1c15", | ||
"prerequisites": [ | ||
"atoms", | ||
"tuples", | ||
"integers", | ||
"lists", | ||
"pattern-matching", | ||
"if", | ||
"cond", | ||
"match", | ||
"structs", | ||
"enum", | ||
"recursion" | ||
], | ||
"practices": [ | ||
"atoms", | ||
"integers" | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Oh I remember now, copy paste :( |
||
"difficulty": 7 | ||
} | ||
], | ||
"foregone": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Description | ||
|
||
Given two buckets of different size, demonstrate how to measure an exact number of liters by strategically transferring liters of fluid between the buckets. | ||
|
||
Since this mathematical problem is fairly subject to interpretation / individual approach, the tests have been written specifically to expect one overarching solution. | ||
|
||
To help, the tests provide you with which bucket to fill first. That means, when starting with the larger bucket full, you are NOT allowed at any point to have the smaller bucket full and the larger bucket empty (aka, the opposite starting point); that would defeat the purpose of comparing both approaches! | ||
|
||
Your program will take as input: | ||
- the size of bucket one | ||
- the size of bucket two | ||
- the desired number of liters to reach | ||
- which bucket to fill first, either bucket one or bucket two | ||
|
||
Your program should determine: | ||
- the total number of "moves" it should take to reach the desired number of liters, including the first fill | ||
- which bucket should end up with the desired number of liters (let's say this is bucket A) - either bucket one or bucket two | ||
- how many liters are left in the other bucket (bucket B) | ||
|
||
Note: any time a change is made to either or both buckets counts as one (1) move. | ||
|
||
Example: | ||
Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters. Let's say bucket one, at a given step, is holding 7 liters, and bucket two is holding 8 liters (7,8). If you empty bucket one and make no change to bucket two, leaving you with 0 liters and 8 liters respectively (0,8), that counts as one "move". Instead, if you had poured from bucket one into bucket two until bucket two was full, leaving you with 4 liters in bucket one and 11 liters in bucket two (4,11), that would count as only one "move" as well. | ||
|
||
To conclude, the only valid moves are: | ||
- pouring from either bucket to another | ||
- emptying either bucket and doing nothing to the other | ||
- filling either bucket and doing nothing to the other | ||
|
||
Written with <3 at [Fullstack Academy](http://www.fullstackacademy.com/) by Lindsay Levine. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"authors": ["jiegillet"], | ||
"contributors": [], | ||
"files": { | ||
"example": [ | ||
".meta/example.ex" | ||
], | ||
"solution": [ | ||
"lib/two_bucket.ex" | ||
], | ||
"test": [ | ||
"test/two_bucket_test.exs" | ||
] | ||
}, | ||
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.", | ||
"source": "Water Pouring Problem", | ||
"source_url": "http://demonstrations.wolfram.com/WaterPouringProblem/" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Agreed! It reads much better now |
||
|
||
@doc """ | ||
Find the quickest way to fill a bucket with some amount of water from two buckets of specific sizes. | ||
""" | ||
@spec measure( | ||
size_one :: integer, | ||
size_two :: integer, | ||
goal :: integer, | ||
start_bucket :: :one | :two | ||
) :: {:ok, TwoBucket.t()} | {:error, :impossible} | ||
def measure(size_one, size_two, goal, start_bucket) do | ||
other_filled = if start_bucket == :one, do: {0, size_two}, else: {size_one, 0} | ||
forbidden_states = MapSet.new([other_filled]) | ||
|
||
# Partially apply function to avoid passing many arguments | ||
next_moves = &next_moves(&1, size_one, size_two) | ||
|
||
pour([{{0, 0}, 0}], forbidden_states, next_moves, goal) | ||
end | ||
|
||
def pour([], _, _, _), do: {:error, :impossible} | ||
|
||
def pour([{{goal, other}, moves} | _], _, _, goal), | ||
do: {:ok, %TwoBucket{goal: :one, moves: moves, other_bucket: other}} | ||
|
||
def pour([{{other, goal}, moves} | _], _, _, goal), | ||
do: {:ok, %TwoBucket{goal: :two, moves: moves, other_bucket: other}} | ||
|
||
def pour([{state, moves} | states], forbidden_states, next_moves, goal) do | ||
next = | ||
next_moves.(state) | ||
|> 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 commentThe 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 commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All good. My comment about two lists and 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 😁 |
||
end | ||
|
||
def next_moves({fill_one, fill_two}, size_one, size_two) do | ||
[ | ||
# Empty a bucket | ||
{0, fill_two}, | ||
{fill_one, 0}, | ||
# Fill a bucket | ||
{fill_one, size_two}, | ||
{size_one, fill_two}, | ||
# 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
It's a similar process for the other bucket, but I use min to prevent over-fullness. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
] | ||
|> Enum.uniq() | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661] | ||
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one" | ||
|
||
[6c4ea451-9678-4926-b9b3-68364e066d40] | ||
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two" | ||
|
||
[3389f45e-6a56-46d5-9607-75aa930502ff] | ||
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one" | ||
|
||
[fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1] | ||
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two" | ||
|
||
[0ee1f57e-da84-44f7-ac91-38b878691602] | ||
description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two" | ||
|
||
[eb329c63-5540-4735-b30b-97f7f4df0f84] | ||
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" | ||
|
||
[449be72d-b10a-4f4b-a959-ca741e333b72] | ||
description = "Not possible to reach the goal" | ||
|
||
[aac38b7a-77f4-4d62-9b91-8846d533b054] | ||
description = "With the same buckets but a different goal, then it is possible" | ||
|
||
[74633132-0ccf-49de-8450-af4ab2e3b299] | ||
description = "Goal larger than both buckets is impossible" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule TwoBucket do | ||
defstruct [:goal, :moves, :other_bucket] | ||
@type t :: %TwoBucket{goal: :one | :two, moves: integer, other_bucket: integer} | ||
|
||
@doc """ | ||
Find the quickest way to fill a bucket with some amount of water from two buckets of specific sizes. | ||
""" | ||
@spec measure( | ||
size_one :: integer, | ||
size_two :: integer, | ||
goal :: integer, | ||
start_bucket :: :one | :two | ||
) :: {:ok, TwoBucket.t()} | {:error, :impossible} | ||
def measure(size_one, size_two, goal, start_bucket) do | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule TwoBucket.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :two_bucket, | ||
version: "0.1.0", | ||
# elixir: "~> 1.8", | ||
start_permanent: Mix.env() == :prod, | ||
deps: deps() | ||
] | ||
end | ||
|
||
# Run "mix help compile.app" to learn about applications. | ||
def application do | ||
[ | ||
extra_applications: [:logger] | ||
] | ||
end | ||
|
||
# Run "mix help deps" to learn about dependencies. | ||
defp deps do | ||
[ | ||
# {:dep_from_hexpm, "~> 0.3.0"}, | ||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | ||
] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ExUnit.start() | ||
ExUnit.configure(exclude: :pending, trace: true) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
defmodule TwoBucketTest do | ||
use ExUnit.Case | ||
|
||
# @tag :pending | ||
test "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one" do | ||
bucket_one = 3 | ||
bucket_two = 5 | ||
goal = 1 | ||
start_bucket = :one | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :one, moves: 4, other_bucket: 5}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two" do | ||
bucket_one = 3 | ||
bucket_two = 5 | ||
goal = 1 | ||
start_bucket = :two | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :two, moves: 8, other_bucket: 3}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one" do | ||
bucket_one = 7 | ||
bucket_two = 11 | ||
goal = 2 | ||
start_bucket = :one | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :one, moves: 14, other_bucket: 11}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two" do | ||
bucket_one = 7 | ||
bucket_two = 11 | ||
goal = 2 | ||
start_bucket = :two | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :two, moves: 18, other_bucket: 7}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two" do | ||
bucket_one = 1 | ||
bucket_two = 3 | ||
goal = 3 | ||
start_bucket = :two | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :two, moves: 1, other_bucket: 0}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" do | ||
bucket_one = 2 | ||
bucket_two = 3 | ||
goal = 3 | ||
start_bucket = :one | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :two, moves: 2, other_bucket: 2}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Not possible to reach the goal" do | ||
bucket_one = 6 | ||
bucket_two = 15 | ||
goal = 5 | ||
start_bucket = :one | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:error, :impossible} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "With the same buckets but a different goal, then it is possible" do | ||
bucket_one = 6 | ||
bucket_two = 15 | ||
goal = 9 | ||
start_bucket = :one | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:ok, %TwoBucket{goal: :two, moves: 10, other_bucket: 0}} | ||
|
||
assert output == expected | ||
end | ||
|
||
@tag :pending | ||
test "Goal larger than both buckets is impossible" do | ||
bucket_one = 5 | ||
bucket_two = 7 | ||
goal = 8 | ||
start_bucket = :one | ||
output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) | ||
expected = {:error, :impossible} | ||
|
||
assert output == expected | ||
end | ||
end |
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 -_-