aralıklar içine tarihlerin listesini / coalescing çöken için Yinelemeli algoritması

oy
1

tarihlerinin listesi göz önüne alındığında

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

Ben yani aralıkları listesini üretmek için (ben FileMaker özel fonksiyonu içine çevirebilir) bir özyinelemeli yalancı kod algoritması doğru işaretçiler, arıyorum

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

Liste ön ayrımı ve tekilleştirilir. İlk değer ve çalışma ileriye ve son değerden hem başlayarak geriye doğru çalışma denedim ama sadece işe almak gibi olamaz. imza gibi bir şey olsaydı o sinir bozucu günlerinden biri ... iyi olurdu

CollapseDateList( dateList, separator, ellipsis )

:-)

Oluştur 18/05/2010 saat 13:58
kaynak kullanıcı
Diğer dillerde...                            


2 cevaplar

oy
1

Ana rutin şöyle görünecektir:

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);

Sana iki tarih ardışık ya değilse size söyler bazı işlevlere sahip varsayıyorum.

Cevap 18/05/2010 saat 15:09
kaynak kullanıcı

oy
1

İşte iş yapar özyinelemeli FileMaker kod. temel yaklaşım bir değer içinde son tarih (sağ çoğu kelime) den tarihini hesaplamak gerektiğinde yerinde değiştirme yapmaktır. bir sonraki değeri hala ilk aralığın bir parçası olup olmadığını kontrol edin veya bitmiş olarak birinci aralığını işaretlemek ve değerlerin geri kalanı odaklanmanızı zaman karar verebilir Bu şekilde. o başkası yardımcı olur.

// 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 )
      ) 
    )
  )
)
Cevap 18/05/2010 saat 16:26
kaynak kullanıcı

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more