首页 > 解决方案 > 我的查询助手方法中是否缺少基本案例?

问题描述

我有一个类 ParkingLot,我需要编写一个名为的方法,enter()该方法将检查停车场是否有空位。如果是这样,我输入车牌号并将空置计数减 1。

到目前为止,一切正常,除了停车场满员时,我需要将其调整vacantSpaces为 0。

到目前为止我所拥有的:

class ParkingLot {
  constructor(capacity, rate) {
    this.spaces = new Array(capacity).fill('vacant');
    this.rate = rate;
    this.revenue = 0;
    this.queue = new Queue();
  }

  /**
   * Returns the number of vacant parking spaces
   * @returns {Number}
   *  the total number of spaces where the value is "vacant".
   */

  get vacantSpaces() {
    return this.spaces.reduce(
      (sum, space, index) => sum + (space === 'vacant' ? 1 : 0),
      0
    );
  }

  /**
   * As cars enter the parking lot, the license plate number is entered and the car is parked in the first vacant space.
   * If the lot is full, the car is added to the queue to be parked when a spot is available.
   *
   * @param licensePlateNumber
   *  the license plate number of the car entering
   */

  enter(licensePlateNumber) {
    const lot = new ParkingLot(licensePlateNumber);

    if (this.spaces.length > 1) {
      lot.enter(licensePlateNumber);
      this.spaces.shift();
    }
    if (this.spaces.length < 1) {
      lot.vacantSpaces = 0;
    }
  }
}

这是我的测试用例:

test("is zero when lot is full", () => {
      const parkingLot = new ParkingLot(2, 1);

      parkingLot.enter("460-QRJ");
      parkingLot.enter("127-HLN");

      expect(parkingLot.vacantSpaces).toEqual(0);
    });

我不断得到:

Error: expect(received).toEqual(expected) // deep equality

Expected: 0
Received: 1

我错过了一个边缘案例还是我当前的逻辑设置不正确?

标签: javascriptalgorithmdata-structures

解决方案


您的实施中的一些问题enter

  • 不要创建新的停车场。您只需要使用当前的 parkLot 实例 ( this)。
  • 不要调用阵列(想象shift一下this.spaces所有那些会移动到邻近地点的汽车!)。而是找到空闲位置(使用indexOf)并在那里写下车牌号。
  • 您不能this.vacantSpaces作为属性进行更新。它是一个吸气剂,而不是一个数字。无需更新任何内容,因为此 getter 将在调用时动态计算有多少空置空间。

这是更正的版本。我还添加了一个leave方法,并扩展了测试,因此队列也被使用:

class ParkingLot {
  constructor(capacity, rate) {
    this.spaces = new Array(capacity).fill('vacant');
    this.rate = rate;
    this.revenue = 0;
    // Unless you have a very efficient Queue implementation, use a native 
    // Array, which already provides Queue operations
    this.queue = []; 
  }

  /**
   * Returns the number of vacant parking spaces
   * @returns {Number}
   *  the total number of spaces where the value is "vacant".
   */

  get vacantSpaces() {
    return this.spaces.reduce(
      (sum, space, index) => sum + (space === 'vacant' ? 1 : 0),
      0
    );
  }

  /**
   * As cars enter the parking lot, the license plate number is entered and the car is parked in the first vacant space.
   * If the lot is full, the car is added to the queue to be parked when a spot is available.
   *
   * @param licensePlateNumber
   *  the license plate number of the car entering
   */

  enter(licensePlateNumber) {
    // Don't create a new parkingLot. You want to use the current parkingLot instance (this)
    let space = this.spaces.indexOf('vacant');
    if (space >= 0) { // found a free spot
      this.spaces[space] = licensePlateNumber; // Overwrite 'vacant' with license place number
      // Don't shift the spaces array
    } else { // No free space
      this.queue.push(licensePlateNumber); // add to end of the queue
      // Don't write a value to this.vacantSpaces -- it already dynamically provides an updated value
    }
  }

  leave(licensePlateNumber) {
    let space = this.spaces.indexOf(licensePlateNumber);
    if (space >= 0) { // found its spot
      this.spaces[space] = 'vacant'; // Overwrite license place number with 'vacant'
      if (this.queue.length) { // There is a car waiting for a space
        this.enter(this.queue.shift()); // Attribute the first car in the queue to the new free space
      }
    }
  }
}

const parkingLot = new ParkingLot(2, 1);

parkingLot.enter("460-QRJ");
parkingLot.enter("127-HLN");

console.log("vacant spaces: ", parkingLot.vacantSpaces);

parkingLot.enter("300-BEL");

console.log("vacant spaces: ", parkingLot.vacantSpaces);

parkingLot.leave("460-QRJ");

console.log("vacant spaces: ", parkingLot.vacantSpaces);

parkingLot.leave("127-HLN");

console.log("vacant spaces: ", parkingLot.vacantSpaces);


推荐阅读