From 1650f6e66af14976a6f847acda18bcbf5083ac11 Mon Sep 17 00:00:00 2001 From: Fatima Date: Mon, 23 Jan 2023 17:25:04 -0500 Subject: [PATCH 1/7] Is Palindrome Number --- maths/pallindrome_number.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 maths/pallindrome_number.ts diff --git a/maths/pallindrome_number.ts b/maths/pallindrome_number.ts new file mode 100644 index 00000000..4136bb31 --- /dev/null +++ b/maths/pallindrome_number.ts @@ -0,0 +1,19 @@ +/** + * A function to see if a number is a Pallindrome + * @param number The input integer + * @return {boolean} True or False based on the integer + * @example isPalindrome(12321) => true | isPalindrome(455) => false + * @see https://en.wikipedia.org/wiki/Palindromic_number + * @author FatimaChariwala + */ + +export const isPalindrome = (number: number): boolean => { + const stringValue = number.toString(); + length = stringValue.length - 1; + for (let i = 0; i < length; ++i, --length) { + if (stringValue[i] !== stringValue[length]) { + return false; + } + } + return true; +}; \ No newline at end of file From 1e32b1901a46e2edad56cf06412baafb0c8f55ef Mon Sep 17 00:00:00 2001 From: Fatima Date: Mon, 23 Jan 2023 17:56:32 -0500 Subject: [PATCH 2/7] Is Palindrome Number update --- ...pallindrome_number.ts => palindrome_number.ts} | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) rename maths/{pallindrome_number.ts => palindrome_number.ts} (52%) diff --git a/maths/pallindrome_number.ts b/maths/palindrome_number.ts similarity index 52% rename from maths/pallindrome_number.ts rename to maths/palindrome_number.ts index 4136bb31..3533c2b2 100644 --- a/maths/pallindrome_number.ts +++ b/maths/palindrome_number.ts @@ -7,13 +7,12 @@ * @author FatimaChariwala */ -export const isPalindrome = (number: number): boolean => { - const stringValue = number.toString(); - length = stringValue.length - 1; - for (let i = 0; i < length; ++i, --length) { - if (stringValue[i] !== stringValue[length]) { - return false; - } +export const IsPalindrome = (number: number): boolean => { + let reverseString = ""; + let stringValue = number.toString(); + let length = stringValue.length - 1; + for (let i = length; i >= 0; --i) { + reverseString = reverseString + stringValue[i]; } - return true; + return stringValue == reverseString; }; \ No newline at end of file From ce03e0f8ac3141d656c3f150c90609dc0012c340 Mon Sep 17 00:00:00 2001 From: Fatima Date: Mon, 23 Jan 2023 18:22:55 -0500 Subject: [PATCH 3/7] Added tests and updated Directory.md --- DIRECTORY.md | 1 + maths/{palindrome_number.ts => is_palindrome.ts} | 0 maths/test/is_palindrome.test.ts | 10 ++++++++++ 3 files changed, 11 insertions(+) rename maths/{palindrome_number.ts => is_palindrome.ts} (100%) create mode 100644 maths/test/is_palindrome.test.ts diff --git a/DIRECTORY.md b/DIRECTORY.md index b96d789d..238054a4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -27,6 +27,7 @@ * [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts) * [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts) * [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts) + * [IS Palindrome](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_palindrome.ts) * [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts) * [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts) * [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts) diff --git a/maths/palindrome_number.ts b/maths/is_palindrome.ts similarity index 100% rename from maths/palindrome_number.ts rename to maths/is_palindrome.ts diff --git a/maths/test/is_palindrome.test.ts b/maths/test/is_palindrome.test.ts new file mode 100644 index 00000000..af7122f2 --- /dev/null +++ b/maths/test/is_palindrome.test.ts @@ -0,0 +1,10 @@ +import { IsPalindrome } from "../is_palindrome"; + +describe("IsPalindrome", () => { + test.each([[5, true], [1234, false], [12321, true], [32.23, true], [31343, false]])( + "correct output for for %i", + (nums, expected) => { + expect(IsPalindrome(nums)).toBe(expected); + }, + ); +}); \ No newline at end of file From f790f547b3f506e5b3a1b4ef8ee687fe464cd188 Mon Sep 17 00:00:00 2001 From: Fatima Date: Mon, 23 Jan 2023 18:24:39 -0500 Subject: [PATCH 4/7] updated Directory.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 238054a4..aeff71c7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -27,7 +27,7 @@ * [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts) * [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts) * [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts) - * [IS Palindrome](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_palindrome.ts) + * [Is Palindrome](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_palindrome.ts) * [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts) * [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts) * [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts) From c1ca2c1568ac8e470fa7fd375b86c6d31fc047e0 Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Wed, 22 Mar 2023 15:52:22 +0100 Subject: [PATCH 5/7] feat(maths): improvements & resolved changes --- maths/is_palindrome.ts | 24 ++++++++++++++---------- maths/test/is_palindrome.test.ts | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/maths/is_palindrome.ts b/maths/is_palindrome.ts index 3533c2b2..2d30ccf5 100644 --- a/maths/is_palindrome.ts +++ b/maths/is_palindrome.ts @@ -1,18 +1,22 @@ /** - * A function to see if a number is a Pallindrome - * @param number The input integer - * @return {boolean} True or False based on the integer + * A function to see if a number is a Palindrome. + * + * @param number The input number. + * @return {boolean} Wether the number is a Palindrome or not. * @example isPalindrome(12321) => true | isPalindrome(455) => false * @see https://en.wikipedia.org/wiki/Palindromic_number * @author FatimaChariwala */ export const IsPalindrome = (number: number): boolean => { - let reverseString = ""; - let stringValue = number.toString(); - let length = stringValue.length - 1; - for (let i = length; i >= 0; --i) { - reverseString = reverseString + stringValue[i]; + const stringValue = number.toString(); + const length = stringValue.length - 1; + + for (let i = 0; i <= length; i++) { + if (stringValue[i] !== stringValue[length - i]) { + return false; + } } - return stringValue == reverseString; -}; \ No newline at end of file + + return true; +}; diff --git a/maths/test/is_palindrome.test.ts b/maths/test/is_palindrome.test.ts index af7122f2..160f4270 100644 --- a/maths/test/is_palindrome.test.ts +++ b/maths/test/is_palindrome.test.ts @@ -2,7 +2,7 @@ import { IsPalindrome } from "../is_palindrome"; describe("IsPalindrome", () => { test.each([[5, true], [1234, false], [12321, true], [32.23, true], [31343, false]])( - "correct output for for %i", + "correct output for %i", (nums, expected) => { expect(IsPalindrome(nums)).toBe(expected); }, From 565c0057cda014b0582c44873146ae3a85b98f3a Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Fri, 24 Mar 2023 15:03:54 +0100 Subject: [PATCH 6/7] feat: improves algorithm & adjusts tests --- maths/is_palindrome.ts | 22 ++++++++++------------ maths/test/is_palindrome.test.ts | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/maths/is_palindrome.ts b/maths/is_palindrome.ts index 2d30ccf5..ce17381f 100644 --- a/maths/is_palindrome.ts +++ b/maths/is_palindrome.ts @@ -1,22 +1,20 @@ /** - * A function to see if a number is a Palindrome. + * A function to see if a number is a palindrome. + * Time Complexity: O(log(n)) * * @param number The input number. * @return {boolean} Wether the number is a Palindrome or not. - * @example isPalindrome(12321) => true | isPalindrome(455) => false - * @see https://en.wikipedia.org/wiki/Palindromic_number - * @author FatimaChariwala */ - export const IsPalindrome = (number: number): boolean => { - const stringValue = number.toString(); - const length = stringValue.length - 1; + if (number < 0 || (number % 10 === 0 && number !== 0)) { + return false; + } - for (let i = 0; i <= length; i++) { - if (stringValue[i] !== stringValue[length - i]) { - return false; - } + let reversed: number = 0; + while (number > reversed) { + reversed = reversed * 10 + (number % 10); + number = Math.floor(number / 10); } - return true; + return number === reversed || number === Math.floor(reversed / 10); }; diff --git a/maths/test/is_palindrome.test.ts b/maths/test/is_palindrome.test.ts index 160f4270..fa375f73 100644 --- a/maths/test/is_palindrome.test.ts +++ b/maths/test/is_palindrome.test.ts @@ -1,7 +1,7 @@ import { IsPalindrome } from "../is_palindrome"; describe("IsPalindrome", () => { - test.each([[5, true], [1234, false], [12321, true], [32.23, true], [31343, false]])( + test.each([[5, true], [1234, false], [12321, true], [31343, false]])( "correct output for %i", (nums, expected) => { expect(IsPalindrome(nums)).toBe(expected); From 7e44879c60a21cab81f5c8c1c7249be6ef111788 Mon Sep 17 00:00:00 2001 From: zfl4wless Date: Sat, 25 Mar 2023 11:00:07 +0100 Subject: [PATCH 7/7] feat: documents limitation --- maths/is_palindrome.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/is_palindrome.ts b/maths/is_palindrome.ts index ce17381f..f093d9be 100644 --- a/maths/is_palindrome.ts +++ b/maths/is_palindrome.ts @@ -1,5 +1,6 @@ /** * A function to see if a number is a palindrome. + * Note that if the reversed number is larger than MAX_SAFE_INTEGER, rounding errors may occur and the result may be incorrect. * Time Complexity: O(log(n)) * * @param number The input number.