D
D
dc65k2021-07-19 16:47:25
JavaScript
dc65k, 2021-07-19 16:47:25

How to solve the ticket sorting problem?

Hello. Wrote a solution to the problem of sorting tickets (sorting routes).
Could you tell me how to solve it more correctly (optimally)?

My decision:

const arr = [
    {from: 'С.Петербург', to: 'Минск'},
    {from: 'Киев', to: 'Новосибирск'},
    {from: 'Череповец', to: 'Москва'},
    {from: 'Минск', to: 'Киев'},
    {from: 'Москва', to: 'С.Петербург'},
]

/*
output:
[
    {from: 'Череповец', to: 'Москва'},
    {from: 'Москва', to: 'С.Петербург'},
    {from: 'С.Петербург', to: 'Минск'},
    {from: 'Минск', to: 'Киев'},
    {from: 'Киев', to: 'Новосибирск'},
]

 */

const getRoutes = (arr) => {

    const fromArray = arr.map(currentValue => {
        return currentValue.from;
    });

    const toArray = arr.map(currentValue => currentValue.to);

    const obj = fromArray.concat(toArray).reduce((accumulator, currentValue) => {
        if (!accumulator[currentValue]) {
            accumulator[currentValue] = [];
        }

        accumulator[currentValue].push(currentValue);

        return accumulator;
    }, {});

    const a = Object.values(obj).filter(el => {
        return el.length < 2;
    }).flat();

    let firstRoute = arr.find(el => {
        return a.includes(el.from);
    });

    let output = [firstRoute];

    let idx = 0;

    arr = arr.filter(el => {
        return el.from !== firstRoute.from && el.to !== firstRoute.to;
    });

    while (arr.length) {

        const currentValue = arr[idx];

        if (currentValue?.from === firstRoute.to) {

            output.push(currentValue);

            firstRoute = currentValue;

            idx = 0;

            arr = arr.filter(el => {
                return el.from !== firstRoute.from && el.to !== firstRoute.to;
            });

        } else {
            idx++;
        }


    }

    return output;
}

console.log(getRoutes(arr));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2021-07-19
@dc65k

function sort(arr) {
  const objFrom = Object.fromEntries(arr.map(n => [ n.from, n ]));
  const objTo = Object.fromEntries(arr.map(n => [ n.to, n ]));
  const sorted = [];

  for (let route = arr.find(n => !objTo[n.from]); route; route = objFrom[route.to]) {
    sorted.push(route);
  }

  return sorted;
}

A
Anton Neverov, 2021-07-19
@TTATPuOT

I would decide so.

const array = [
  { from: "С.Петербург", to: "Минск" },
  { from: "Киев", to: "Новосибирск" },
  { from: "Череповец", to: "Москва" },
  { from: "Минск", to: "Киев" },
  { from: "Москва", to: "С.Петербург" }
];
const cities = {};
const result = [];

const getNextDestanation = (destatnation, array) => {
  for (const i of array) {
    if (i.from === destatnation) return i;
  }
}
const checkCity = (city) => {
  if (cities[city]) cities[city]++;
  else cities[city] = 1;
}

array.map(i => {
  checkCity(i.from);
  checkCity(i.to);
});
const firstCandidates = Object.keys(cities).filter(c => cities[c] === 1);
const firstElement = array.find(i => firstCandidates.includes(i.from));
result.push(firstElement);

for (let i = 1; i < array.length; i++) {
  result.push(getNextDestanation(result[i - 1].to, array));
}

//result будет результатом

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question