Skip to content

Commit 4ab6a06

Browse files
feat: add strobogrammatic number algorithm
1 parent 5c39e87 commit 4ab6a06

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎Maths/StrobogrammaticNumber.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @function isStrobogrammaticNumber
3+
* @description A strobogrammatic number is a number whose numeral is
4+
* rotationally symmetric, so it appears the same when rotated 180 degrees.
5+
* Valid strobogrammatic digit pairs: 0↔0, 1↔1, 6↔9, 8↔8, 9↔6.
6+
* @param {number} n - A positive integer
7+
* @returns {boolean} - True if n is a strobogrammatic number, false otherwise
8+
* @see https://en.wikipedia.org/wiki/Strobogrammatic_number
9+
* @example isStrobogrammaticNumber(69) => true
10+
* @example isStrobogrammaticNumber(818) => true
11+
* @example isStrobogrammaticNumber(123) => false
12+
*/
13+
const isStrobogrammaticNumber = (n) => {
14+
if (!Number.isInteger(n) || n < 0) {
15+
throw new TypeError('Input must be a non-negative integer')
16+
}
17+
const map = { 0: '0', 1: '1', 6: '9', 8: '8', 9: '6' }
18+
const str = String(n)
19+
let left = 0
20+
let right = str.length - 1
21+
while (left <= right) {
22+
if (map[str[left]] !== str[right]) return false
23+
left++
24+
right--
25+
}
26+
return true
27+
}
28+
29+
export { isStrobogrammaticNumber }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { isStrobogrammaticNumber } from '../StrobogrammaticNumber.js'
2+
3+
describe('isStrobogrammaticNumber', () => {
4+
test('0 is strobogrammatic', () => {
5+
expect(isStrobogrammaticNumber(0)).toBe(true)
6+
})
7+
8+
test('1 is strobogrammatic', () => {
9+
expect(isStrobogrammaticNumber(1)).toBe(true)
10+
})
11+
12+
test('8 is strobogrammatic', () => {
13+
expect(isStrobogrammaticNumber(8)).toBe(true)
14+
})
15+
16+
test('69 is strobogrammatic', () => {
17+
expect(isStrobogrammaticNumber(69)).toBe(true)
18+
})
19+
20+
test('96 is strobogrammatic', () => {
21+
expect(isStrobogrammaticNumber(96)).toBe(true)
22+
})
23+
24+
test('818 is strobogrammatic', () => {
25+
expect(isStrobogrammaticNumber(818)).toBe(true)
26+
})
27+
28+
test('1001 is strobogrammatic', () => {
29+
expect(isStrobogrammaticNumber(1001)).toBe(true)
30+
})
31+
32+
test('123 is NOT strobogrammatic', () => {
33+
expect(isStrobogrammaticNumber(123)).toBe(false)
34+
})
35+
36+
test('2 is NOT strobogrammatic', () => {
37+
expect(isStrobogrammaticNumber(2)).toBe(false)
38+
})
39+
40+
test('100 is NOT strobogrammatic', () => {
41+
expect(isStrobogrammaticNumber(100)).toBe(false)
42+
})
43+
44+
test('throws TypeError for negative input', () => {
45+
expect(() => isStrobogrammaticNumber(-69)).toThrow(TypeError)
46+
})
47+
48+
test('throws TypeError for float input', () => {
49+
expect(() => isStrobogrammaticNumber(6.9)).toThrow(TypeError)
50+
})
51+
})

0 commit comments

Comments
 (0)