鉴于日期的列表

12/07/2010
13/07/2010
14/07/2010
15/07/2010
12/08/2010
13/08/2010
14/08/2010
15/08/2010
19/08/2010
20/08/2010
21/08/2010

我想找朝向递归算法的伪代码的指针(我可以转化为一个的FileMaker自定义函数)用于产生范围的列表,即

12/07/2010 to 15/07/2010, 12/08/2010 to 15/08/2010, 19/08/2010 to 20/08/2010

列表被预排序和去重复。我试着从第一个值都和前锋的工作,最后价值和工作向后开始,但我似乎无法得到它的工作。有了这些令人沮丧的一天......这将是很好,如果签名是类似

CollapseDateList( dateList, separator, ellipsis )

: - )

有帮助吗?

解决方案

在主程序会是这个样子:

List<String> list  = new ArrayList<String>();

String firstDate   = dateList[0];
String lastDate    = dateList[0];
String currentDate = dateList[0];

for (int i = 1; i < dateList.length(); i++) {
    if (dateDiff(dateList[i], currentDate) == 1) {
        lastDate   = dateList[i];
    } else {
        list.add(firstDate + separator + lastDate);
        firstDate = dateList[i];
        lastDate  = dateList[i];
    }
    currentDate = dateList[i];
}
list.add(firstDate + separator + lastDate);

我假设你有一些功能,告诉你,如果两个日期是连续的或没有。

其他提示

下面是递归的FileMaker代码,没有工作。其基本做法是做到位更换,如有必要计算从一个值内的最后日期(最右边的字)的日期。这样,它可以决定何时检查下一个值仍然是第一系列的一部分,或标志着第范围为已完成并专注于价值的其余部分。希望它可以帮助别人。

// CollapseDateList( dates, comma, dash)

Let(
  countDates = ValueCount ( dates );

  If (
    countDates < 2 ; dates;  // return the dates we've been given...

    Let(
      [ 
        start_date = GetAsDate( LeftWords( GetValue ( dates ; 1 ); 1 ) );
        date_1 = GetAsDate( RightWords( GetValue ( dates ; 1 ); 1 ) );
        date_2 = GetAsDate( GetValue ( dates ; 2 ) );
        date_3 = GetAsDate( GetValue ( dates ; 3 ) );
        dv_1 = GetAsNumber( date_1 );
        dv_2 = GetAsNumber( date_2 );
        dv_3 = GetAsNumber( date_3 );
        twoFollowsOne = (dv_2 = dv_1 + 1);
        threeFollowsTwo = (dv_3 = dv_2 + 1)
      ];

       // compare dates
      Case(
        // only two dates in list
        countDates = 2;
          if (
            twoFollowsOne;
            start_date & dash & date_2;
            GetValue ( dates ; 1 ) & comma & date_2
          );

        // first three values are sequential
        threeFollowsTwo and twoFollowsOne; 
          CollapseDateList( start_date & dash & date_3 & ¶ & RightValues( dates; countDates - 3 ); comma; dash );

        // first two values are sequential only
        not threeFollowsTwo and twoFollowsOne; 
          start_date & dash & date_2 & comma & CollapseDateList(  RightValues(  dates; countDates - 2 ); comma; dash );

        // first two values are not sequential 
        // default
        GetValue ( dates ; 1 ) & comma & CollapseDateList( RightValues( dates; countDates - 1 ); comma; dash )
      ) 
    )
  )
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top