-
-
Notifications
You must be signed in to change notification settings - Fork 402
Add practice exercise piecing-it-together
#1576
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -2675,6 +2675,25 @@ | |
], | ||
"difficulty": 7 | ||
}, | ||
{ | ||
"slug": "piecing-it-together", | ||
"name": "Piecing It Together", | ||
"uuid": "e20ce41a-ff3f-46e4-9fbb-8810883c488f", | ||
"practices": [ | ||
"structs" | ||
], | ||
"prerequisites": [ | ||
"structs", | ||
"errors", | ||
"multiple-clause-functions", | ||
"pattern-matching", | ||
"guards", | ||
"if", | ||
"case", | ||
"floating-point-numbers" | ||
Comment on lines
+2686
to
+2693
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. You have also used: enum, keyword-lists, maps, erlang-libraries. Which ones are really necessary for this exercise? I'm assuming erlang-libraries are optional because As for enum, maps, and keyword-lists, they all come from the fact that you've turned then input struct into a keyword list. Was that really necessary? Can the exercise be solved without that? |> Map.from_struct()
|> Enum.filter(fn {_, value} -> value end)
|> Enum.sort_by(fn {key, _} -> key end) |
||
], | ||
"difficulty": 7 | ||
}, | ||
{ | ||
"slug": "queen-attack", | ||
"name": "Queen Attack", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Instructions | ||
|
||
Given partial information about a jigsaw puzzle, add the missing pieces. | ||
|
||
If the information is insufficient to complete the details, or if given parts are in contradiction, the user should be notified. | ||
|
||
The full information about the jigsaw puzzle contains the following parts: | ||
|
||
- `pieces`: Total number of pieces | ||
- `border`: Number of border pieces | ||
- `inside`: Number of inside (non-border) pieces | ||
- `rows`: Number of rows | ||
- `columns`: Number of columns | ||
- `aspectRatio`: Aspect ratio of columns to rows | ||
- `format`: Puzzle format, which can be `portrait`, `square`, or `landscape` | ||
|
||
For this exercise, you may assume square pieces, so that the format can be derived from the aspect ratio: | ||
|
||
- If the aspect ratio is less than 1, it's `portrait` | ||
- If it is equal to 1, it's `square` | ||
- If it is greater than 1, it's `landscape` | ||
|
||
## Three examples | ||
|
||
### Portrait | ||
|
||
A portrait jigsaw puzzle with 6 pieces, all of which are border pieces and none are inside pieces. It has 3 rows and 2 columns. The aspect ratio is 1.5 (3/2). | ||
|
||
 | ||
|
||
### Square | ||
|
||
A square jigsaw puzzle with 9 pieces, all of which are border pieces except for the one in the center, which is an inside piece. It has 3 rows and 3 columns. The aspect ratio is 1 (3/3). | ||
|
||
 | ||
|
||
### Landscape | ||
|
||
A landscape jigsaw puzzle with 12 pieces, 10 of which are border pieces and 2 are inside pieces. It has 3 rows and 4 columns. The aspect ratio is 1.333333... (4/3). | ||
|
||
 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Introduction | ||
|
||
Your best friend has started collecting jigsaw puzzles and wants to build a detailed catalog of their collection — recording information such as the total number of pieces, the number of rows and columns, the number of border and inside pieces, aspect ratio, and puzzle format. | ||
Even with your powers combined, it takes multiple hours to solve a single jigsaw puzzle with one thousand pieces — and then you still need to count the rows and columns and calculate the remaining numbers manually. | ||
The even larger puzzles with thousands of pieces look quite daunting now. | ||
"There has to be a better way!" you exclaim and sit down in front of your computer to solve the problem. |
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,26 @@ | ||
# The directory Mix will write compiled artifacts to. | ||
/_build/ | ||
|
||
# If you run "mix test --cover", coverage assets end up here. | ||
/cover/ | ||
|
||
# The directory Mix downloads your dependencies sources to. | ||
/deps/ | ||
|
||
# Where third-party dependencies like ExDoc output generated docs. | ||
/doc/ | ||
|
||
# Ignore .fetch files in case you like to edit your project deps locally. | ||
/.fetch | ||
|
||
# If the VM crashes, it generates a dump, let's ignore it too. | ||
erl_crash.dump | ||
|
||
# Also ignore archive artifacts (built via "mix archive.build"). | ||
*.ez | ||
|
||
# Ignore package tarball (built via "mix hex.build"). | ||
piecing_it_together-*.tar | ||
|
||
# Temporary files, for example, from tests. | ||
/tmp/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"jiegillet" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/piecing_it_together.ex" | ||
], | ||
"test": [ | ||
"test/piecing_it_together_test.exs" | ||
], | ||
"example": [ | ||
".meta/example.ex" | ||
] | ||
}, | ||
"blurb": "Fill in missing jigsaw puzzle details from partial data", | ||
"source": "atk just started another 1000-pieces jigsaw puzzle when this idea hit him", | ||
"source_url": "https://github.com/exercism/problem-specifications/pull/2554" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
defmodule PiecingItTogether do | ||
@doc """ | ||
TODO: add function description and replace types in @spec | ||
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. 👀 |
||
""" | ||
|
||
defmodule JigsawPuzzle do | ||
@type format() :: :landscape | :portrait | :square | ||
@type t() :: %__MODULE__{ | ||
pieces: pos_integer(), | ||
rows: pos_integer(), | ||
columns: pos_integer(), | ||
format: format(), | ||
aspect_ratio: float(), | ||
border: pos_integer(), | ||
inside: pos_integer() | ||
} | ||
|
||
defstruct [:pieces, :rows, :columns, :format, :aspect_ratio, :border, :inside] | ||
end | ||
|
||
@spec jigsaw_data(jigsaw_puzzle :: JigsawPuzzle.t()) :: | ||
{:ok, JigsawPuzzle.t()} | {:error, String.t()} | ||
def jigsaw_data(%JigsawPuzzle{} = jigsaw_puzzle) do | ||
jigsaw_puzzle | ||
|> Map.from_struct() | ||
|> Enum.filter(fn {_, value} -> value end) | ||
|> Enum.sort_by(fn {key, _} -> key end) | ||
|> complete_jigsaw_data() | ||
end | ||
|
||
defp complete_jigsaw_data(aspect_ratio: aspect_ratio, pieces: pieces) do | ||
columns = round(:math.sqrt(aspect_ratio * pieces)) | ||
rows = Integer.floor_div(pieces, columns) | ||
border = 2 * (rows + columns) - 4 | ||
inside = pieces - border | ||
|
||
{:ok, | ||
%JigsawPuzzle{ | ||
pieces: pieces, | ||
rows: rows, | ||
columns: columns, | ||
format: aspect_ratio_to_format(aspect_ratio), | ||
aspect_ratio: aspect_ratio, | ||
border: border, | ||
inside: inside | ||
}} | ||
end | ||
|
||
defp complete_jigsaw_data(format: :square, rows: rows) do | ||
columns = rows | ||
pieces = rows * columns | ||
border = 2 * (rows + columns) - 4 | ||
inside = pieces - border | ||
|
||
{:ok, | ||
%JigsawPuzzle{ | ||
pieces: pieces, | ||
rows: rows, | ||
columns: columns, | ||
format: :square, | ||
aspect_ratio: 1.0, | ||
border: border, | ||
inside: inside | ||
}} | ||
end | ||
|
||
defp complete_jigsaw_data(format: _, rows: _) do | ||
{:error, "Insufficient data"} | ||
end | ||
|
||
defp complete_jigsaw_data(aspect_ratio: 1.0, inside: inside) do | ||
columns = 2 + round(:math.sqrt(inside)) | ||
rows = columns | ||
pieces = rows * columns | ||
border = pieces - inside | ||
|
||
{:ok, | ||
%JigsawPuzzle{ | ||
pieces: pieces, | ||
rows: rows, | ||
columns: columns, | ||
format: :square, | ||
aspect_ratio: 1.0, | ||
border: border, | ||
inside: inside | ||
}} | ||
end | ||
|
||
defp complete_jigsaw_data(aspect_ratio: _, inside: _) do | ||
{:error, "Insufficient data"} | ||
end | ||
|
||
defp complete_jigsaw_data(aspect_ratio: aspect_ratio, rows: rows) do | ||
columns = round(rows * aspect_ratio) | ||
pieces = rows * columns | ||
border = 2 * (rows + columns) - 4 | ||
inside = pieces - border | ||
|
||
{:ok, | ||
%JigsawPuzzle{ | ||
pieces: pieces, | ||
rows: rows, | ||
columns: columns, | ||
format: aspect_ratio_to_format(aspect_ratio), | ||
aspect_ratio: aspect_ratio, | ||
border: border, | ||
inside: inside | ||
}} | ||
end | ||
|
||
defp complete_jigsaw_data(border: border, format: :square, pieces: pieces) do | ||
complete_jigsaw_data(aspect_ratio: 1.0, inside: pieces - border) | ||
end | ||
|
||
defp complete_jigsaw_data(border: border, format: format, pieces: pieces) do | ||
center = 1 + border / 4 | ||
diff = :math.sqrt(center ** 2 - pieces) | ||
|
||
{rows, columns} = | ||
case format do | ||
:landscape -> {round(center - diff), round(center + diff)} | ||
:portrait -> {round(center + diff), round(center - diff)} | ||
end | ||
|
||
aspect_ratio = columns / rows | ||
inside = pieces - border | ||
|
||
{:ok, | ||
%JigsawPuzzle{ | ||
pieces: pieces, | ||
rows: rows, | ||
columns: columns, | ||
format: format, | ||
aspect_ratio: aspect_ratio, | ||
border: border, | ||
inside: inside | ||
}} | ||
end | ||
|
||
defp complete_jigsaw_data(columns: columns, format: format, rows: rows) do | ||
aspect_ratio = columns / rows | ||
|
||
if format == aspect_ratio_to_format(aspect_ratio) do | ||
pieces = rows * columns | ||
border = 2 * (rows + columns) - 4 | ||
inside = rows * columns - border | ||
|
||
{:ok, | ||
%JigsawPuzzle{ | ||
pieces: pieces, | ||
rows: rows, | ||
columns: columns, | ||
format: format, | ||
aspect_ratio: aspect_ratio, | ||
border: border, | ||
inside: inside | ||
}} | ||
else | ||
{:error, "Contradictory data"} | ||
end | ||
end | ||
|
||
defp complete_jigsaw_data(_) do | ||
{:error, "Insufficient data"} | ||
end | ||
|
||
defp aspect_ratio_to_format(aspect_ratio) do | ||
case aspect_ratio do | ||
aspect_ratio when aspect_ratio < 1 -> :portrait | ||
aspect_ratio when aspect_ratio > 1 -> :landscape | ||
_ -> :square | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 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. | ||
|
||
[ad626f23-09a2-4f5f-ba22-eec0671fa2a9] | ||
description = "1000 pieces puzzle with 1.6 aspect ratio" | ||
|
||
[3e0c5919-3561-42f5-b9ed-26d70c20214e] | ||
description = "square puzzle with 32 rows" | ||
|
||
[1126f160-b094-4dc2-bf37-13e36e394867] | ||
description = "400 pieces square puzzle with only inside pieces and aspect ratio" | ||
|
||
[a9743178-5642-4cc0-8fdb-00d6b031c3a0] | ||
description = "1500 pieces landscape puzzle with 30 rows and 1.6 aspect ratio" | ||
|
||
[f6378369-989c-497f-a6e2-f30b1fa76cba] | ||
description = "300 pieces portrait puzzle with 70 border pieces" | ||
|
||
[f53f82ba-5663-4c7e-9e86-57fdbb3e53d2] | ||
description = "puzzle with insufficient data" | ||
|
||
[a3d5c31a-cc74-44bf-b4fc-9e4d65f1ac1a] | ||
description = "puzzle with contradictory data" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule PiecingItTogether do | ||
@doc """ | ||
Fill in missing jigsaw puzzle details from partial data | ||
""" | ||
|
||
defmodule JigsawPuzzle do | ||
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. Nitpick: I'm not sure if I would have chosen a nested module for this exercise. I think I would have created a module called |
||
@type format() :: :landscape | :portrait | :square | ||
@type t() :: %__MODULE__{ | ||
pieces: pos_integer(), | ||
rows: pos_integer(), | ||
columns: pos_integer(), | ||
format: format(), | ||
aspect_ratio: float(), | ||
border: pos_integer(), | ||
inside: pos_integer() | ||
Comment on lines
+9
to
+15
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. Should this type be extended somehow to signal that any of those values might be |
||
} | ||
|
||
defstruct [:pieces, :rows, :columns, :format, :aspect_ratio, :border, :inside] | ||
end | ||
|
||
@spec jigsaw_data(jigsaw_puzzle :: JigsawPuzzle.t()) :: | ||
{:ok, JigsawPuzzle.t()} | {:error, String.t()} | ||
def jigsaw_data(jigsaw_puzzle) do | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule PiecingItTogether.MixProject do | ||
use Mix.Project | ||
|
||
def project do | ||
[ | ||
app: :piecing_it_together, | ||
version: "0.1.0", | ||
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 |
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.
Maybe like this? There's a lot of math: