Skip to content

Commit

Permalink
feat: string reversal algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Aug 11, 2023
1 parent eb6853e commit 83b05aa
Show file tree
Hide file tree
Showing 27 changed files with 52 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/algorithms/string-reverse/string-reverse.ts
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 src/algorithms/string-reverse/tests/string-reverse.test.ts
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.
17 changes: 9 additions & 8 deletions src/index.ts
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';

0 comments on commit 83b05aa

Please sign in to comment.