11
11
# Add the python module to the path
12
12
sys .path .insert (0 , str (Path (__file__ ).parent .parent / 'python' ))
13
13
14
- from python import (algorithms , containers , exceptions , memory , random , shapes ,
15
- timing )
14
+ from python import algorithms , containers , exceptions , memory , random , shapes , timing
16
15
17
16
18
17
class ShapeAnalyzer :
@@ -68,8 +67,7 @@ def create_shapes():
68
67
69
68
# Transform for efficiency ratios (area/perimeter)
70
69
efficiency_ratios = algorithms .transform (
71
- list (zip (areas , perimeters )),
72
- lambda ap : ap [0 ] / ap [1 ] if ap [1 ] > 0 else 0
70
+ list (zip (areas , perimeters )), lambda ap : ap [0 ] / ap [1 ] if ap [1 ] > 0 else 0
73
71
)
74
72
75
73
self .operation_count += 1
@@ -81,10 +79,10 @@ def create_shapes():
81
79
'statistics' : {
82
80
'large_shapes' : large_shapes ,
83
81
'complex_shapes' : complex_shapes ,
84
- 'avg_efficiency' : sum (efficiency_ratios ) / len (efficiency_ratios )
82
+ 'avg_efficiency' : sum (efficiency_ratios ) / len (efficiency_ratios ),
85
83
},
86
84
'creation_time' : creation_stats ['human_readable' ]['mean' ],
87
- 'operation_count' : self .operation_count
85
+ 'operation_count' : self .operation_count ,
88
86
}
89
87
90
88
@@ -125,7 +123,7 @@ def safe_statistical_analysis(squares):
125
123
'max' : max_val ,
126
124
'median' : squares [len (squares ) // 2 ],
127
125
'sum' : sum (squares ),
128
- 'large_values' : algorithms .count_if (squares , lambda x : x > 1000 )
126
+ 'large_values' : algorithms .count_if (squares , lambda x : x > 1000 ),
129
127
}
130
128
131
129
# Execute pipeline
@@ -174,12 +172,13 @@ def benchmark_sorting_algorithms(self) -> dict:
174
172
'large_random' : lambda : random .RandomGenerator (42 ).integers (1 , 1000 , 1000 ),
175
173
'already_sorted' : lambda : list (range (1 , 501 )),
176
174
'reverse_sorted' : lambda : list (range (500 , 0 , - 1 )),
177
- 'mostly_sorted' : lambda : list (range (1 , 500 )) + [499 , 498 , 497 ]
175
+ 'mostly_sorted' : lambda : list (range (1 , 500 )) + [499 , 498 , 497 ],
178
176
}
179
177
180
178
results = {}
181
179
182
180
for scenario_name , data_gen in scenarios .items ():
181
+
183
182
def sort_test ():
184
183
data = data_gen ()
185
184
algorithms .sort_inplace (data )
@@ -189,7 +188,7 @@ def sort_test():
189
188
results [scenario_name ] = {
190
189
'mean_time' : stats ['human_readable' ]['mean' ],
191
190
'iterations' : stats ['iterations' ],
192
- 'data_size' : len (data_gen ())
191
+ 'data_size' : len (data_gen ()),
193
192
}
194
193
195
194
return results
@@ -217,7 +216,7 @@ def mixed_operations():
217
216
operations = {
218
217
'filter' : filter_operation ,
219
218
'transform' : transform_operation ,
220
- 'mixed' : mixed_operations
219
+ 'mixed' : mixed_operations ,
221
220
}
222
221
223
222
results = {}
@@ -250,7 +249,7 @@ def cleanup_heavy_operations():
250
249
251
250
operations = {
252
251
'shape_creation' : create_many_shapes ,
253
- 'cleanup_operations' : cleanup_heavy_operations
252
+ 'cleanup_operations' : cleanup_heavy_operations ,
254
253
}
255
254
256
255
results = {}
@@ -268,7 +267,7 @@ def run_full_benchmark(self) -> dict:
268
267
self .results = {
269
268
'sorting' : self .benchmark_sorting_algorithms (),
270
269
'containers' : self .benchmark_container_operations (),
271
- 'memory' : self .benchmark_memory_management ()
270
+ 'memory' : self .benchmark_memory_management (),
272
271
}
273
272
274
273
return self .results
@@ -289,8 +288,7 @@ def monte_carlo_pi_estimation(samples: int = 1000000) -> exceptions.Result[float
289
288
290
289
# Count points inside unit circle
291
290
inside_circle = algorithms .count_if (
292
- list (zip (x_coords , y_coords )),
293
- lambda p : p [0 ]** 2 + p [1 ]** 2 <= 1.0
291
+ list (zip (x_coords , y_coords )), lambda p : p [0 ] ** 2 + p [1 ] ** 2 <= 1.0
294
292
)
295
293
296
294
# Estimate π
@@ -311,24 +309,26 @@ def advanced_functional_programming_example():
311
309
data = gen .integers (1 , 100 , 50 )
312
310
313
311
# Complex functional chain with multiple transformations
314
- result = (algorithms .functional_chain (data )
315
- .filter (lambda x : x % 3 == 0 ) # Divisible by 3
316
- .map (lambda x : x * x ) # Square them
317
- .filter (lambda x : x < 1000 ) # Keep reasonable size
318
- .sort (reverse = True ) # Sort descending
319
- .take (10 ) # Take top 10
320
- .map (lambda x : x / 9 ) # Divide by 9
321
- .collect ())
312
+ result = (
313
+ algorithms .functional_chain (data )
314
+ .filter (lambda x : x % 3 == 0 ) # Divisible by 3
315
+ .map (lambda x : x * x ) # Square them
316
+ .filter (lambda x : x < 1000 ) # Keep reasonable size
317
+ .sort (reverse = True ) # Sort descending
318
+ .take (10 ) # Take top 10
319
+ .map (lambda x : x / 9 ) # Divide by 9
320
+ .collect ()
321
+ )
322
322
323
323
print (f'Functional chain result: { result } ' )
324
324
325
325
# Pipeline composition
326
326
numerical_pipeline = algorithms .pipeline (
327
- lambda nums : [x for x in nums if x > 20 ], # Filter
328
- lambda nums : [x * 2 for x in nums ], # Double
327
+ lambda nums : [x for x in nums if x > 20 ], # Filter
328
+ lambda nums : [x * 2 for x in nums ], # Double
329
329
lambda nums : algorithms .transform (nums , lambda x : x + 1 ), # Add 1
330
- lambda nums : sorted (nums ), # Sort
331
- lambda nums : nums [:5 ] # Take first 5
330
+ lambda nums : sorted (nums ), # Sort
331
+ lambda nums : nums [:5 ], # Take first 5
332
332
)
333
333
334
334
pipeline_result = numerical_pipeline (gen .integers (1 , 50 , 20 ))
@@ -342,6 +342,7 @@ def safe_logarithm(x: float) -> exceptions.Result[float]:
342
342
if x <= 0 :
343
343
return exceptions .Result .error ('Logarithm of non-positive number' )
344
344
import math
345
+
345
346
return exceptions .Result .ok (math .log (x ))
346
347
347
348
# Chain safe operations
@@ -380,7 +381,9 @@ def real_world_simulation():
380
381
print (f"Environment Analysis Results:" )
381
382
print (f" - Total shapes: { analysis ['shape_count' ]} " )
382
383
print (f" - Area range: { analysis ['areas' ]['min' ]:.2f} - { analysis ['areas' ]['max' ]:.2f} " )
383
- print (f" - Perimeter range: { analysis ['perimeters' ]['min' ]:.2f} - { analysis ['perimeters' ]['max' ]:.2f} " )
384
+ print (
385
+ f" - Perimeter range: { analysis ['perimeters' ]['min' ]:.2f} - { analysis ['perimeters' ]['max' ]:.2f} "
386
+ )
384
387
print (f" - Large shapes (area > 50): { analysis ['statistics' ]['large_shapes' ]} " )
385
388
print (f" - Complex shapes (perimeter > 20): { analysis ['statistics' ]['complex_shapes' ]} " )
386
389
print (f" - Average efficiency ratio: { analysis ['statistics' ]['avg_efficiency' ]:.4f} " )
@@ -445,6 +448,7 @@ def main():
445
448
except Exception as e :
446
449
print (f'Error running advanced examples: { e } ' )
447
450
import traceback
451
+
448
452
traceback .print_exc ()
449
453
return 1
450
454
0 commit comments