将工作日列表转换为间隔

发布于 2024-11-07 09:21:12 字数 678 浏览 6 评论 0原文

我正在寻找一个函数,如果连续 3 天或更多天连续,则该函数可以从一系列工作日中删除一周中的几天。这是我的测试代码(使用 Test::More 测试框架)

// function is($result, $expected, $message)
is(drop_days(""), "", 'Empty');
is(drop_days("Mo"), "Mo", 'One Day');
is(drop_days("Mo,Tu"), "Mo,Tu", 'Two Days');
is(drop_days("Mo,Tu,We"), "Mo-We", 'Three Days');
is(drop_days("Mo,Tu,We,Th,Fr,Sa,Su"), "Mo-Su", 'Seven Days');
is(drop_days("Mo,Tu,Th,Fr"), "Mo,Tu,Th,Fr", 'Four days with gap');
is(drop_days("Mo,Tu,We,Fr"), "Mo-We,Fr", '3 consecutive days, one single day');
is(drop_days("Mo,Tu,We,Fr,Sa,Su"), "Mo-We,Fr-Su", '2 pairs of 3 consecutive days');

I'm looking for a function that drops days of the week from a sequence of weekdays if 3 or more consecutive days follow each other. This is my test code (uses Test::More test framework)

// function is($result, $expected, $message)
is(drop_days(""), "", 'Empty');
is(drop_days("Mo"), "Mo", 'One Day');
is(drop_days("Mo,Tu"), "Mo,Tu", 'Two Days');
is(drop_days("Mo,Tu,We"), "Mo-We", 'Three Days');
is(drop_days("Mo,Tu,We,Th,Fr,Sa,Su"), "Mo-Su", 'Seven Days');
is(drop_days("Mo,Tu,Th,Fr"), "Mo,Tu,Th,Fr", 'Four days with gap');
is(drop_days("Mo,Tu,We,Fr"), "Mo-We,Fr", '3 consecutive days, one single day');
is(drop_days("Mo,Tu,We,Fr,Sa,Su"), "Mo-We,Fr-Su", '2 pairs of 3 consecutive days');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九局 2024-11-14 09:21:12

有时“愚蠢”的做法是最聪明的

function drop_days($s) {
    static $map  = array(
        'Mo,Tu,We,Th,Fr,Sa,Su' => 'Mo-Su',
        'Mo,Tu,We,Th,Fr,Sa'    => 'Mo-Sa',
        'Mo,Tu,We,Th,Fr'       => 'Mo-Fr',
        'Mo,Tu,We,Th'          => 'Mo-Th',
        'Mo,Tu,We'             => 'Mo-We',
        'Tu,We,Th,Fr,Sa,Su'    => 'Tu-Su',
        'Tu,We,Th,Fr,Sa'       => 'Tu-Sa',
        'Tu,We,Th,Fr'          => 'Tu-Fr',
        'Tu,We,Th'             => 'Tu-Th',
        'We,Th,Fr,Sa,Su'       => 'We-Su',
        'We,Th,Fr,Sa'          => 'We-Sa',
        'We,Th,Fr'             => 'We-Fr',
        'Th,Fr,Sa,Su'          => 'Th-Su',
        'Th,Fr,Sa'             => 'Th-Sa',
        'Fr,Sa,Su'             => 'Fr-Su',
    );
    return strtr($s, $map);
}

sometimes the "dumb" approach is the smartest

function drop_days($s) {
    static $map  = array(
        'Mo,Tu,We,Th,Fr,Sa,Su' => 'Mo-Su',
        'Mo,Tu,We,Th,Fr,Sa'    => 'Mo-Sa',
        'Mo,Tu,We,Th,Fr'       => 'Mo-Fr',
        'Mo,Tu,We,Th'          => 'Mo-Th',
        'Mo,Tu,We'             => 'Mo-We',
        'Tu,We,Th,Fr,Sa,Su'    => 'Tu-Su',
        'Tu,We,Th,Fr,Sa'       => 'Tu-Sa',
        'Tu,We,Th,Fr'          => 'Tu-Fr',
        'Tu,We,Th'             => 'Tu-Th',
        'We,Th,Fr,Sa,Su'       => 'We-Su',
        'We,Th,Fr,Sa'          => 'We-Sa',
        'We,Th,Fr'             => 'We-Fr',
        'Th,Fr,Sa,Su'          => 'Th-Su',
        'Th,Fr,Sa'             => 'Th-Sa',
        'Fr,Sa,Su'             => 'Fr-Su',
    );
    return strtr($s, $map);
}
情定在深秋 2024-11-14 09:21:12

另一种方法:

function drop_days( $str, $days = 'mo,tu,we,th,fr,sa,su' ) {
    $days_i_s = array_flip( explode( ',', $days ) );
    $days_i_n = explode( ',', $days );
    $day_string = str_replace( array_keys( $days_i_s ), array_values( $days_i_s ), strtolower( $str ) );
    return str_replace( array_values( $days_i_s ), array_keys( $days_i_s ), preg_replace_callback( '~(0,1,2,3,4,5,6)|(1,2,3,4,5,6)|(0,1,2,3,4,5)|(1,2,3,4,5)|(2,3,4,5,6)|(0,1,2,3,4)|(0,1,2,3)|(0,1,2)|(3,4,5,6)|(2,3,4,5)|(2,3,4)|(3,4,5)|(1,2,3,4)|(1,2,3)|(2,3,4)|(4,5,6)~', function($m) use($days_i_n){return $days_i_n[$m[0][0]].'-'.$days_i_n[$m[0][strlen($m[0])-1]] ;}, $day_string ) );
}

echo drop_days( "Mo,Tu,We,Fr,Sa,Su" );

Another approach:

function drop_days( $str, $days = 'mo,tu,we,th,fr,sa,su' ) {
    $days_i_s = array_flip( explode( ',', $days ) );
    $days_i_n = explode( ',', $days );
    $day_string = str_replace( array_keys( $days_i_s ), array_values( $days_i_s ), strtolower( $str ) );
    return str_replace( array_values( $days_i_s ), array_keys( $days_i_s ), preg_replace_callback( '~(0,1,2,3,4,5,6)|(1,2,3,4,5,6)|(0,1,2,3,4,5)|(1,2,3,4,5)|(2,3,4,5,6)|(0,1,2,3,4)|(0,1,2,3)|(0,1,2)|(3,4,5,6)|(2,3,4,5)|(2,3,4)|(3,4,5)|(1,2,3,4)|(1,2,3)|(2,3,4)|(4,5,6)~', function($m) use($days_i_n){return $days_i_n[$m[0][0]].'-'.$days_i_n[$m[0][strlen($m[0])-1]] ;}, $day_string ) );
}

echo drop_days( "Mo,Tu,We,Fr,Sa,Su" );
み零 2024-11-14 09:21:12

http://codepad.org/ulJnW3Iu 处的 is() 示例。函数可以接受任意顺序的工作日。

<?php
function drop_days($string, $atleast = 3) {
    $weekdays = 'Mo,Tu,We,Th,Fr,Sa,Su';
    $weekdays = explode(',', $weekdays);
    $selected = array();
    $sequences = array();
    foreach ($weekdays as $weekday)
        $selected[] = (strpos($string,$weekday)!==false);
    $i = 0;
    while ($i<count($weekdays)) {
        if (!$selected[$i]) {
            $i++;
            continue;
        }
        if ($i+$atleast<=count($weekdays)) {
            for ($j=$i+1; $j<count($weekdays); $j++) {
                if ($selected[$j]) {
                    if ($j+1<count($weekdays)) continue;
                    $sequences[] = $weekdays[$i] . '-' . $weekdays[$j];
                    break 2;
                } else {
                    if ($j-$i>=$atleast)
                        $sequences[] = $weekdays[$i] . '-' . $weekdays[$j-1];
                    else
                        for ($k=$i; $k<$j; $k++) $sequences[] = $weekdays[$k];
                    $i = $j+1;
                    break;
                }
            }
        } else {
            $sequences[] = $weekdays[$i];
            $i++;
        }
    }
    return implode(',', $sequences);
}
?>

Example with is() at http://codepad.org/ulJnW3Iu. Function can accept weekdays in any order.

<?php
function drop_days($string, $atleast = 3) {
    $weekdays = 'Mo,Tu,We,Th,Fr,Sa,Su';
    $weekdays = explode(',', $weekdays);
    $selected = array();
    $sequences = array();
    foreach ($weekdays as $weekday)
        $selected[] = (strpos($string,$weekday)!==false);
    $i = 0;
    while ($i<count($weekdays)) {
        if (!$selected[$i]) {
            $i++;
            continue;
        }
        if ($i+$atleast<=count($weekdays)) {
            for ($j=$i+1; $j<count($weekdays); $j++) {
                if ($selected[$j]) {
                    if ($j+1<count($weekdays)) continue;
                    $sequences[] = $weekdays[$i] . '-' . $weekdays[$j];
                    break 2;
                } else {
                    if ($j-$i>=$atleast)
                        $sequences[] = $weekdays[$i] . '-' . $weekdays[$j-1];
                    else
                        for ($k=$i; $k<$j; $k++) $sequences[] = $weekdays[$k];
                    $i = $j+1;
                    break;
                }
            }
        } else {
            $sequences[] = $weekdays[$i];
            $i++;
        }
    }
    return implode(',', $sequences);
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文