Skip to content

Commit

Permalink
new exercise: word-search
Browse files Browse the repository at this point in the history
Added a new Exercise per issue #238
  • Loading branch information
msomji committed Oct 23, 2019
1 parent ff3f7d1 commit b9760de
Show file tree
Hide file tree
Showing 11 changed files with 5,092 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@
"unicode"
]
},
{
"slug": "word-search",
"uuid": "8ba2c83e-f544-11e9-802a-5aa538984bd8",
"core": false,
"unlocked_by": "linked-list",
"difficulty": 8,
"topics": [
"arrays",
"conditionals",
"loops",
"equality",
"optional_values",
"parsing",
"text_formatting"
]
},
{
"slug": "difference-of-squares",
"uuid": "3f649490-dc7d-4a77-a2a0-2ae71ae834a9",
Expand Down
8 changes: 8 additions & 0 deletions exercises/word-search/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
bin/*
dist/*
docs/*
node_modules/*
production_node_modules/*
test/fixtures/*
tmp/*
jest.config.js
64 changes: 64 additions & 0 deletions exercises/word-search/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json",
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"env": {
"browser": true,
"es6": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/array-type": "off", // Styling not forced upon the student
"@typescript-eslint/explicit-function-return-type": [
"warn", {
"allowExpressions": false,
"allowTypedFunctionExpressions": true,
"allowHigherOrderFunctions": true
}
], // Prevent bugs
"@typescript-eslint/explicit-member-accessibility": "off", // Styling not forced upon the student
"@typescript-eslint/indent": "off", // Styling not forced upon the student
"@typescript-eslint/no-inferrable-types": [
"error", {
"ignoreParameters": true
}
],
"@typescript-eslint/member-delimiter-style": "off", // Styling not forced upon the student
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-parameter-properties": [
"warn", {
"allows": [
"private", "protected", "public",
"private readonly", "protected readonly", "public readonly"
]
}
], // only disallow readonly without an access modifier
"@typescript-eslint/no-unused-vars": "off", // Covered by the tsc compiler (noUnusedLocals)
"@typescript-eslint/no-use-before-define": [
"error", {
"functions": false,
"typedefs": false
}
], // Prevent bugs, not styling
"semi": "off", // Always disable base-rule
"@typescript-eslint/semi": "off" // Styling not forced upon student
}
}
63 changes: 63 additions & 0 deletions exercises/word-search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Word Search

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.
## Setup

Go through the setup instructions for TypeScript to install the necessary
dependencies:

[https://exercism.io/tracks/typescript/installation](https://exercism.io/tracks/typescript/installation)

## Requirements

Install assignment dependencies:

```bash
$ yarn install
```

## Making the test suite pass

Execute the tests with:

```bash
$ yarn test
```

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing `xit` to
`it`.

## Source

This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have
completed the exercise.
21 changes: 21 additions & 0 deletions exercises/word-search/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
verbose: true,
projects: [
'<rootDir>'
],
testMatch: [
"**/__tests__/**/*.[jt]s?(x)",
"**/test/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[jt]s?(x)"
],
testPathIgnorePatterns: [
'/(?:production_)?node_modules/',
'.d.ts$',
'<rootDir>/test/fixtures',
'<rootDir>/test/helpers',
'__mocks__'
],
transform: {
'^.+\\.[jt]sx?$': 'ts-jest',
},
};
29 changes: 29 additions & 0 deletions exercises/word-search/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@exercism/typescript",
"version": "1.0.0",
"description": "Exercism exercises in Typescript.",
"author": "",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/typescript"
},
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/node": "^12.7.12",
"@typescript-eslint/eslint-plugin": "^2.5.0",
"@typescript-eslint/parser": "^2.3.3",
"eslint": "^6.5.1",
"eslint-plugin-import": "^2.18.2",
"jest": "^24.9.0",
"ts-jest": "^24.1.0",
"typescript": "^3.6.4"
},
"scripts": {
"test": "yarn lint:types && jest --no-cache",
"lint": "yarn lint:types && yarn lint:ci",
"lint:types": "yarn tsc --noEmit -p .",
"lint:ci": "eslint . --ext .tsx,.ts"
},
"dependencies": {}
}
65 changes: 65 additions & 0 deletions exercises/word-search/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["esnext", "es2016", "es2017"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./build", /* Redirect output structure to the directory. */
// "rootDirs": ["./"], /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
"isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
"noEmitOnError": true, /* Do not emit outputs when compilation fails. */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": { "~src/*": ["./src/*"] }, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"compileOnSave": true,
"exclude": [
"node_modules"
]
}
91 changes: 91 additions & 0 deletions exercises/word-search/word-search.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
interface Result {
start: number[];
end: number[];
}

export default class WordSearch {
private grid: string[]
constructor(grid: string[]) {
this.grid = grid
}
private findCoordsWhereLetterMatch(currentLetter: string, board: string[]): number[][] {
return board.reduce((accumulatedCoordinates: number[][], row: string, rowNumber: number) => [...accumulatedCoordinates,
...row.split('')
.reduce((matchingLetterIndecies: number[], letter: string, index: number) => letter === currentLetter ? [...matchingLetterIndecies, index] : matchingLetterIndecies, [])
.reduce((coordinates: number[][], col) => [...coordinates, [rowNumber, col]], [])
],
[])
}

private getCoordsOfSurroundingLetters(initialCoord: number[], totalRows: number, totalColumns: number): number[][] {

const top: number[] = [initialCoord[0] - 1, initialCoord[1]]
const bottom: number[] = [initialCoord[0] + 1, initialCoord[1]]

const right: number[] = [initialCoord[0], initialCoord[1] + 1]
const left: number[] = [initialCoord[0], initialCoord[1] - 1]

const topRight: number[] = [initialCoord[0] + 1, initialCoord[1] + 1]
const topLeft: number[] = [initialCoord[0] + 1, initialCoord[1] - 1]

const bottomRight: number[] = [initialCoord[0] - 1, initialCoord[1] + 1]
const bottomLeft: number[] = [initialCoord[0] - 1, initialCoord[1] - 1]

return [top, bottom, right, left, topRight, topLeft, bottomRight, bottomLeft]
.filter((coordinates: number[]) =>
coordinates[0] <= (totalRows - 1) &&
coordinates[1] <= (totalColumns - 1) &&
coordinates[0] >= 0 &&
coordinates[1] >= 0)
}

private getValueFromCoordinate(board: string[], coordinates: number[]): string {
return board[coordinates[0]] && board[coordinates[0]].split('')[coordinates[1]]
}

private matchingValues(board: string[], coordinates: number[], letter: string): boolean {
return this.getValueFromCoordinate(board, coordinates) === letter
}

private getDirectionFunction(originCoords: number[], destinationCoords: number[]): (currentCoords: number[]) => number[] {
return (nextCoord: number[]): number[] => [nextCoord[0] + (destinationCoords[0] - originCoords[0]), nextCoord[1] + (destinationCoords[1] - originCoords[1])]
}


private getValidNeighbouringCoordinates(initial: number[], board: string[], letter: string): number[][] {
return this.getCoordsOfSurroundingLetters(initial, board.length, board[0].length)
.filter(neighbouringCoordinate => this.matchingValues(board, neighbouringCoordinate, letter))
}

private findOne(word: string, board: string[]): Result {
const allPossibleStartCoords: number[][] = this.findCoordsWhereLetterMatch(word[0], board)
const allPossibleCoordsForFirstTwoLetters: number[][][] = allPossibleStartCoords.reduce((accum: number[][][], initial) => {
return [...accum, ...this.getValidNeighbouringCoordinates(initial, board, word[1]).map(secondCoordinate => [initial, secondCoordinate])]
}, [])

const allPossiblePaths: number[][][] = allPossibleCoordsForFirstTwoLetters.map((coordsSoFar: number[][]) => {
const incrementFunction = this.getDirectionFunction(coordsSoFar[0], coordsSoFar[1])
return word.substr(2, word.length).split('').reduce((accum: number[][], _) => [...accum, incrementFunction(accum[accum.length - 1])], coordsSoFar)
})

const validPaths: number[][][] = allPossiblePaths.reduce((validPaths: number[][][], path: number[][]) =>
word.split('')
.map((letter, index) => this.matchingValues(board, path[index], letter))
.includes(false) ? validPaths : [...validPaths, path], [] as number[][][])


return validPaths.reduce((_: Result, path: number[][]) => ({
start: path[0].map(c => c + 1),
end: path[path.length - 1].map(c => c + 1)
}), {} as Result)
}

public find(words: string[]): { [word: string]: Result } | { [word: string]: undefined } {
return words.reduce((accum: { [word: string]: Result } | { [word: string]: undefined }, word) => {
const result = this.findOne(word, this.grid)
accum[word] = Object.keys(result).length == 0 ? undefined : result

return accum
}, {} as { [word: string]: Result } | { [word: string]: undefined })
}
}
Empty file.
Loading

0 comments on commit b9760de

Please sign in to comment.