Skip to content

Commit

Permalink
Added number.is_armstrong() method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 29, 2016
1 parent 80c5dd2 commit 0a70a4d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -13,6 +13,7 @@ Changelog
- Add ``first``.
- Add ``from_``.
- Add ``includes``.
- Add ``is_armstrong``.
- Add ``is_array``.
- Add ``is_boolean``.
- Add ``is_empty``.
Expand Down
7 changes: 7 additions & 0 deletions docs/example.rst
Expand Up @@ -147,6 +147,13 @@ Numbers
>>> import sugar as _
>>> _.is_armstrong(371)
True
>>> _.is_armstrong(8208)
True
>>> _.is_armstrong(51)
False
>>> _.is_even(6)
True
>>> _.is_even(7)
Expand Down
8 changes: 7 additions & 1 deletion docs/upgrading.rst
Expand Up @@ -4,6 +4,10 @@
Upgrading
*********


From v0.1.0
===========

- 14 array methods

- :func:`sugar.arrays.add`.
Expand All @@ -22,8 +26,9 @@ Upgrading
- :func:`sugar.arrays.last`.


- 5 number methods
- 6 number methods

- :func:`sugar.number.is_armstrong`.
- :func:`sugar.number.is_even`.
- :func:`sugar.number.is_multiple_of`.
- :func:`sugar.number.is_odd`.
Expand All @@ -45,6 +50,7 @@ Improvements

The below methods are new apart from Sugar JS utility library:

- :func:`sugar.number.is_armstrong`.
- :func:`sugar.number.is_prime`.


Expand Down
1 change: 1 addition & 0 deletions sugar/__init__.py
Expand Up @@ -33,6 +33,7 @@
)

from .number import (
is_armstrong,
is_even,
is_multiple_of,
is_odd,
Expand Down
37 changes: 37 additions & 0 deletions sugar/number.py
Expand Up @@ -8,6 +8,43 @@
from ._compat import _range


def is_armstrong(num):
"""Retursn True if :attr:`num` is armstrong number.
Args:
num (int): Number passed in by the user.
Returns:
bool: True if the given :attr:`num` is armstrong number else False.
Example:
>>> is_armstrong(371)
True
>>> is_armstrong(8208)
True
>>> is_armstrong(51)
False
.. versionadded:: TODO
"""
total = 0
temp = num
digits = len(str(num))

while temp != 0:
remainder = temp % 10
total += math.pow(remainder, digits)
temp //= 10

if total == num:
result = True
else:
result = False

return result


def is_even(num):
"""Returns True if :attr:`num` is even.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_number.py
Expand Up @@ -5,6 +5,15 @@
import sugar as _


@parametrize('num,expected', [
(371, True),
(8208, True),
(51, False)
])
def test_is_armstrong(num, expected):
assert _.is_armstrong(num) == expected


@parametrize('num,expected', [
(6, True),
(7, False)
Expand Down

0 comments on commit 0a70a4d

Please sign in to comment.