From 4ab6a0663cb6f714112616bf7f5f30f81f075b5e Mon Sep 17 00:00:00 2001 From: sarthakgupta1907-cell Date: Sat, 7 Nov 2026 10:49:05 +0530 Subject: [PATCH] feat: add strobogrammatic number algorithm --- Maths/StrobogrammaticNumber.js | 29 ++++++++++++++ Maths/test/StrobogrammaticNumber.test.js | 51 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 Maths/StrobogrammaticNumber.js create mode 100644 Maths/test/StrobogrammaticNumber.test.js diff --git a/Maths/StrobogrammaticNumber.js b/Maths/StrobogrammaticNumber.js new file mode 100644 index 0000000000..99d286b379 --- /dev/null +++ b/Maths/StrobogrammaticNumber.js @@ -0,0 +1,29 @@ +/** + * @function isStrobogrammaticNumber + * @description A strobogrammatic number is a number whose numeral is + * rotationally symmetric, so it appears the same when rotated 180 degrees. + * Valid strobogrammatic digit pairs: 0↔0, 1↔1, 6↔9, 8↔8, 9↔6. + * @param {number} n - A positive integer + * @returns {boolean} - True if n is a strobogrammatic number, false otherwise + * @see https://en.wikipedia.org/wiki/Strobogrammatic_number + * @example isStrobogrammaticNumber(69) => true + * @example isStrobogrammaticNumber(818) => true + * @example isStrobogrammaticNumber(123) => false + */ +const isStrobogrammaticNumber = (n) => { + if (!Number.isInteger(n) || n < 0) { + throw new TypeError('Input must be a non-negative integer') + } + const map = { 0: '0', 1: '1', 6: '9', 8: '8', 9: '6' } + const str = String(n) + let left = 0 + let right = str.length - 1 + while (left <= right) { + if (map[str[left]] !== str[right]) return false + left++ + right-- + } + return true +} + +export { isStrobogrammaticNumber } diff --git a/Maths/test/StrobogrammaticNumber.test.js b/Maths/test/StrobogrammaticNumber.test.js new file mode 100644 index 0000000000..4e8b81285d --- /dev/null +++ b/Maths/test/StrobogrammaticNumber.test.js @@ -0,0 +1,51 @@ +import { isStrobogrammaticNumber } from '../StrobogrammaticNumber.js' + +describe('isStrobogrammaticNumber', () => { + test('0 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(0)).toBe(true) + }) + + test('1 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(1)).toBe(true) + }) + + test('8 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(8)).toBe(true) + }) + + test('69 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(69)).toBe(true) + }) + + test('96 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(96)).toBe(true) + }) + + test('818 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(818)).toBe(true) + }) + + test('1001 is strobogrammatic', () => { + expect(isStrobogrammaticNumber(1001)).toBe(true) + }) + + test('123 is NOT strobogrammatic', () => { + expect(isStrobogrammaticNumber(123)).toBe(false) + }) + + test('2 is NOT strobogrammatic', () => { + expect(isStrobogrammaticNumber(2)).toBe(false) + }) + + test('100 is NOT strobogrammatic', () => { + expect(isStrobogrammaticNumber(100)).toBe(false) + }) + + test('throws TypeError for negative input', () => { + expect(() => isStrobogrammaticNumber(-69)).toThrow(TypeError) + }) + + test('throws TypeError for float input', () => { + expect(() => isStrobogrammaticNumber(6.9)).toThrow(TypeError) + }) +})