Skip to content

Commit

Permalink
perf: improve performance by checking if the text length is prime or not
Browse files Browse the repository at this point in the history
  • Loading branch information
umutcanbolat committed Oct 5, 2019
1 parent 2266fd5 commit ce6a9f8
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import { isPrime } from './util';

export interface Output {
repeated: string;
count: number;
}

/**
* @Method: Returns the repeated string along with the repeat count.
* @Method Returns the repeated string along with the repeat count.
* @param string - The string that will be searched for repeats.
* @returns Object containing the repeated string and the repeat count.
*/
export default function unrepeat(text: string): Output {
const textLength = text.length;

// divider: number to attempt dividing the original text
// initially, the minimum prime number
let divider = 2;

// if textLength is a prime number,
// its repeated string is either a single character, or the text itself.
// so we will not iterate through it to find out the repeated string.
if (isPrime(textLength)) {
divider = textLength;
}

// start from the divider and iterate till the text length.
// i: possible repeats count
// start from the minimum prime number and iterate till the text length.
for (let i = 2; i <= text.length; i++) {
for (let i = divider; i <= textLength; i++) {
// check if the text can be divided by i without remainder.
// if there is a reminder, the repeats count cannot be i. So we can skip.
if (text.length % i === 0) {
if (textLength % i === 0) {
// possible repeated string and its length.
const repLength = text.length / i;
const repLength = textLength / i;
const repeated = text.substr(0, repLength);

// set this flag false if it is not repeated on every step.
Expand All @@ -35,7 +50,7 @@ export default function unrepeat(text: string): Output {
// if the possible repeated is repeated along the whole text.
if (isRepeated) {
// repeat count is equal to the text length, repeated is a single char.
if (i === text.length) {
if (i === textLength) {
return {
repeated,
count: i,
Expand All @@ -45,7 +60,7 @@ export default function unrepeat(text: string): Output {
const res = unrepeat(repeated);
return {
repeated: res.repeated,
count: text.length / res.repeated.length,
count: textLength / res.repeated.length,
};
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @Method Returns if provided number is prime or not.
* @param n - The number to check.
* @returns boolean
*/
export function isPrime(n: number): boolean {
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
return false;
}
}
return n > 1;
}
23 changes: 23 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,27 @@ describe('unrepeat', () => {
expect(result.repeated).toEqual(char);
expect(result.count).toEqual(repCount);
});

describe('chck for the strings whose length is a prime number', () => {
it('should work for not repeated strings', () => {
const prime = 76521967;
const char = 'A';
const text = char.repeat(prime - 1) + 'B';

const result = unrepeat(text);

expect(result.repeated).toEqual(text);
expect(result.count).toEqual(1);
});

it('should work for repeated strings', () => {
const prime = 76521967;
const char = 'A';

const result = unrepeat(char.repeat(prime));

expect(result.repeated).toEqual(char);
expect(result.count).toEqual(prime);
});
});
});
39 changes: 39 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { isPrime } from '../src/util';

describe('isPrime', () => {
it('should return true for 2', () => {
expect(isPrime(2)).toBe(true);
});

it('should return false for numbers < 2', () => {
expect(isPrime(1)).toBe(false);
expect(isPrime(0)).toBe(false);
expect(isPrime(-1)).toBe(false);
expect(isPrime(-7)).toBe(false);
});

it('should return false for even numbers > 2', () => {
expect(isPrime(4)).toBe(false);
expect(isPrime(20)).toBe(false);
expect(isPrime(40)).toBe(false);
expect(isPrime(100)).toBe(false);
});

it('should return false for odd numbers which are not prime', () => {
expect(isPrime(9)).toBe(false);
expect(isPrime(27)).toBe(false);
expect(isPrime(9523371)).toBe(false);
expect(isPrime(89525673)).toBe(false);
});

it('should return true for prime numbers', () => {
expect(isPrime(3)).toBe(true);
expect(isPrime(5)).toBe(true);
expect(isPrime(7)).toBe(true);
expect(isPrime(11)).toBe(true);
expect(isPrime(89)).toBe(true);
expect(isPrime(97)).toBe(true);
expect(isPrime(6523757)).toBe(true);
expect(isPrime(76521967)).toBe(true);
});
});

0 comments on commit ce6a9f8

Please sign in to comment.