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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,26 @@
"integers"
],
"difficulty": 3
},
{
"slug": "two-bucket",
"name": "Two Bucket",
"uuid": "07b4679a-3d9f-42bc-ad1e-b9ba272a1c15",
"prerequisites": [
"atoms",
"tuples",
"integers",
"lists",
"pattern-matching",
"if",
"cond",
"case",
"structs",
"enum",
"recursion"
],
"practices": [],
"difficulty": 7
}
],
"foregone": [
Expand Down
30 changes: 30 additions & 0 deletions exercises/practice/two-bucket/.docs/instructions.md
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.
4 changes: 4 additions & 0 deletions exercises/practice/two-bucket/.formatter.exs
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}"]
]
21 changes: 21 additions & 0 deletions exercises/practice/two-bucket/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": ["jiegillet"],
"contributors": [
"angelikatyborska",
"neenjaw"
],
"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/"
}
60 changes: 60 additions & 0 deletions exercises/practice/two-bucket/.meta/example.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule TwoBucket do
defstruct [:bucket_one, :bucket_two, :moves]
@type t :: %TwoBucket{bucket_one: integer, bucket_two: integer, moves: 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
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([{{a, b}, moves} | _], _, _, goal) when a == goal or b == goal,
do: {:ok, %TwoBucket{bucket_one: a, bucket_two: b, moves: moves}}

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

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
if fill_one < size_two - fill_two do
{0, fill_one + fill_two}
else
{fill_one - (size_two - fill_two), size_two}
end,
if fill_two < size_one - fill_one do
{fill_one + fill_two, 0}
else
{size_one, fill_two - (size_one - fill_one)}
end
]
|> Enum.uniq()
end
end
36 changes: 36 additions & 0 deletions exercises/practice/two-bucket/.meta/tests.toml
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"
16 changes: 16 additions & 0 deletions exercises/practice/two-bucket/lib/two_bucket.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule TwoBucket do
defstruct [:bucket_one, :bucket_two, :moves]
@type t :: %TwoBucket{bucket_one: integer, bucket_two: integer, moves: 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
28 changes: 28 additions & 0 deletions exercises/practice/two-bucket/mix.exs
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
2 changes: 2 additions & 0 deletions exercises/practice/two-bucket/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
111 changes: 111 additions & 0 deletions exercises/practice/two-bucket/test/two_bucket_test.exs
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{bucket_one: goal, bucket_two: 5, moves: 4}}

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{bucket_one: 3, bucket_two: goal, moves: 8}}

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{bucket_one: goal, bucket_two: 11, moves: 14}}

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{bucket_one: 7, bucket_two: goal, moves: 18}}

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{bucket_one: 0, bucket_two: goal, moves: 1}}

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{bucket_one: 2, bucket_two: goal, moves: 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{bucket_one: 0, bucket_two: goal, moves: 10}}

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