1

我想创建一个算法,但不知道如何开始。

这个算法实际上是一个方法,它接受一个包含 N 个对象的数组,其中包含一些属性 createdAt 和 value。我将数组从旧的到新的(createdAt)排序,然后我必须找出可用数据的一致性,这意味着每一个小时我至少有 5 条记录,每半小时有 2 条记录。

示例测试代码:

- (void) normalizeData:(NSArray*)records
{
// sort the records
NSArray* sortedRecords = [records sortWithCreatedAt];

// split all dates in the records, distinct them, and create a dictionary with a key for every date, for value create another dictionary with the hour as key and the records as the value.

NSArray* distinctDates = [sortedRecords valueForKeyPath:@"@distinctUnionOfObjects.createdAt"]; // should only consider month-day-year-hour
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
for (NSDate* date in distinctDates)
    {
    NSString* stringDate = [date string]; 
    NSArray* recordsForDate = [sortedRecords valueForKeyPath:[NSString stringWithFormat:@"[collect].{createdAt=%@}.self", stringDate]]; // let's say you got them with this line
    [dictionary setObject:recordsForDate forKey:date];
    }

for (NSDate* keyDate in dictionary)
   {
   NSArray* records = [dictionary objectForKey:keyDate];
   Record* previousRecord = nil;
   for (Records* record in records)
      {
      // I'll have to keep the previous record and compare the time difference with the new
      NSInteger secondsAfterDate = 0;
      if (previousRecord)
         {
         secondsAfterDate = [record.createdAt timeIntervalSinceDate:previousRecord.createdAt];
         // add logic to create trend difference in a model that has for every hour of the records count, the records and suffice description
         // logic if the records count and timespan is suffice.

         }
      previousRecord = record;
      }
   }
}

我将不胜感激对方法中的过程的任何贡献。

此外,最终目标是为处理的记录的每个结果创建一个返回(调用块处理程序)。逻辑应该以每小时至少 5 条记录以及它们之间的时间跨度在 15 分钟以内结束。

4

1 回答 1

0

取记录收集的总时间长度(第一条记录的 createdAt 和最后一条记录的 createdAt 之间的差异)并将其离散到 bin 中。将每个对象放入适当的箱中。然后使用具有两种窗口大小(30 分钟和 60 分钟)的滑动窗口。当您沿着阵列走时,不断评估您描述的条件是否得到满足。

请注意,对于上述方法,将 bin 宽度正确定义为时间戳处理的分辨率非常重要。由于您没有在帖子中指出这一点,因此如果这是一个问题,请随时发表评论。

于 2015-04-02T18:02:55.660 回答