The length of the array is increased to units + 1 to reconcile the idea of each element of the array scaling from the beginning of that index that index to the beginning of the next, IE, 0-1, 1-2, ..., 100-0
It doesn't matter if temp.isgap starts as true or false, and it's possible to see a sort of representation of how the cars align by returning lot instead of this.parkedcars.
Code:
PHP Code:
function onCreated(){
this.gapmax = 1;
this.gapmin = 0;
temp.val1 = findMin(100, 2);
temp.val2 = findMax(100, 2);
echo(val1);
echo(val2);
for(temp.i = 0; i < 100; i++){
temp.val3 += findRand(100, 2);
}
echo(val3/100);
}
function findMin(units, carlength){
this.parkedcars = 0;
temp.isgap = false;
for(temp.i = 0; i < units+1; i++){
temp.lot.add("E");
}
for(temp.i = 0; i < lot.size(); i++){
temp.isgap = !temp.isgap;
if(!temp.isgap){
lot = parkCar(lot, i);
i++;
}
}
return this.parkedcars;
}
function findRand(units, carlength){
temp.i = int(random(0,2));
if(i == 0){
return findMin(units, carlength);
}
else{
return findMax(units, carlength);
}
}
function parkCar(arr, i){
if(i + 1 == arr.size()){
temp.val = 0;
}
else{
temp.val = i + 1;
}
if(arr[i] == "E" && arr[val] == "E"){
arr[i] = "C";
arr[val] = "C";
this.parkedcars++;
}
return arr;
}
function findMax(units, carlength){
return int((units+1)/carlength);
}
Output: