Home > other >  Find sorting algorithm for an elevators floor travel schedule, starting from the current floor with
Find sorting algorithm for an elevators floor travel schedule, starting from the current floor with

Time:05-14

I'm making an elevator in react, But I need to make a function that sorts an array to the nearest to the number X and also there is a condition if the elevator goes up or down,

so for example,

  • There are in total 5 floors.
  • X = Current Floor 3

You're now on floor 3 and click on the button UP and you click the numbers 2 -> 5 -> 4 -> 1

The array should be sorted so that it goes like this: 3 -> 4 -> 5 -> 2 -> 1.

Pseudocode:

let currentFloor = 3; 
let direction = "UP";
let clickedButtons = [2,5,4,1];

// ClickedButtons after sorted
 clickedButtons = [4,5,2,1]

CodePudding user response:

I guess you need sth like this

Whenever you go up, you need to find all greater numbers and sort them in ascending order so elevator will stop at the next possible floor as it goes up.

Then find all lower numbers and sort them in reverse order.

The opposite procedure will be done if you choose to go down.

let arr = [10, 4, 8, 1, -2, 6];

let x = 3;

const goUp = (num, arr) => arr.filter(obj => obj > num).sort((a, b) => a - b)
const goDown = (num, arr) => arr.filter(obj => obj < num).sort((a, b) => b - a)

function printSequence(arr, initial, direction) {
  if (direction === 'up') {
    console.log(goUp(initial, arr).concat(goDown(initial, arr)))
  } else {
    console.log(goDown(initial, arr).concat(goUp(initial, arr)))
  }
}

printSequence(arr, 3, 'up')
printSequence(arr, 3, 'down')

CodePudding user response:

You can separate the array into two arrays. One contains only higher numbers than the currentFloor other contains only lower numbers such as:

let lowerFloors = clickedButtons.filter(floor => floor > currnentFloor);
let upperFloors = clickedButtons.filter(floor => floor > currnentFloor);

Then sort each array lowerFloors would be descending, and higherFloors would be in ascending order.

upperFloors = upperFloors.sort((a, b) => {
  return a - b;
});

lowerFloors = lowerFloors.sort((a, b) => {
  return a - b;
});
lowerFloors = lowerFloors.reverse();

Then merge both arrays. If you going up upperFloors array would be first, else lowerFloors would be first.

let newArray;

if (direction === "UP") {
 newArray = upperFloors.concat(lowerFloors);
} else {
 newArray = lowerFloors.concat(upperFloors)
}

CodePudding user response:

From both of my above comments ...

@Cedric ... 1/2 With this scenario the only sorting needed is e.g. an ascending sorting of all floor numbers ... [1, 2, 3, 4, 5] ... one find's the index of the current floor number 3 which is 2 ... 'up' will be translated into direction vector of 1, thus one takes 1st everything higher than index 2 or right from index 2 ... and 2nd everything left from index 2 ... result ... [4, 5, 2, 1].

@Cedric ... 2/2 One does likewise for 'down'/-1 where one 1st takes everything left from index 2 and 2nd everything right from index 2 ... result ... [2, 1, 4, 5]. There is no sorting magic since with the known preset of either 'up' or 'down' the floor travel schedule is obvious.

... but of cause one can cast the imperative recipe of my comments into a single sort formula.

function getFloorTravelSchedule(
  direction = 'up',
  floorNumber = 0,
  floorTravelList = [],
) {
  direction = ((direction === 'up') && 1) || -1;

  function getTravelPrecedence(a, b) {
    return (
      (a > floorNumber && b > floorNumber && a - b) ||
      (a < floorNumber && b < floorNumber && b - a) ||
      ((a > floorNumber && b < floorNumber && -1) || 1) * direction
    );
  }
  return Array
    .from(floorTravelList)
    .sort(getTravelPrecedence);
}
const currentFloorNumber = 3;
const selectedFloorNumbers = [2, 5, 4, 1];

const upwardFloorTravelSchedule =
  getFloorTravelSchedule('up', currentFloorNumber, selectedFloorNumbers);
const downwardFloorTravelSchedule =
  getFloorTravelSchedule('down', currentFloorNumber, selectedFloorNumbers);

console.log({
  currentFloorNumber,
  selectedFloorNumbers,
  upwardFloorTravelSchedule,
  downwardFloorTravelSchedule,
});

console.log(
  "getFloorTravelSchedule('up', 3, [10, 4, 8, 1, -2, 6]) ...",
  getFloorTravelSchedule('up', 3, [10, 4, 8, 1, -2, 6])
);
console.log(
  "getFloorTravelSchedule('down', 3, [10, 4, 8, 1, -2, 6]) ...",
  getFloorTravelSchedule('down', 3, [10, 4, 8, 1, -2, 6])
);

console.log(
  "getFloorTravelSchedule('up', 7, [10, 4, 8, 1, -2, 6]) ...",
  getFloorTravelSchedule('up', 7, [10, 4, 8, 1, -2, 6])
);
console.log(
  "getFloorTravelSchedule('down', 0, [10, 4, 8, 1, -2, 6]) ...",
  getFloorTravelSchedule('down', 0, [10, 4, 8, 1, -2, 6])
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

  • Related