Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 23:40

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Error in Algorithm Implementation

Discussion on Error in Algorithm Implementation within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2022
Posts: 14
Received Thanks: 0
Error in Algorithm Implementation

I'm putting together a Master's Algorithm for a time series analysis assignment. However, I appear to have encountered an unusual bug that is preventing my code from running as planned. The relevant section of the code is as follows:
Code:
def masters_algorithm(data):
    if not data or len(data) == 0:
        return None

    # Extracting timestamps and values from data
    timestamps = [item[0] for item in data]
    values = [item[1] for item in data]

    # Applying a moving average to the values
    window_size = 3
    moving_averages = []
    for i in range(len(values) - window_size + 1):
        window_sum = sum(values[i:i + window_size])
        moving_averages.append(window_sum / window_size)

    # Finding the timestamp corresponding to the highest moving average
    max_average = max(moving_averages)
    max_average_index = moving_averages.index(max_average)
    result_timestamp = timestamps[max_average_index]

    return result_timestamp

# Example usage
data_points = [(1609459200, 10), (1609545600, 5), (1609632000, 2), (1609718400, 7), (1609804800, 15)]
result = masters_algorithm(data_points)
print(result)
The method seeks the timestamp that corresponds to the highest moving average of the data points, as seen in this . However, when the code is executed with the sample data_points, it does not yield the expected timestamp. I believe there is a problem with how I calculate the moving averages or obtain the correct date.

Could you kindly assist me in determining the source of the problem and making recommendations for how to resolve it? Your help would be highly appreciated!

Thank you very much!
TonyFinch09 is offline  
Old 07/23/2023, 23:06   #2



 
cypher's Avatar
 
elite*gold: 600
The Black Market: 1061/0/0
Join Date: Sep 2008
Posts: 10,540
Received Thanks: 3,079
Quote:
Originally Posted by TonyFinch09 View Post
I'm putting together a Master's Algorithm for a time series analysis assignment. However, I appear to have encountered an unusual bug that is preventing my code from running as planned. The relevant section of the code is as follows:
Code:
def masters_algorithm(data):
    if not data or len(data) == 0:
        return None

    # Extracting timestamps and values from data
    timestamps = [item[0] for item in data]
    values = [item[1] for item in data]

    # Applying a moving average to the values
    window_size = 3
    moving_averages = []
    for i in range(len(values) - window_size + 1):
        window_sum = sum(values[i:i + window_size])
        moving_averages.append(window_sum / window_size)

    # Finding the timestamp corresponding to the highest moving average
    max_average = max(moving_averages)
    max_average_index = moving_averages.index(max_average)
    result_timestamp = timestamps[max_average_index]

    return result_timestamp

# Example usage
data_points = [(1609459200, 10), (1609545600, 5), (1609632000, 2), (1609718400, 7), (1609804800, 15)]
result = masters_algorithm(data_points)
print(result)
The method seeks the timestamp that corresponds to the highest moving average of the data points, as seen in this . However, when the code is executed with the sample data_points, it does not yield the expected timestamp. I believe there is a problem with how I calculate the moving averages or obtain the correct date.

Could you kindly assist me in determining the source of the problem and making recommendations for how to resolve it? Your help would be highly appreciated!

Thank you very much!
The master's theorem has nothing to do with your assignment. It is used to determine the time complexity of recursive functions. You should rename your method.

Your method returns the correct value. What timestamp did you expect it to return? (2+7+15) / 3 is the highest moving average. The timestamp for the first value (2) is 1609632000. That's what your method returns.
cypher is offline  
Reply


Similar Threads Similar Threads
[Just another graph-related algorithm] Kruskal`s algorithm
07/25/2012 - CO2 Programming - 11 Replies
(Scroll down for the code if you aren`t interested in my wall of text.) Okay, my previous post on Dijkstra`s algorithm didn`t really get the attention it was supposed to get, but I`m still starting a trend out of this. What exactly? I will be posting general purpose algorithms from time to time, as I truly believe that this is one of the most fundamental and important aspect of being a good coder : knowing the basic algorithms (lol?). If anyone can provide a way of implementing this in a CO...
Getting blowfish key and using the algorithm
03/17/2011 - SRO Coding Corner - 10 Replies
Good evening:) I am still busy with the packets of silkroad and I'm now trying to understand on how to decrypt the packets but I'm having some problems with it. I read drew benton's artikel about the silkroad security but I still don't know on which blowfish key is used to decrypt the packets. let's say these four packets are the first 4 S -> C 00000 25 00 00 50 00 00 0e d2 a9 27 80 2d c3 23 6c f6 %..P.....'.-.#l. 00016 00 00 00 62 00 00 00 8d ac fe 38 3d d0 9c ef 67 ...
brauche vorschläge für rabot algorithm
10/01/2008 - Guild Wars - 6 Replies
da ich gerade an einem ra-bot schreibe, suche/brauche ich einige schöne vorschläge für den kampf. !nicht, die skills, welche der bot benutzen soll oder welche charakterklasse! -wann sollte man angreifen? -wie lange angreifen/ wann ein wechsel stattfinden soll, etc? -auf was genau sollte man reagieren? -nur gruppenmitglieder verfolgen oder feinere laufwege (wie die laufwege generieren?)? vlt. habt ihr schon ra-bots geschrieben. wie habt ihr die kampfsituation ausgearbeitet?
INTERESTED IN STUDYING THE PACKET ALGORITHM OF THE CLIENT.EXE
08/25/2008 - Perfect World - 7 Replies
just wait for updates



All times are GMT +1. The time now is 23:40.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.