dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
108 lines
2.0 KiB
Markdown
108 lines
2.0 KiB
Markdown
---
|
|
id: 69cfca90e8a0a6d4d6871c52
|
|
title: "Challenge 268: Narcissistic Number"
|
|
challengeType: 29
|
|
dashedName: challenge-268
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given a positive integer, determine whether it is a narcissistic number.
|
|
|
|
- A number is narcissistic if the sum of each of its digits raised to the power of the total number of digits equals the number itself.
|
|
|
|
For example, 153 has 3 digits, and 1<sup>3</sup> + 5<sup>3</sup> + 3<sup>3</sup> = 153, so it is narcissistic.
|
|
|
|
# --hints--
|
|
|
|
`is_narcissistic(153)` should return `True`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(153), True)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(154)` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(154), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(371)` should return `True`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(371), True)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(512)` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(512), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(9)` should return `True`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(9), True)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(11)` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(11), False)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(9474)` should return `True`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(9474), True)`)
|
|
}})
|
|
```
|
|
|
|
`is_narcissistic(6549)` should return `False`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertIs(is_narcissistic(6549), False)`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def is_narcissistic(n):
|
|
|
|
return n
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def is_narcissistic(n):
|
|
digits = str(n)
|
|
power = len(digits)
|
|
return sum(int(d) ** power for d in digits) == n
|
|
```
|