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

Add word-search exercise #133

Merged
merged 1 commit into from
Oct 9, 2024
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,14 @@
"prerequisites": [],
"difficulty": 6
},
{
"slug": "word-search",
"name": "Word Search",
"uuid": "6e00fa23-6f9c-412b-9cc4-f257158c8f64",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "bowling",
"name": "Bowling",
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/word-search/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

In word search puzzles you get a square of letters and have to find specific words in them.

For example:

```text
jefblpepre
camdcimgtc
oivokprjsm
pbwasqroua
rixilelhrs
wolcqlirpc
screeaumgr
alxhpburyi
jalaycalmp
clojurermt
```

There are several programming languages hidden in the above square.

Words can be hidden in all kinds of directions: left-to-right, right-to-left, vertical and diagonal.

Given a puzzle and a list of words return the location of the first and last letter of each word.
63 changes: 63 additions & 0 deletions exercises/practice/word-search/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module [search]

Position : { column : U64, row : U64 }
WordLocation : { start : Position, end : Position }

## Pad each row with spaces to ensure all rows have the same width
padRight : List (List U8) -> { rows : List (List U8), width : U64 }
padRight = \grid ->
width = grid |> List.map List.len |> List.max |> Result.withDefault 0
padSpaces = \chars -> List.repeat ' ' (width - List.len chars)
{
rows: grid |> List.map \chars -> chars |> List.concat (padSpaces chars),
width,
}

getChar : List (List U8), U64, U64 -> U8
getChar = \grid, columnIndex, rowIndex ->
grid
|> List.get rowIndex
|> Result.withDefault []
|> List.get columnIndex
|> Result.withDefault ' '

search : Str, List Str -> Dict Str WordLocation
search = \grid, wordsToSearchFor ->
{ rows, width } = grid |> Str.split "\n" |> List.map Str.toUtf8 |> padRight
height = List.len rows
heightI64 = height |> Num.toI64
widthI64 = width |> Num.toI64
maxLength = Num.max width height |> Num.toI64
# for all possible starting positions:
List.range { start: At 0, end: Before width }
|> List.joinMap \columnIndex ->
List.range { start: At 0, end: Before height }
|> List.joinMap \rowIndex ->
# for all possible directions:
[(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
|> List.joinMap \(dirColumn, dirRow) ->
start = { column: columnIndex + 1, row: rowIndex + 1 }
# for all possible lengths:
List.range { start: At 0, end: Before maxLength } # for all possible words starting at the given position and
# going in the given direction, take note of the start and end
# positions
|> List.walkUntil { foundWords: [], chars: [] } \state, index ->
endColumnIndex = Num.toI64 columnIndex + dirColumn * index
endRowIndex = Num.toI64 rowIndex + dirRow * index
if endColumnIndex < 0 || endColumnIndex >= widthI64 || endRowIndex < 0 || endRowIndex >= heightI64 then
Break state
else

endColumnIndexU64 = endColumnIndex |> Num.toU64
endrowIndexU64 = endRowIndex |> Num.toU64
char = rows |> getChar endColumnIndexU64 endrowIndexU64
newChars = state.chars |> List.append char
end = { column: endColumnIndexU64 + 1, row: endrowIndexU64 + 1 }
maybeWord = wordsToSearchFor |> List.findFirst \word -> word |> Str.toUtf8 == newChars
newFoundWords =
when maybeWord is
Ok word -> state.foundWords |> List.append (word, { start, end })
Err NotFound -> state.foundWords
Continue { foundWords: newFoundWords, chars: newChars }
|> .foundWords
|> Dict.fromList
17 changes: 17 additions & 0 deletions exercises/practice/word-search/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"ageron"
],
"files": {
"solution": [
"WordSearch.roc"
],
"test": [
"word-search-test.roc"
],
"example": [
".meta/Example.roc"
]
},
"blurb": "Create a program to solve a word search puzzle."
}
24 changes: 24 additions & 0 deletions exercises/practice/word-search/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}
{{ macros.header() }}

