Професионална програма
Loading...
+ Нов въпрос
gyurukova avatar gyurukova 1 Точки

Problem 2 - The Lift - JS Fundamentals

Здравейте! Решавам задача The Lift от примерните изпитни задачи за Mid Exam във фундаменталс модула и не мога да разбера защо Judge ми дава 33/100 при положение, че задачата работи с дадените примери.

Бихте ли могли да ми помогнете да си открия грешката? 

https://pastebin.com/AZXrv5B2 

Слагам условието тук:

Write a program that finds a place for the tourist on a lift. 

Every wagon should have a maximum of 4 people on it. If a wagon is full, you should direct the people to the next one with space available.

Input

  • On the first line, you will receive how many people are waiting to get on the lift

  • On the second line, you will receive the current state of the lift separated by a single space: " ".

Output

When there is no more available space left on the lift, or there are no more people in the queue, you should print on the console the final state of the lift's wagons separated by " " and one of the following messages:

  • If there are no more people and the lift have empty spots, you should print:

"The lift has empty spots!

{wagons separated by ' '}"

  • If there are still people in the queue and no more available space, you should print:

"There isn't enough space! {people} people in a queue!

{wagons separated by ' '}"

  • If the lift is full and there are no more people in the queue, you should print only the wagons separated by " "

JS Examples

Input

Output

[

 "15",

 "0 0 0 0 0"

]

The lift has empty spaces!

4 4 4 3 0

Comment

First state - 4 0 0 0 -> 11 people left

Second state – 4 4 0 0 -> 7 people left

Third state – 4 4 4 0 -> 3 people left

Input

Output

[

 "20",

 "0 2 0"

]

There isn't enough space! 10 people in a queue!

4 4 4

Comment

First state - 4 2 0  -> 16 people left

Second state – 4 4 0  -> 14 people left

Third state – 4 4 4 -> 10 people left, but there're no more wagons.

0
JavaScript Fundamentals
Axiomatik avatar Axiomatik 1491 Точки

Instead of using a second array, better to modify the original lift-array. Otherwise, tests will not pass for following input since only part of the lift will be printed out.

solve(['2', '0 0 0 0 0'])

Additional validations had to be included as well, using Array-methods (find, some, every....) in order to solve the remaining tests.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every).

;-)

 

Code: 

function solve(input) {
    let people = Number(input.shift());
    let wagons = input.toString().split(" ").map((x) => Number(x));
    let fullWagon = 0;
    let newWagons = [];

    for (let i = 0; i < wagons.length; i++) {
        let singleWagon = wagons[i];
        let emptySpaces = false;
        let noMorePeople = false;

        if (singleWagon < 4 && people > 0) {
            for (let singleSeats = 0; singleSeats <= 3; singleSeats++) {
                people -= 1;
                singleWagon++;
                wagons[i] = singleWagon;
                // fullWagon = singleWagon;
                // if (people == 0 && singleWagon < 4) {
                if (people == 0 && wagons.some(w => w < 4)) {
                    console.log(`The lift has empty spots!`);
                    emptySpaces = true;
                    break;
                } else if (people == 0) {
                    noMorePeople = true;
                    break;
                }
                if (singleWagon == 4) {
                    break;
                }
            }

            // newWagons.push(fullWagon);
            // wagons[i] = singleWagon;
            if (emptySpaces || noMorePeople) {
                break;
            }
        }
    }
    if (people > 0 && wagons.every(w => w === 4)) {
        console.log(`There isn't enough space! ${people} people in a queue!`);
    }
    // console.log(newWagons.join(" "));
    console.log(wagons.join(" "));
}

 

 

1