Skip to content

chore: fix some issues to follow ts-standard #249

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions other/parse_nested_brackets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @example parseNestedBrackets(`<MAIN hoge><MAIN2 fuga>`) => [ '<MAIN hoge>', '<MAIN2 fuga>' ]
* @example parseNestedBrackets(
* `THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))`,
* { openBrackets: '(', closingBrackets: ')' }) =>
* { openBrackets: '(', closingBrackets: ')' }) =>
* [
'(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))',
'(ITEM fuga hoge)',
Expand All @@ -18,13 +18,14 @@
*/
export const parseNestedBrackets = (
text: string,
openBrackets = '<',
closingBrackets = '>'
) => {
openBrackets: string = '<',
closingBrackets: string = '>'
): string[] => {
let array: string[] = [] // The array of the tags in this present floor.
let prFloor = 0 // The present floor.
let begin = 0, // The begin index of the tag.
end = 0 // The end index of the tag.
let begin = 0 // The begin index of the tag.
let end = 0 // The end index of the tag.

for (let i = 0; i < text.length; i++) {
if (text[i] === openBrackets) {
prFloor++
Expand Down
6 changes: 2 additions & 4 deletions other/shuffle_array.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export function shuffleArray(arr: number[]) {
export function shuffleArray(arr: number[]): void {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
;[arr[i], arr[j]] = [arr[j], arr[i]]
}
}
2 changes: 1 addition & 1 deletion other/test/parse_nested_brackets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('parseNestedBrackets', () => {
it('should return an array of the tags (nested)', () => {
expect(
parseNestedBrackets(
`THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))`,
'THIS IS SAMPLE TEXT(MAIN hoge 0.1 fuga(ITEM fuga hoge)hoge(ITEM2 nogami(ABBR)))',
'(',
')'
)
Expand Down
2 changes: 1 addition & 1 deletion search/binary_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @param {number[]} array - sorted list of numbers
* @param {number} target - target number to search for
* @return {number} - index of the target number in the list, or null if not found
* @return {number | null} - index of the target number in the list, or null if not found
* @see [BinarySearch](https://www.geeksforgeeks.org/binary-search/)
* @example binarySearch([1,2,3], 2) => 1
* @example binarySearch([4,5,6], 2) => null
Expand Down
12 changes: 6 additions & 6 deletions search/exponential_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { binarySearchIterative } from './binary_search'
* thus avoiding several comparisons that would make the search more verbose.
*
* @param {number[]} array - sorted list of numbers
* @param {number} x - target number to search for
* @param {number} target - target number to search for
* @return {number | null} - index of the target number in the list, or null if not found
* @see [ExponentialSearch](https://www.geeksforgeeks.org/exponential-search/)
* @example exponentialSearch([1, 2, 3, 4, 5], 3) => 2
Expand All @@ -20,21 +20,21 @@ import { binarySearchIterative } from './binary_search'

export const exponentialSearch = (
array: number[],
x: number
target: number
): number | null => {
const arrayLength = array.length
if (arrayLength === 0) return null

if (array[0] === x) return 0
if (array[0] === target) return 0

let i = 1
while (i < arrayLength && array[i] <= x) {
i = i * 2
while (i < arrayLength && array[i] <= target) {
i *= 2
}

const start = Math.floor(i / 2)
const end = Math.min(i, arrayLength - 1)
const result = binarySearchIterative(array, x, start, end)
const result = binarySearchIterative(array, target, start, end)

return result
}
13 changes: 6 additions & 7 deletions search/fibonacci_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const fibonacciSearch = (
target: number
): number | null => {
const arrayLength = array.length
let a = 0 // (n-2)'th Fibonacci No.
let b = 1 // (n-1)'th Fibonacci No.
let c = a + b // n'th Fibonacci
let a: number = 0 // (n-2)'th Fibonacci No.
let b: number = 1 // (n-1)'th Fibonacci No.
let c: number = a + b // n'th Fibonacci

while (c < arrayLength) {
a = b
Expand All @@ -31,11 +31,10 @@ export const fibonacciSearch = (
let offset = -1

while (c > 1) {
let i = Math.min(offset + a, arrayLength - 1)
const i = Math.min(offset + a, arrayLength - 1)

if (array[i] < target) {
c = b
b = a
;[c, b] = [b, a]
a = c - b
offset = i
} else if (array[i] > target) {
Expand All @@ -48,7 +47,7 @@ export const fibonacciSearch = (
}
}

if (b && array[offset + 1] === target) {
if (Boolean(b) && array[offset + 1] === target) {
return offset + 1
}

Expand Down
6 changes: 3 additions & 3 deletions search/jump_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export const jumpSearch = (array: number[], target: number): number => {

// declare pointers for the current and next indexes and step size
const stepSize: number = Math.floor(Math.sqrt(array.length))
let currentIdx: number = 0,
nextIdx: number = stepSize
let currentIdx: number = 0
let nextIdx: number = stepSize

while (array[nextIdx - 1] < target) {
currentIdx = nextIdx
Expand All @@ -37,7 +37,7 @@ export const jumpSearch = (array: number[], target: number): number => {
}

for (let index = currentIdx; index < nextIdx; index++) {
if (array[index] == target) {
if (array[index] === target) {
return index
}
}
Expand Down
2 changes: 1 addition & 1 deletion search/linear_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* if it's not present, the return it will be -1
* @param {number[]} array - list of numbers
* @param {number} target - target number to search for
* @return {number} - index of the target number in the list, or -1 if not found
* @return {number | -1} - index of the target number in the list, or -1 if not found
* @see https://en.wikipedia.org/wiki/Linear_search\
* @example linearSearch([1,2,3,5], 3) => 2
* @example linearSearch([1,5,6], 2) => -1
Expand Down
50 changes: 50 additions & 0 deletions search/minesweeper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Author: IcarusTheFly (https://github.com/IcarusTheFly)
* Minesweeper explanation can be found in: https://en.wikipedia.org/wiki/Minesweeper_(video_game)
* This function will take a rectangular matrix filled with boolean values - the value for a cell
* with a mine will be true, otherwise it will be false.
* As a result it will return a rectangular matrix where each cell will have an integer that
* counts all the mines in the adjacent cells
* Two cells should share at least one corner to be considered adjacent
*/

/**
* @function minesweeper
* @description It counts the amount of mines surrounding every cell and returns a formatted matrix
* @param {boolean[][]} matrix
* @returns {number[][]} Matrix of numbers with the amount of mines surrounding each cell
* @example minesweeper([
[true, false, false],
[false, true, false],
[false, false, false]
]) ===> [
[1, 2, 1],
[2, 1, 1],
[1, 1, 1]
]
*/

export const minesweeper = (matrix: boolean[][]): number[][] => {
const arrResult: number[][] = []

for (let x = 0; x < matrix.length; x++) {
const arrLine: number[] = []

for (let y = 0; y < matrix[x].length; y++) {
let minesInCell: number = 0

for (let xi = x - 1; xi <= x + 1; xi++) {
if (matrix[xi] !== undefined) {
for (let yi = y - 1; yi <= y + 1; yi++) {
if ((xi !== x || yi !== y) && matrix[xi][yi]) {
minesInCell++
}
}
}
}
arrLine.push(minesInCell)
}
arrResult.push(arrLine)
}
return arrResult
}
64 changes: 64 additions & 0 deletions search/rabin_karp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Implements the Rabin-Karp algorithm for pattern searching.
*
* The Rabin-Karp algorithm is a string searching algorithm that uses hashing to find patterns in strings.
* It is faster than naive string matching algorithms because it avoids comparing every character in the text.
*
* This implementation uses a rolling hash function to efficiently compute the hash values of substrings.
* It also uses a modulo operator to reduce the size of the hash values, which helps to prevent hash collisions.
*
* The algorithm returns an array of indices where the pattern is found in the text. If the pattern is not
* found, the algorithm returns an empty array.
*
* [Reference](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm)
*/

const BASE = 256 // The number of characters in the alphabet
const MOD = 997 // A prime number used for the hash function

function rabinKarpSearch(text: string, pattern: string): number[] {
const patternLength: number = pattern.length
const textLength: number = text.length
const hashPattern: number = hash(pattern, patternLength)
const hashText: number[] = []
const indices: number[] = []

// Calculate the hash of the first substring in the text
hashText[0] = hash(text, patternLength)

// Precompute BASE^(patternLength-1) % MOD
const basePow: number = Math.pow(BASE, patternLength - 1) % MOD

for (let i = 1; i <= textLength - patternLength + 1; i++) {
// Update the rolling hash by removing the first character
// and adding the next character in the text
hashText[i] =
(BASE * (hashText[i - 1] - text.charCodeAt(i - 1) * basePow) +
text.charCodeAt(i + patternLength - 1)) %
MOD

// In case of hash collision, check character by character
if (hashText[i] < 0) {
hashText[i] += MOD
}

// Check if the hashes match and perform a character-wise comparison
if (hashText[i] === hashPattern) {
if (text.substring(i, i + patternLength) === pattern) {
indices.push(i) // Store the index where the pattern is found
}
}
}

return indices
}

function hash(str: string, length: number): number {
let hashValue: number = 0
for (let i = 0; i < length; i++) {
hashValue = (hashValue * BASE + str.charCodeAt(i)) % MOD
}
return hashValue
}

export { rabinKarpSearch }
4 changes: 2 additions & 2 deletions search/sentinel_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @param {number[]} array - sorted list of numbers
* @param {number} target - target number to search for
* @return {number|null} - index of the target number in the list, or null if not found
* @return {number | null} - index of the target number in the list, or null if not found
* @see [SentinelSearch](https://www.geeksforgeeks.org/sentinel-linear-search/)
* @example sentinelSearch([1,2,3], 2) => 1
* @example sentinelSearch([4,5,6], 2) => null
Expand All @@ -30,7 +30,7 @@ export const sentinelSearch = (
if (arrayLength === 0) return null

// Element to be searched is placed at the last index
const last = array[arrayLength - 1]
const last: number = array[arrayLength - 1]
array[arrayLength - 1] = target

let index: number = 0
Expand Down
87 changes: 87 additions & 0 deletions search/ternary_search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* Ternary search is similar to binary search but it divides the sorted array
* into three parts and determines which part the key lies in. The array will
* be divided into three intervals by using two middle points, mid1 and mid2.
* The value of the key will first be compared with the two mid points, the value
* will be returned if there is a match. Then, if the value of the key is less
* than mid1, narrow the interval to the first part. Else, if the value of the
* key is greater than mid2, narrow the interval to the third part. Otherwise,
* narrow the interval to the middle part. Repeat the steps until the value is
* found or the interval is empty (value not found after checking all elements).
*
* Reference: https://www.geeksforgeeks.org/ternary-search/
*/

function ternarySearchRecursive(
arr: any[],
key: any,
low: number = 0,
high: number = arr.length - 1
): number {
// if low > high => we have searched the whole array without finding the item
if (low > high) return -1

// find the mid1 and mid2
const mid1: number = Math.floor(low + (high - low) / 3)
const mid2: number = Math.floor(high - (high - low) / 3)

// check if key is found at any mid
// return index of key if found
if (arr[mid1] === key) return mid1

// return index of key if found
if (arr[mid2] === key) return mid2

// since the key is not found at mid,
// check in which region it is present
// and repeat the Search operation
// in that region
if (key < arr[mid1]) {
// the key lies in between low and mid1
return ternarySearchRecursive(arr, key, low, mid1 - 1)
} else if (key > arr[mid2]) {
// the key lies in between mid2 and high
return ternarySearchRecursive(arr, key, mid2 + 1, high)
} else {
// the key lies in between mid1 and mid2
return ternarySearchRecursive(arr, key, mid1 + 1, mid2 - 1)
}
}

function ternarySearchIterative(
arr: any[],
key: any,
low: number = 0,
high: number = arr.length - 1
): number {
while (high >= low) {
// find the mid1 and mid2
const mid1: number = Math.floor(low + (high - low) / 3)
const mid2: number = Math.floor(high - (high - low) / 3)

// check if key is found at any mid
// return index of key if found
if (arr[mid1] === key) return mid1

// return index of key if found
if (arr[mid2] === key) return mid2

// since the key is not found at mid,
// check in which region it is present
// and repeat the Search operation
// in that region
if (key < arr[mid1]) {
// the key lies in between low and mid1
high = mid1 - 1
} else if (key > arr[mid2]) {
// the key lies in between mid2 and high
low = mid2 + 1
} else {
// the key lies in between mid1 and mid2
;[low, high] = [mid1 + 1, mid2 - 1]
}
}
// the key was not found
return -1
}

export { ternarySearchRecursive, ternarySearchIterative }
2 changes: 1 addition & 1 deletion search/test/binary_search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { binarySearchIterative, binarySearchRecursive } from '../binary_search'

describe('BinarySearch', () => {
const testArray: number[] = [1, 2, 3, 4]
type FunctionsArray = { (array: number[], index: number): number | null }[]
type FunctionsArray = Array<(array: number[], index: number) => number | null>
const functions: FunctionsArray = [
binarySearchIterative,
binarySearchRecursive
Expand Down
Loading