Skip to content

Commit

Permalink
finding number of digits using logs created with unit tests
Browse files Browse the repository at this point in the history
vicky1999 authored Nov 17, 2020
1 parent cf62ede commit 2dbf296
Showing 4 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -13,5 +13,5 @@ __pycache__/
/*.egg
# docs
build/

pythonenv3.8/
.vscode/
1 change: 1 addition & 0 deletions algorithms/maths/__init__.py
Original file line number Diff line number Diff line change
@@ -18,3 +18,4 @@
from .find_order_simple import *
from .find_primitive_root_simple import *
from .diffie_hellman_key_exchange import *
from .num_digits import *
10 changes: 10 additions & 0 deletions algorithms/maths/num_digits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
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):
if(n==0):
return 1;
return int(math.log10(n))+1
15 changes: 14 additions & 1 deletion tests/test_maths.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,8 @@
cosine_similarity,
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
alice_private_key, alice_public_key, bob_private_key, bob_public_key, alice_shared_key, bob_shared_key, diffie_hellman_key_exchange,
num_digits
)

import unittest
@@ -369,6 +370,18 @@ def test_find_order_simple(self):
self.assertFalse(diffie_hellman_key_exchange(5, 211))
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))

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

0 comments on commit 2dbf296

Please sign in to comment.