2017-02-17 12 views
4

私は診療所のキューシステムで作業しています。フロントデスクにいる人には、キューに患者を追加する2つのオプションがあります。固定の予定とタイムアレイで最も近い空きスロットを見つける

  1. 患者
  2. ウォークイン患者

だから例えば4人の患者がキュー内に既に存在している、私の既存の予定配列は

existing_appointments = ["09:30", "10:00", "12:15", "13:45"]; 

と平均のように見えます患者の診察時間は15分です。

avg_wait_per_patient = 15; 

患者が歩いてすぐに、私は彼のために最も利用可能なタイムスロットを探しています。

は時間が今、このスロットには何の約束がないので、それは9時の代わりに9時15分を返すため、以下の機能find_free_slot()が動作しない9時

current_time = "09:00"; 

であると言います。

私が目指しているのは、current_time + avg_wait_per_patientの近くに誰もいない場合は、current_timeスロットを与えられるべきです。このスロットが利用できない場合、空きスロットが見つからない限り、配列をループします。失敗した場合は、last_index_of_array + avg_waitの時間に追加する必要があります。

function toMinutes(t) { 
    return 60 * Number(t.split(":")[0]) + Number(t.split(":")[1]); 
} 
function reverse_toMinutes(t) { 
    return ("0" + Math.floor(t/60)).slice(-2) + ":" + ("0" + t % 60).slice(-2); 
} 
function find_free_slot(ct,appointments,avg_wait) { 
    ct = toMinutes(ct); 
    free_slot = ''; 
    if(appointments.length==0) { 
     free_slot = ct; 
    } else { 
     for(i=0; i<appointments.length; i++) { 
      appointment = toMinutes(appointments[i]); 
      if(free_slot <= appointment - avg_wait) { 
       i == 0 ? 
        free_slot = ct + avg_wait : 
        free_slot = toMinutes(appointments[i - 1]) + avg_wait; 
       break; 
      } 
     } 
    } 
    return reverse_toMinutes(free_slot); 
} 

jsfiddle

+0

私はCURRENT_TIMEとexisting_appointmentsに異なる値を入力しようとしています。たとえば、現在の時刻を9:50の 'current_time =" 09:50 ";'に変更した場合、出力は10:05になりました。さらに、existing_appointmentsの最初の値を9:10に変更しました。結果の出力は9:15でした。 – Inkdot

答えて

4

問題は、次のとおりです。

i == 0 ? 
    free_slot = ct + avg_wait : 
    free_slot = toMinutes(appointments[i - 1]) + avg_wait; 

あなたが最初の予定(午前9時30分)、および空きスロット<= (9:30 - 15)をチェックしている場合、あなたは、ct + avg_waitを返しました9:00 + 15です。

私はそれを動作させるためのロジックを少し再働いてきた:次のスロットプラス平均時間は、その後の予定よりも小さい場合

function toMinutes(t) { 
 
    return 60 * Number(t.split(":")[0]) + Number(t.split(":")[1]); 
 
} 
 

 
function reverse_toMinutes(t) { 
 
    return ("0" + Math.floor(t/60)).slice(-2) + ":" + ("0" + t % 60).slice(-2); 
 
} 
 

 
function find_free_slot(ct, appointments, avg_wait) { 
 
    ct = toMinutes(ct); 
 
    free_slot = ct; // The first slot you want to check is "right now" 
 

 
    if (appointments.length == 0) 
 
    return reverse_toMinutes(ct); 
 

 
    for (i = 0; i < appointments.length; i++) { 
 
    appointment = toMinutes(appointments[i]); 
 
    if (ct <= appointment + avg_wait) {  // The appointment must be later than the current appointment's end time. 
 
     if (free_slot <= appointment - avg_wait) // Free slot is enough time before the current appointment 
 
     return reverse_toMinutes(free_slot); // Return the free slot 
 

 
     free_slot = toMinutes(appointments[i]) + avg_wait; // Otherwise, set the free slot to `avg` after the current appointment, to check the next iteration of the loop. 
 
    } 
 
    } 
 
    return reverse_toMinutes(free_slot); // No free slot has been found, `free_slot` is `last appointment + avg` 
 
} 
 

 
var appointments = ["09:30", "10:00", "12:15", "13:45"]; 
 

 
console.log(" time - appointment"); 
 
console.log(" 9:00 -", find_free_slot("9:00", appointments, 15)); 
 
console.log(" 9:15 -", find_free_slot("9:15", appointments, 15)); 
 
console.log(" 9:16 -", find_free_slot("9:16", appointments, 15)); 
 
console.log(" 9:31 -", find_free_slot("9:31", appointments, 15)); 
 
console.log("10:09 -", find_free_slot("10:09", appointments, 15)); 
 
console.log("11:59 -", find_free_slot("11:59", appointments, 15)); 
 
console.log("12:00 -", find_free_slot("12:00", appointments, 15)); 
 
console.log("12:01 -", find_free_slot("12:01", appointments, 15));

+0

あなたの答えをありがとう。論理にまだ何らかのエラーがあるように見えます。 'current_time =" 09:31 "と答えるべきです09:46" – AZee

+0

@AZee:なぜそれが '09:46'を返しますか?それは最寄りの予定の16分後です。予約が平均15分間であるため、最も近い利用可能なスロットは09:30予約の15分後です。 (9時30分の予約は平均9時45分に終わり、なぜ次の患者を予約するのに時間がかかるのですか?) – Cerbrus

