-
-
Notifications
You must be signed in to change notification settings - Fork 163
[New Exercise] Add word-search exercise #867
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
99 changes: 99 additions & 0 deletions
99
exercises/practice/word-search/.meta/Sources/WordSearch/WordSearchExample.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import Foundation | ||
|
|
||
| struct WordLocation: Equatable { | ||
|
|
||
| struct Location: Equatable { | ||
| let row: Int | ||
| let column: Int | ||
| } | ||
|
|
||
| let start: Location | ||
| let end: Location | ||
|
|
||
| } | ||
|
|
||
| func search(words: [String], in grid: [String]) -> [String: WordLocation?] { | ||
| let board: [[Character]] = grid.map { Array($0) } | ||
| let trie = TrieNode.buildTrie(words) | ||
| var result = [String: WordLocation?]() | ||
|
|
||
| let directions = [ | ||
| (-1,0), (1,0), (0,-1), (0,1), | ||
| (-1,-1), (1,1), (1,-1), (-1,1) | ||
| ] | ||
| for row in 0..<board.count { | ||
| for column in 0..<board[0].count { | ||
| for direction in directions { | ||
| TrieNode.dfs( | ||
| board: board, | ||
| initialRow: row, | ||
| initialColumn: column, | ||
| row: row, | ||
| column: column, | ||
| direction: direction, | ||
| node: trie, | ||
| result: &result | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| fileprivate class TrieNode { | ||
| var children: [Character: TrieNode] = [:] | ||
| var word: String? = nil | ||
| } | ||
|
|
||
| fileprivate extension TrieNode { | ||
|
|
||
| static func buildTrie(_ words: [String]) -> TrieNode { | ||
| let root = TrieNode() | ||
| for word in words { | ||
| var node = root | ||
| for char in word { | ||
| if node.children[char] == nil { | ||
| node.children[char] = TrieNode() | ||
| } | ||
| node = node.children[char]! | ||
| } | ||
| node.word = word | ||
| } | ||
| return root | ||
| } | ||
|
|
||
| static func dfs( | ||
| board: [[Character]], | ||
| initialRow: Int, | ||
| initialColumn: Int, | ||
| row: Int, | ||
| column: Int, | ||
| direction: (Int, Int), | ||
| node: TrieNode, | ||
| result: inout [String: WordLocation?] | ||
| ) { | ||
| guard row >= 0, row < board.count, column >= 0, column < board[0].count else { return } | ||
|
|
||
| let char = board[row][column] | ||
| guard let nextNode = node.children[char] else { return } | ||
|
|
||
| if let word = nextNode.word { | ||
| let start = WordLocation.Location(row: initialRow + 1, column: initialColumn + 1) | ||
| let end = WordLocation.Location(row: row + 1, column: column + 1) | ||
| result[word] = WordLocation(start: start, end: end) | ||
| } | ||
|
|
||
| dfs( | ||
| board: board, | ||
| initialRow: initialRow, | ||
| initialColumn: initialColumn, | ||
| row: row + direction.0, | ||
| column: column + direction.1, | ||
| direction: direction, | ||
| node: nextNode, | ||
| result: &result | ||
| ) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "authors": [ | ||
| "Sencudra" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "Sources/WordSearch/WordSearch.swift" | ||
| ], | ||
| "test": [ | ||
| "Tests/WordSearchTests/WordSearchTests.swift" | ||
| ], | ||
| "example": [ | ||
| ".meta/Sources/WordSearch/WordSearchExample.swift" | ||
| ] | ||
| }, | ||
| "blurb": "Create a program to solve a word search puzzle." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import Testing | ||
| import Foundation | ||
| @testable import {{exercise | camelCase}} | ||
|
|
||
| let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
|
||
| @Suite struct {{exercise | camelCase}}Tests { | ||
| {% for case in cases %} | ||
| {% if forloop.first -%} | ||
| @Test("{{case.description}}") | ||
| {% else -%} | ||
| @Test("{{case.description}}", .enabled(if: RUNALL)) | ||
| {% endif -%} | ||
| func test{{case.description | camelCase}}() { | ||
| let grid = {{case.input.grid | toStringArray}} | ||
| let words = {{case.input.wordsToSearchFor | toStringArray}} | ||
|
|
||
| var expected = [String: WordLocation?]() | ||
| {%- for word in case.expected %} | ||
| expected["{{word}}"] = | ||
| {%- if case.expected[word] | isNull -%} | ||
| nil | ||
| {%- else -%} | ||
| WordLocation( | ||
| start: {{case.expected[word]["start"] | toStringDictionary}}, | ||
| end: {{case.expected[word]["end"] | toStringDictionary}} | ||
| ) | ||
| {%- endif %} | ||
| {%- endfor %} | ||
|
|
||
| #expect({{case.property}}(words: words, in: grid) == expected) | ||
| } | ||
| {% endfor -%} | ||
| } | ||
|
|
||
| fileprivate extension WordLocation { | ||
|
|
||
| init?(start: [String: Int], end: [String: Int]) { | ||
| guard | ||
| let startRow = start["row"], | ||
| let startColumn = start["column"], | ||
| let endRow = end["row"], | ||
| let endColumn = end["column"] | ||
| else { | ||
| return nil | ||
| } | ||
| let start = Location(row: startRow, column: startColumn) | ||
| let end = Location(row: endRow, column: endColumn) | ||
| self.init(start: start, end: end) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // swift-tools-version:6.0 | ||
|
|
||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "WordSearch", | ||
| products: [ | ||
| .library( | ||
| name: "WordSearch", | ||
| targets: ["WordSearch"]) | ||
| ], | ||
| dependencies: [], | ||
| targets: [ | ||
| .target( | ||
| name: "WordSearch", | ||
| dependencies: []), | ||
| .testTarget( | ||
| name: "WordSearchTests", | ||
| dependencies: ["WordSearch"]), | ||
| ] | ||
| ) |
17 changes: 17 additions & 0 deletions
17
exercises/practice/word-search/Sources/WordSearch/WordSearch.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import Foundation | ||
|
|
||
| struct WordLocation: Equatable { | ||
|
|
||
| struct Location: Equatable { | ||
| let row: Int | ||
| let column: Int | ||
| } | ||
|
|
||
| let start: Location | ||
| let end: Location | ||
|
|
||
| } | ||
|
|
||
| func search(words: [String], in grid: [String]) -> [String: WordLocation?] { | ||
| // Write your code for the 'WordSearch' exercise in this file. | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 would increase the difficulty to at least 5
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. Done.