import {{ exercise | to_pascal }} exposing [{{ cases[0]["property"] | to_camel }}]

{% for case in cases -%}
# {{ case["description"] }}
expect
grid = {{ case["input"]["grid"] | to_roc_multiline_string | indent(8) }}
wordsToSearchFor = {{ case["input"]["wordsToSearchFor"] | to_roc }}
result = grid |> {{ case["property"] | to_camel }} wordsToSearchFor
expected = Dict.fromList [
{%- for word, result in case["expected"].items() %}
{%- if result is none %}
# {{ word | to_roc }} is not in the grid
{%- else %}
({{ word | to_roc }}, {{ result | to_roc }}),
{%- endif %}
{%- endfor %}
]
result == expected

{% endfor %}
82 changes: 82 additions & 0 deletions exercises/practice/word-search/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 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.

[b4057815-0d01-41f0-9119-6a91f54b2a0a]
description = "Should accept an initial game grid and a target search word"

[6b22bcc5-6cbf-4674-931b-d2edbff73132]
description = "Should locate one word written left to right"

[ff462410-434b-442d-9bc3-3360c75f34a8]
description = "Should locate the same word written left to right in a different position"

[a02febae-6347-443e-b99c-ab0afb0b8fca]
description = "Should locate a different left to right word"

[e42e9987-6304-4e13-8232-fa07d5280130]
description = "Should locate that different left to right word in a different position"

[9bff3cee-49b9-4775-bdfb-d55b43a70b2f]
description = "Should locate a left to right word in two line grid"

[851a35fb-f499-4ec1-9581-395a87903a22]
description = "Should locate a left to right word in three line grid"

[2f3dcf84-ba7d-4b75-8b8d-a3672b32c035]
description = "Should locate a left to right word in ten line grid"

[006d4856-f365-4e84-a18c-7d129ce9eefb]
description = "Should locate that left to right word in a different position in a ten line grid"

[eff7ac9f-ff11-443e-9747-40850c12ab60]
description = "Should locate a different left to right word in a ten line grid"

[dea39f86-8c67-4164-8884-13bfc48bd13b]
description = "Should locate multiple words"

[29e6a6a5-f80c-48a6-8e68-05bbbe187a09]
description = "Should locate a single word written right to left"

[3cf34428-b43f-48b6-b332-ea0b8836011d]
description = "Should locate multiple words written in different horizontal directions"

[2c8cd344-a02f-464b-93b6-8bf1bd890003]
description = "Should locate words written top to bottom"

[9ee1e43d-e59d-4c32-9a5f-6a22d4a1550f]
description = "Should locate words written bottom to top"

[6a21a676-f59e-4238-8e88-9f81015afae9]
description = "Should locate words written top left to bottom right"

[c9125189-1861-4b0d-a14e-ba5dab29ca7c]
description = "Should locate words written bottom right to top left"

[b19e2149-7fc5-41ec-a8a9-9bc6c6c38c40]
description = "Should locate words written bottom left to top right"

[69e1d994-a6d7-4e24-9b5a-db76751c2ef8]
description = "Should locate words written top right to bottom left"

[695531db-69eb-463f-8bad-8de3bf5ef198]
description = "Should fail to locate a word that is not in the puzzle"

[fda5b937-6774-4a52-8f89-f64ed833b175]
description = "Should fail to locate words that are not on horizontal, vertical, or diagonal lines"

[5b6198eb-2847-4e2f-8efe-65045df16bd3]
description = "Should not concatenate different lines to find a horizontal word"

[eba44139-a34f-4a92-98e1-bd5f259e5769]
description = "Should not wrap around horizontally to find a word"

[cd1f0fa8-76af-4167-b105-935f78364dac]
description = "Should not wrap around vertically to find a word"
8 changes: 8 additions & 0 deletions exercises/practice/word-search/WordSearch.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module [search]

Position : { column : U64, row : U64 }
WordLocation : { start : Position, end : Position }

search : Str, List Str -> Dict Str WordLocation
search = \grid, wordsToSearchFor ->
crash "Please implement the 'search' function"
Loading