Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e695b5

Browse files
committed
FEAT Implemented recursive function to compute floor of lg(N)
1 parent 0a9c400 commit 1e695b5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

‎main.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# code based on https://github.com/dev-xero/java-algorithms-exercise-practice
22

33
"""main.py
4-
Python code implementation of some exercises presented in chapter 1.1
5-
of the algorithms book
4+
Python code implementation of some exercises presented in chapter 1.1
5+
of the algorithms book
66
"""
77

88
# ---------------------------------------------------------------------------------------------------------
@@ -87,6 +87,17 @@ def matrix_transposition(the_matrix: [[int]]) -> [[int]]:
8787
# ---------------------------------------------------------------------------------------------------------
8888

8989

90+
def lg(n: float) -> int:
91+
"""Returns the largest integer not larger than lg(n)"""
92+
if n <= 1:
93+
return 0
94+
95+
return lg(n / 2) + 1
96+
97+
98+
# ---------------------------------------------------------------------------------------------------------
99+
100+
90101
def main():
91102
"""For testing"""
92103
test_x: float = 0.1
@@ -103,13 +114,14 @@ def main():
103114
[6, 5, 2]
104115
]
105116

117+
test_int_array = [5, 7, 6, 9, 3, 8, 2, 4, 1, 10]
118+
106119
test_int_matrix = [[0] * 2 for _ in range(3)]
120+
107121
for i in range(3):
108122
for j in range(2):
109123
test_int_matrix[i][j] = generator.randint(0, 9)
110124

111-
test_int_array = [5, 7, 6, 9, 3, 8, 2, 4, 1, 10]
112-
113125
print(is_between_zero_and_one(test_x, test_y))
114126
print(to_binary_string(8))
115127
print()
@@ -131,6 +143,9 @@ def main():
131143
print("- Transposition")
132144
print_two_dm_int_array(matrix_transposition(test_int_matrix))
133145

146+
print()
147+
print(f"floor of lg(128): { lg(128) }")
148+
134149

135150
# ---------------------------------------------------------------------------------------------------------
136151

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /