1
1
# code based on https://github.com/dev-xero/java-algorithms-exercise-practice
2
2
3
3
"""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
6
6
"""
7
7
8
8
# ---------------------------------------------------------------------------------------------------------
@@ -87,6 +87,17 @@ def matrix_transposition(the_matrix: [[int]]) -> [[int]]:
87
87
# ---------------------------------------------------------------------------------------------------------
88
88
89
89
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
+
90
101
def main ():
91
102
"""For testing"""
92
103
test_x : float = 0.1
@@ -103,13 +114,14 @@ def main():
103
114
[6 , 5 , 2 ]
104
115
]
105
116
117
+ test_int_array = [5 , 7 , 6 , 9 , 3 , 8 , 2 , 4 , 1 , 10 ]
118
+
106
119
test_int_matrix = [[0 ] * 2 for _ in range (3 )]
120
+
107
121
for i in range (3 ):
108
122
for j in range (2 ):
109
123
test_int_matrix [i ][j ] = generator .randint (0 , 9 )
110
124
111
- test_int_array = [5 , 7 , 6 , 9 , 3 , 8 , 2 , 4 , 1 , 10 ]
112
-
113
125
print (is_between_zero_and_one (test_x , test_y ))
114
126
print (to_binary_string (8 ))
115
127
print ()
@@ -131,6 +143,9 @@ def main():
131
143
print ("- Transposition" )
132
144
print_two_dm_int_array (matrix_transposition (test_int_matrix ))
133
145
146
+ print ()
147
+ print (f"floor of lg(128): { lg (128 ) } " )
148
+
134
149
135
150
# ---------------------------------------------------------------------------------------------------------
136
151
0 commit comments