user1096685
user1096685

Reputation: 121

Separating an array effectively

I'm having an asbolute nightmare dealing with an array of numbers which has the following structure :

Odd numbers in the array : NumberRepresenting Week
Even numbers in the array : NumberRepresenting Time

So for example in the array :

index : value
0 : 9
1 : 1
2 : 10
3 : 1

Would mean 9 + 10 on Day 1 (Monday).

The problem is, I have a an unpredictable number of these and I need to work out how many "sessions" there are per day. The rules of a session are that if they are on a different day they are automatically different sessions. If they are next to each other like in the example 9 + 10 that would count as a single session. The maximum number than can be directly next to eachother is 3. After this there needs to be a minimum of a 1 session break in between to count as a new session.

Unfortunately, we cannot also assume that the data will be sorted. It will always follow the even / odd pattern BUT could potentially not have sessions stored next to each other logically in the array.

I need to work out how many sessions there are.

My code so far is the following :

for($i = 0; $i < (count($TimesReq)-1); $i++){
    $Done = false;
    if($odd = $i % 2 )
    {
    //ODD WeekComp
        if(($TimesReq[$i] != $TimesReq[$i + 2])&&($TimesReq[$i + 2] != $TimesReq[$i + 4])){
        $WeeksNotSame = true;
        }
    }
    else
        {
        //Even TimeComp

        if(($TimesReq[$i] != ($TimesReq[$i + 2] - 1))&& ($TimesReq[$i + 2] != ($TimesReq[$i + 4] - 1)))
        $TimesNotSame = true; 
        }
    if($TimesNotSame == true && $Done == false){
    $HowMany++; 
    $Done = true;
    }
    if($WeeksNotSame == true && $Done == false){
    $HowMany++;
    $Done = true; 
    }
$TimesNotSame = false;
$WeeksNotSame = false;
    }

However this isn't working perfectly. for example it does not work if you have a single session and then a break and then a double session. It is counting this as one session.

This is, probably as you guessed, a coursework problem, but this is not a question out of a textbook, it is part of a timetabling system I am implementing and is required to get it working. So please don't think i'm just copy and pasting my homework to you guys!

Thank you so much!

New Code being used :

if (count($TimesReq) % 2 !== 0) {
//throw new InvalidArgumentException();
}

for ($i = 0; $i < count($TimesReq); $i += 2) {

    $time = $TimesReq[$i];
    $week = $TimesReq[$i + 1];

    if (!isset($TimesReq[$i - 2])) {
        // First element has to be a new session
        $sessions += 1;
                $StartTime[] = $TimesReq[$i];
                $Days[] = $TimesReq[$i + 1];
        continue;
    }

    $lastTime = $TimesReq[$i - 2];
    $lastWeek = $TimesReq[$i - 1];

    $sameWeek = ($week === $lastWeek);
    $adjacentTime = ($time - $lastTime === 1);
    if (!$sameWeek || ($sameWeek && !$adjacentTime)) {

    if(!$sameWeek){//Time
    $Days[] = $TimesReq[$i + 1];
            $StartTime[] = $TimesReq[$i];
            $looking = true;
    }
    if($sameWeek && !$adjacentTime){
    }
    if($looking && !$adjacentTime){
        $EndTime[] = $TimesReq[$i];
        $looking = false;           
    }
//Week      

        $sessions += 1;
    }
}

Upvotes: 1

Views: 117

Answers (2)

ghbarratt
ghbarratt

Reputation: 11711

Here is my little attempt at solving your problem. Hopefully I understand what you want:

$TimesReq = array(9,4,11,4,13,4,8,4,7,2,12,4,16,4,18,4,20,4,17,4);

// First just create weeks with all times lumped together 
$weeks = array();
for($tri=0; $tri<count($TimesReq); $tri+=2){
  $time = $TimesReq[$tri];
  $week = $TimesReq[$tri+1];

  $match_found = false;
  foreach($weeks as $wi=>&$w){
    if($wi==$week){
      $w[0] = array_merge($w[0], array($time));
      $match_found = true;
      break;
    }
  }
  if(!$match_found) $weeks[$week][] = array($time);
}

