Python Forum
Can someone help me solve this programming problem?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone help me solve this programming problem?
#7
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.
Reply


Messages In This Thread
RE: Can someone help me solve this programming problem? - by EdwardMatthew - Nov-20-2023, 10:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple problem, how best to solve it? SuchUmami 2 757 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
Question Linear Programming Problem Axel_LF 0 763 Feb-23-2023, 11:03 PM
Last Post: Axel_LF
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,285 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How do I solve the second problem? Cranberry 1 1,160 May-16-2022, 11:56 AM
Last Post: Larz60+
  Try to solve GTG multiplication table problem. Frankduc 6 2,076 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Sudoku Solver, please help to solve a problem. AdithyaR 5 2,174 Oct-28-2021, 03:15 PM
Last Post: deanhystad
Lightbulb Object Oriented programming (OOP) problem OmegaRed94 6 2,979 May-31-2021, 07:19 PM
Last Post: OmegaRed94
  python 3 raspberry pi 4 dual control motor programming problem yome 0 2,009 Mar-21-2021, 05:17 PM
Last Post: yome
  General list size question to solve problem Milfredo 3 2,399 Sep-27-2020, 08:42 AM
Last Post: Milfredo
  I want to solve the following problem srisrinu 4 6,008 May-09-2020, 01:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020