-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb6853e
commit 83b05aa
Showing
27 changed files
with
52 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,19 @@ | ||
export const reverseString = (text: string) => { | ||
return Array.from(text).reverse().join(''); | ||
}; | ||
|
||
export const reverseWithLoop = (text: string) => { | ||
let reversed = ''; | ||
|
||
for (const character of text) { | ||
reversed = character + reversed; | ||
} | ||
|
||
return reversed; | ||
}; | ||
|
||
export const reverseWithReduce = (text: string) => { | ||
return text | ||
.split('') | ||
.reduce((reversed, character) => character + reversed, ''); | ||
}; |
24 changes: 24 additions & 0 deletions
24
src/algorithms/string-reverse/tests/string-reverse.test.ts
This file contains 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 @@ | ||
import { describe, test } from 'vitest'; | ||
import { | ||
reverseString, | ||
reverseWithLoop, | ||
reverseWithReduce, | ||
} from '../string-reverse'; | ||
|
||
describe('String Reversal', () => { | ||
test('Reverse function exists', () => { | ||
expect([reverseString, reverseWithLoop, reverseWithReduce]).toBeDefined(); | ||
}); | ||
|
||
test('Reverse reverses a string', () => { | ||
expect(reverseString('abcd')).toEqual('dcba'); | ||
expect(reverseWithLoop('abcd')).toEqual('dcba'); | ||
expect(reverseWithReduce('abcd')).toEqual('dcba'); | ||
}); | ||
|
||
test('Reverse reverses a string', () => { | ||
expect(reverseString(' abcd')).toEqual('dcba '); | ||
expect(reverseWithLoop(' abcd')).toEqual('dcba '); | ||
expect(reverseWithReduce(' abcd')).toEqual('dcba '); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
console.log('Typescript Data Structures & Algorithms'); | ||
import './arrays/arrayClass'; | ||
import './hash-table/HashTable'; | ||
import './linked-list/LinkedList'; | ||
import './doubly-linked-list/DoublyLinkedList'; | ||
import './stack/StackClass'; | ||
import './queue/QueueClass'; | ||
import './tree/binary-search/BinarySearchTree'; | ||
import './graph/Graph'; | ||
|
||
import './data-structures/arrays/arrayClass'; | ||
import './data-structures/doubly-linked-list/DoublyLinkedList'; | ||
import './data-structures/graph/Graph'; | ||
import './data-structures/hash-table/HashTable'; | ||
import './data-structures/linked-list/LinkedList'; | ||
import './data-structures/queue/QueueClass'; | ||
import './data-structures/stack/StackClass'; | ||
import './data-structures/tree/binary-search/BinarySearchTree'; |