import math
from robot_control_class import RobotControl
def get_highest_lowest():
control = RobotControl()
values_positions_list = []
# Iterate over laser readings from 0 to 719
for i in range(720):
laser_value = control.get_laser(i)
# Filter out "inf" values and add numeric values with their positions to the list
if isinstance(laser_value, (int, float)) and not math.isinf(laser_value):
values_positions_list.append((laser_value, i))
# If no valid laser values found, return empty lists for both positions
if not values_positions_list:
return [], []
# Find the highest and lowest numeric values and their positions
highest_value_position = max(values_positions_list, key=lambda x: x[0])[1]
lowest_value_position = min(values_positions_list, key=lambda x: x[0])[1]
# Print the positions of the lowest and highest numeric values
print("Position of highest value:", highest_value_position)
print("Position of lowest value:", lowest_value_position)
# Return the positions
return [lowest_value_position], [highest_value_position]
from robot_control_class import RobotControl
def get_highest_lowest():
control = RobotControl()
values_positions_list = []
# Iterate over laser readings from 0 to 719
for i in range(720):
laser_value = control.get_laser(i)
# Discard "inf" values
if not isinstance(laser_value, float) or not float('-inf') < laser_value < float('inf'):
# Add numeric values with their positions to the list
if isinstance(laser_value, (int, float)):
values_positions_list.append((laser_value, i))
values_positions_list.sort()
# If no valid laser values found, return empty list
if not values_positions_list:
return []
# Find the highest and lowest numeric values and their positions
lowest_value_position = min(values_positions_list, key=lambda x: x[0])[1]
highest_value_position = max(values_positions_list, key=lambda x: x[0])[1]
print([lowest_value_position, highest_value_position])
# Return the positions sorted in a single list
return [lowest_value_position, highest_value_position]
Hi,
I do not know the requirements, but maybe the “position” is not the ranges index, but an angle in radian.
About your algorithm:
I think, it is ok, but IMHO
a) it can be faster, if you pick the min and max values in the first loop. In that case, you do not have to store all the values and indexes and iterate again for max and iterate again for min.
b) it can be shorter (in code length) you can use numpy argmax/argmin instead of loops
But these are not serious things.
I found another interesting part:
values_positions_list.sort()
I do not know, how this sorts your list, but if you can sort the list by range value,
your min will be in the first position and your max will be at the last position, so no need to use min/max.
But sorting an array is very slow, I rather use a) option only. That is only O(n), so probably the fastest.
I see you are calling control.get_laser(i), but you could just call laser_values = control.get_laser_full() once and iterate over those values.
I think your solution is a bit complex.
You could just set initial values for indexes, and initial values, and compare the values in the indexes to find the higher and lower indexes.
Example:
higher_index = lower_index = -1
higher_value = -1
lower_value = 100000
# now, iterate over the laser_values and compare them:
for value in laser_values:
# do the checks now, and set the 4 variables defined above.
# At the end of this loop you will already have the values you are looking for.
Also, I see that sometimes you return [] and sometimes [value1, value1].
If the code that calls your function expects two values, you should always return two values. So, instead of returning [], you could return [lower_index, higher_index], for example, but the error is not exactly at this point.
Please consider the code example I provided above.