Nov-20-2023, 10:01 AM
(This post was last modified: Nov-20-2023, 12:12 PM by Gribouillis.)
def find_peaks_troughs(values): if not values: return "No values provided" # Initialize the first peak and trough peak = trough = values[0] peaks = [] troughs = [] is_peak = False for value in values: if is_peak: # Check if current value is a trough if value / peak <= 0.9: troughs.append(peak) trough = value is_peak = False else: # Check if current value is a peak if value / trough >= 1.1: peaks.append(trough) peak = value is_peak = True # Add the last peak or trough found if is_peak: troughs.append(peak) else: peaks.append(trough) return peaks, troughs # List of values values = [100, 99, 90, 79, 62, 94, 88, 75, 80, 72, 74, 87, 84, 90] # Find peaks and troughs peaks, troughs = find_peaks_troughs(values) # Print results for i in range(min(len(peaks), len(troughs))): print(f"Peak {i + 1} = {troughs[i]}") print(f"Trough {i + 1} = {peaks[i]}")This code will process your list of values and print out the peaks and troughs as per your requirement. It treats the initial value as a potential trough and toggles between looking for peaks and troughs as it finds significant movements in the values.
Gribouillis write Nov-20-2023, 12:12 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.