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?
#5
Here is what I've currently got:

import pandas as pd


def detect_trend_change(data, threshold=10):
    trend_dataframes = []
    trend = None
    trend_sum = 0
    trend_points = []


    for i in range(1, len(data)):
        change_percent = 100 * (data[i] - data[i-1]) / data[i-1]
        current_trend = 'Uptrend' if change_percent > 0 else 'Downtrend'

        if trend is None:
            trend = current_trend
            trend_points.append(data[i-1])


        if current_trend == trend:
            trend_sum += abs(change_percent)
            trend_points.append(data[i])
        else:
            if abs(trend_sum) >= threshold:
                df = pd.DataFrame({'Value': trend_points, 'Trend': trend})
                trend_dataframes.append(df)
                trend_points = [data[i-1], data[i]]
                trend_sum = abs(change_percent)
                trend = current_trend
            else:
                trend_sum += abs(change_percent)
                trend_points.append(data[i])


    # Add the last trend
    if trend_points:
        df = pd.DataFrame({'Value': trend_points, 'Trend': trend})
        trend_dataframes.append(df)


    return trend_dataframes


# Provided dataset
dataset = [100, 99, 90, 79, 62, 94, 88, 75, 80, 72, 74, 87, 84, 90]


# Call the function with the dataset
resulting_dataframes = detect_trend_change(dataset)


# Print the resulting dataframes
for idx, df in enumerate(resulting_dataframes, 1):
    print(f"DataFrame {idx}:\n{df}\n")
However, it's coming out as:

DataFrame 1:
Value Trend
0 100 Downtrend
1 99 Downtrend
2 90 Downtrend
3 79 Downtrend
4 62 Downtrend

DataFrame 2:
Value Trend
0 62 Uptrend
1 94 Uptrend

DataFrame 3:
Value Trend
0 94 Downtrend
1 88 Downtrend
2 75 Downtrend

DataFrame 4:
Value Trend
0 75 Uptrend
1 80 Uptrend
2 72 Uptrend
3 74 Uptrend
4 87 Uptrend

DataFrame 5:
Value Trend
0 87 Downtrend
1 84 Downtrend
2 90 Downtrend


The errors start at dataframe 3, where it should have gone down to 72 because there was no 10% upmove to invalidate it. Then it should have went from 72 to 90 as an uptrend.
Reply


Messages In This Thread
RE: Can someone help me solve this programming problem? - by SuchUmami - Nov-16-2023, 05:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A simple problem, how best to solve it? SuchUmami 2 767 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
Question Linear Programming Problem Axel_LF 0 771 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,298 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How do I solve the second problem? Cranberry 1 1,176 May-16-2022, 11:56 AM
Last Post: Larz60+
  Try to solve GTG multiplication table problem. Frankduc 6 2,093 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Sudoku Solver, please help to solve a problem. AdithyaR 5 2,196 Oct-28-2021, 03:15 PM
Last Post: deanhystad
Lightbulb Object Oriented programming (OOP) problem OmegaRed94 6 3,002 May-31-2021, 07:19 PM
Last Post: OmegaRed94
  python 3 raspberry pi 4 dual control motor programming problem yome 0 2,028 Mar-21-2021, 05:17 PM
Last Post: yome
  General list size question to solve problem Milfredo 3 2,408 Sep-27-2020, 08:42 AM
Last Post: Milfredo
  I want to solve the following problem srisrinu 4 6,025 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