// Now order the times in the sessions in the weeks
foreach($weeks as &$w){
  foreach($w as &$s) sort($s);
}

// Now break up sessions by gaps/breaks
$breaking = true;
while($breaking){
  $breaking = false;
  foreach($weeks as &$w){
    foreach($w as &$s){
      foreach($s as $ti=>&$t){
        if($ti>0 && $t!=$s[$ti-1]+1){
          // A break was found
          $new_times = array_splice($s, $ti);
          $s = array_splice($s, 0, $ti);
          $w[] = $new_times;
          $breaking = true;
          break;
        }
      }
    }
  }
}

//print_r($weeks);

foreach($weeks as $wi=>&$w){
  echo 'Week '.$wi.' has '.count($w)." session(s):\n";
  foreach($w as $si=>&$s)
  {
    echo "\tSession ".($si+1).":\n";
    echo "\t\tStart Time: ".$s[0]."\n";
    echo "\t\tEnd Time: ".((int)($s[count($s)-1])+1)."\n";
  }
}

Given $TimesReq = array(9,4,11,4,13,4,8,4,7,2,12,4,16,4,18,4,20,4,17,4); the code will produce as output:

Week 4 has 4 session(s):
    Session 1:
        Start Time: 8
        End Time: 10
    Session 2:
        Start Time: 11
        End Time: 14
    Session 3:
        Start Time: 16
        End Time: 19
    Session 4:
        Start Time: 20
        End Time: 21
Week 2 has 1 session(s):
    Session 1:
        Start Time: 7
        End Time: 8

Hope that helps.

Upvotes: 0

Brenton Alker
Brenton Alker

Reputation: 9072

If you want a single total number of sessions represented in the data, where each session is separated by a space (either a non-contiguous time, or a separate day). I think this function will get you your result:

function countSessions($data)
{
    if (count($data) % 2 !== 0) throw new InvalidArgumentException();

    $sessions = 0;
    for ($i = 0; $i < count($data); $i += 2) {
        $time = $data[$i];
        $week = $data[$i + 1];

        if (!isset($data[$i - 2])) {
            // First element has to be a new session
            $sessions += 1;
            continue;
        }

        $lastTime = $data[$i - 2];
        $lastWeek = $data[$i - 1];

        $sameWeek = ($week === $lastWeek);
        $adjacentTime = ($time - $lastTime === 1);
        if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
            $sessions += 1;
        }
    }

    return $sessions;
}

$totalSessions = countSessions(array(
    9, 1,
    10, 1,
));

This of course assumes the data is sorted. If it is not, you will need to sort it first. Here is an alternate implementation that includes support for unsorted data.

function countSessions($data)
{
    if (count($data) % 2 !== 0) throw new InvalidArgumentException();

    $slots = array();
    foreach ($data as $i => $value) {
        if ($i % 2 === 0) $slots[$i / 2]['time'] = $value;
        else $slots[$i / 2]['week'] = $value;
    }

    usort($slots, function($a, $b) {
        if ($a['week'] == $b['week']) {
            if ($a['time'] == $b['time']) return 0;
            return ($a['time'] < $b['time']) ? -1 : 1;
        } else {
            return ($a['week'] < $b['week']) ? -1 : 1;
        }
    });

    $sessions = 0;
    for ($i = 0; $i < count($slots); $i++) {
        if (!isset($slots[$i - 1])) { // First element has to be a new session
            $sessions += 1;
            continue;
        }

        $sameWeek = ($slots[$i - 1]['week'] === $slots[$i]['week']);
        $adjacentTime = ($slots[$i]['time'] - $slots[$i - 1]['time'] === 1);
        if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
            $sessions += 1;
        }
    }

    return $sessions;
}

Upvotes: 1

Related Questions