+0

来る予定の空きスロットを意味する配列の「9時30分」に既に予約があります最初の人が15分かかると9時45分に2人目のターンが来るので、人は9:45です。 – AZee

0

は、あなたがチェックできます。

trueの場合は、時間プラス平均時間が次の開いているスロットより大きいか等しいかどうかを確認してから、スロットプラス平均時間を取ってください。

function getMinutes(t) { 
 
    var p = t.split(':'); 
 
    return 60 * p[0] + +p[1]; 
 
} 
 

 
function find_free_slot(current_time) { 
 
    var next_time = getMinutes(current_time); 
 

 
    existing_appointments.some(function (a) { 
 
     var minutes = getMinutes(a); 
 
     if (next_time + avg_wait_per_patient <= minutes) { 
 
      return true; 
 
     } 
 
     next_time = minutes + avg_wait_per_patient >= next_time ? minutes + avg_wait_per_patient : next_time; 
 
    }); 
 
    return ('0' + Math.floor(next_time/60)).slice(-2) + ':' + ('0' + next_time % 60).slice(-2); 
 
} 
 

 
var existing_appointments = ["09:30", "10:00", "12:15", "13:45"], 
 
    avg_wait_per_patient = 15; 
 

 
console.log(find_free_slot('09:00')); // 09:00 
 
console.log(find_free_slot('09:15')); // 09:15 
 
console.log(find_free_slot('09:16')); // 09:45 
 
console.log(find_free_slot('09:45')); // 09:45 
 
console.log(find_free_slot('09:46')); // 10:15 
 
console.log(find_free_slot('10:00')); // 10:15 
 
console.log(find_free_slot('10:09')); // 10:15 
 
console.log(find_free_slot('11:59')); // 11:59 
 
console.log(find_free_slot('12:00')); // 12:00 
 
console.log(find_free_slot('12:01')); // 12:30

-1

私はいくつかの編集をした、今それは大丈夫だ、多分あなたもチェックするclose_timeとのstart_timeが必要になります。

function toMinutes(t) { 
 
\t return 60 * Number(t.split(":")[0]) + Number(t.split(":")[1]); 
 
} 
 
function reverse_toMinutes(t) { 
 
\t return ("0" + Math.floor(t/60)).slice(-2) + ":" + ("0" + t % 60).slice(-2); 
 
} 
 

 
var existing_appointments = ["09:30", "10:00", "12:55", "13:45"]; 
 
var avg_wait_per_patient = 15; // in minutes 
 
var current_time = "10:00"; 
 

 
function find_free_slot(ct,appointments,avg_wait) { 
 
\t ct = toMinutes(ct); 
 
\t var free_slot = ''; 
 
    
 
\t if(appointments.length==0) { 
 
\t \t free_slot = ct; 
 
\t } 
 
    else if (appointments.length==1) { 
 
    \t free_slot = toMinutes(appointments[0])+avg_wait; 
 
\t } 
 
    else { 
 
    var i=0; 
 
    \t for (i=1;i<appointments.length;i++){ 
 
    \t if (toMinutes(appointments[i])-toMinutes(appointments[i-1])>=2*avg_wait){ 
 
     \t free_slot=toMinutes(appointments[i-1])+avg_wait; 
 
      break; 
 
     }  
 
     } 
 
\t } 
 
    
 
    if (free_slot=='') free_slot=toMinutes(appointments[appointments.length-1])+avg_wait; 
 

 
\t return reverse_toMinutes(free_slot); 
 
} 
 

 
document.write(find_free_slot(current_time,existing_appointments,avg_wait_per_patient));

-1

私はあなたが計算&フォーマットのこの種を行うにmomentjsライブラリを使用することをお勧めします。ここでは、あなたが瞬間を使って欲しいものをどのように実現するかのサンプルを示します。

function find_free_slot(ct,appointments,avg_wait) { 
 
    existing_appointments.sort((a,b) => a-b); 
 
    let possibleSlot = ct; 
 
    for(let i=0; i<appointments.length; i++) { 
 
    const a = appointments[i]; 
 
    if(a >= +possibleSlot + (avg_wait * 60000)) { 
 
     appointments.splice(i, 0, possibleSlot); 
 
     return possibleSlot; 
 
    } 
 
    const endOfCurrentSlot = a.clone().add(avg_wait,'minute'); 
 
    if(endOfCurrentSlot > possibleSlot) { 
 
     possibleSlot = endOfCurrentSlot; 
 
    } 
 
    } 
 
    appointments.push(possibleSlot); 
 
    return possibleSlot; 
 
    
 
} 
 

 
const existing_appointments = [ 
 
    moment('09:30','HH:mm'), 
 
    moment('10:00','HH:mm'), 
 
    moment('13:45','HH:mm') 
 
]; 
 
const avg_wait_per_patient = 15; // in minutes 
 

 
function getSlotStr(time) { 
 
    var slot = find_free_slot(time, existing_appointments, avg_wait_per_patient); 
 
    return time.format('HH:mm') + ' --> ' + slot.format('HH:mm'); 
 
} 
 

 
document.write(getSlotStr(moment('09:00','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('09:10','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('09:20','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('09:30','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('09:40','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('10:10','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('13:00','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('13:40','HH:mm')) + '<br />'); 
 
document.write(getSlotStr(moment('13:50','HH:mm')) + '<br />');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

関連する問題