Skip to content

Commit

Permalink
Merge pull request keon#740 from vicky1999/master
Browse files Browse the repository at this point in the history
finding number of digits using logs created with unit tests
  • Loading branch information
ankit167 authored Jan 27, 2021
2 parents 162024f + fec7a5b commit e48b9ac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ __pycache__/
/*.egg
# docs
build/

pythonenv3.8/
.vscode/
# Ignoring the virtual Environment when using GitHub Codespaces
.venv/
2 changes: 2 additions & 0 deletions algorithms/maths/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from .find_order_simple import *
from .find_primitive_root_simple import *
from .diffie_hellman_key_exchange import *
from .num_digits import *
from .power import *
from .magic_number import *
from .krishnamurthy_number import *

11 changes: 11 additions & 0 deletions algorithms/maths/num_digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
num_digits() method will return the number of digits of a number in O(1) time using math.log10() method.
"""

import math

def num_digits(n):
n=abs(n)
if(n==0):
return 1;
return int(math.log10(n))+1
18 changes: 18 additions & 0 deletions tests/test_maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
magic_number,
find_order,
find_primitive_root,
alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key, diffie_hellman_key_exchange,
num_digits,
alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key,
diffie_hellman_key_exchange, krishnamurthy_number
)
Expand Down Expand Up @@ -448,5 +450,21 @@ def test_find_order_simple(self):
self.assertTrue(diffie_hellman_key_exchange(11, 971))


class TestNumberOfDigits(unittest.TestCase):
"""[summary]
Test for the file num_digits.py
Arguments:
unittest {[type]} -- [description]
"""
def test_num_digits(self):
self.assertEqual(2,num_digits(12))
self.assertEqual(5,num_digits(99999))
self.assertEqual(1,num_digits(8))
self.assertEqual(1,num_digits(0))
self.assertEqual(1,num_digits(-5))
self.assertEqual(3,num_digits(-254))


if __name__ == "__main__":
unittest.main()

0 comments on commit e48b9ac

Please sign in to comment.