Categorize Box According to Criteria

🏠 ⬅️ ➑️

Given four integers length, width, height, and mass, representing the dimensions and mass of a box, respectively, return a string representing the category of the box.

  • The box is "Bulky " if:
    • Any of the dimensions of the box is greater or equal to 104.
    • Or, the volume of the box is greater or equal to 109.
  • If the mass of the box is greater or equal to 100, it is "Heavy ".
  • If the box is both "Bulky " and "Heavy ", then its category is "Both ".
  • If the box is neither "Bulky " nor "Heavy ", then its category is "Neither ".
  • If the box is "Bulky " but not "Heavy ", then its category is "Bulky ".
  • If the box is "Heavy " but not "Bulky ", then its category is "Heavy ".

Note that the volume of the box is the product of its length, width and height.

Example 1:

Input: length = 1000, width = 35, height = 700, mass = 300 Output: "Heavy " Explanation: None of the dimensions of the box is greater or equal to 104. Its volume = 24500000 <= 109. So it cannot be categorized as "Bulky ". However mass >= 100, so the box is "Heavy ". Since the box is not "Bulky " but "Heavy ", we return "Heavy ".

Example 2:

Input: length = 200, width = 50, height = 800, mass = 50 Output: "Neither " Explanation: None of the dimensions of the box is greater or equal to 104. Its volume = 8 * 106 <= 109. So it cannot be categorized as "Bulky ". Its mass is also less than 100, so it cannot be categorized as "Heavy " either. Since its neither of the two above categories, we return "Neither ".

Constraints:

  • 1 <= length, width, height <= 105
  • 1 <= mass <= 103

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main

! This program solves the following problem:
!
! Given four integers length, width, height, and mass, representing the dimensions and mass of a box, respectively,
! return a string representing the category of the box.
!
! The box is "Bulky " if:
!     Any of the dimensions of the box is greater or equal to 104.
!     Or, the volume of the box is greater or equal to 109.
! If the mass of the box is greater or equal to 100, it is "Heavy ".
! If the box is both "Bulky " and "Heavy ", then its category is "Both ".
! If the box is neither "Bulky " nor "Heavy ", then its category is "Neither ".
! If the box is "Bulky " but not "Heavy ", then its category is "Bulky ".
! If the box is "Heavy " but not "Bulky ", then its category is "Heavy ".
!
! Note that the volume of the box is the product of its length, width and height.
!
! Examples:
!
! Input: length = 1000, width = 35, height = 700, mass = 300
! Output: "Heavy "
!
! Input: length = 200, width = 50, height = 800, mass = 50
! Output: "Neither "
!
! Constraints:
!     1 <= length, width, height <= 105
!     1 <= mass <= 103

implicit none

integer :: length, width, height, mass
character(len=10) :: category

print '("Enter length, width, height, and mass: ")', &
        '(A, I0, A, I0, A, I0, A, I0)', &
        'length = ', length, ', ', &
        'width = ', width, ', ', &
        'height = ', height, ', ', &
        'mass = ', mass

call get_category(length, width, height, mass, category)

print '("The category of the box is: ", A)', category

contains

subroutine get_category(length, width, height, mass, category)
    implicit none
    integer, intent(in) :: length, width, height, mass
    character(len=10), intent(out) :: category

    ! Bulky boxes have any dimension greater or equal to 104 or volume greater or equal to 109
    if (any(shape=
❌ Compiled
❌ Executed
❌ Correct
! This is a comment

PROGRAM box_category

IMPLICIT NONE

INTEGER, PARAMETER :: length = 1000, width = 35, height = 700, mass = 300

CHARACTER(len=10) :: category

! Calculate the volume of the box
INTEGER :: volume
volume = length * width * height

! Check if the box is bulky
IF (ANY(length >= 104, width >= 104, height >= 104)) THEN
    category = "Bulky "
ELSE IF (volume >= 109) THEN
    category = "Bulky "
END IF

! Check if the box is heavy
IF (mass >= 100) THEN
    category = "Heavy "
END IF

! Check if the box is both bulky and heavy
IF (category == "Bulky " .AND. mass >= 100) THEN
    category = "Both "
ELSE IF (category == "Heavy " .AND. mass >= 100) THEN
    category = "Both "
END IF

! Check if the box is neither bulky nor heavy
IF (category /= "Bulky " .AND. category /= "Heavy ") THEN
    category = "Neither "
END IF

! Check if the box is bulky but not heavy
IF (category == "Bulky " .AND. category /= "Heavy ") THEN
    category = "Bulky "
END IF

! Check if the box is heavy but not bulky
IF (category == "Heavy " .AND. category /= "Bulky ") THEN
    category = "Heavy "
END IF

! Print the category of the box
WRITE (*,*) category

END PROGRAM box_category
🌐 Data from online sources
def boxCategory(length: int, width: int, height: int, mass: int) -> str:
    bulky = length >= 10000 or width >= 10000 or height >= 10000 or int(length) * width * height >= 1000000000
    heavy = mass >= 100

    if bulky and heavy:
        return "Both "
    if bulky:
        return "Bulky "
    if heavy:
        return "Heavy "
    return "Neither "

The algorithm first checks if the box is bulky by verifying if any of the dimensions equal or exceed 10^4, or if the volume of the box is equal to or greater than 10^9. If the mass of the box is greater or equal to 100, then it's considered heavy.

We then use conditional statements to determine the category of the box according to the values of the bulky and heavy variables.

If both the bulky and heavy conditions are true, we return "Both ". If bulky is true and heavy is false, we return "Bulky ". If heavy is true and bulky is false, we return "Heavy ". Otherwise, if neither condition is true, we return "Neither ".

🌐 Data from online sources
#include <string>

std::string boxCategory(int length, int width, int height, int mass) {
    bool bulky = length >= 10000 || width >= 10000 || height >= 10000 || (long)length * width * height >= 1000000000;
    bool heavy = mass >= 100;

    if (bulky && heavy) return "Both ";
    if (bulky) return "Bulky ";
    if (heavy) return "Heavy ";
    return "Neither ";
}

The algorithm first checks if the box is bulky by verifying if any of the dimensions equal or exceed 10^4, or if the volume of the box is equal to or greater than 10^9. If the mass of the box is greater or equal to 100, then it's considered heavy.

We then use conditional statements to determine the category of the box according to the values of the bulky and heavy variables.

If both the bulky and heavy conditions are true, we return "Both ". If bulky is true and heavy is false, we return "Bulky ". If heavy is true and bulky is false, we return "Heavy ". Otherwise, if neither condition is true, we return "Neither ".