Skip to content

Instantly share code, notes, and snippets.

@AugFJTan
Last active January 8, 2018 04:57
Show Gist options
  • Save AugFJTan/0fda42f5496353b240a91e6b1af3ebde to your computer and use it in GitHub Desktop.
Save AugFJTan/0fda42f5496353b240a91e6b1af3ebde to your computer and use it in GitHub Desktop.
The Chair Problem
// File : ChairProblem.cpp
// Description: A problem involving a store room and some chairs...
//
// Author : AugFJTan
// Last Modified 08 Jan 2018 11:56 AM
#include <iostream>
class Space
{
public:
Space(double length, double width) : m_length(length), m_width(width) {}
double getArea() { return m_length * m_width; }
double getLength() { return m_length; }
double getWidth() { return m_width; }
protected:
double m_length, m_width;
};
class Stack : public Space
{
public:
Stack(double length, double width, int number, int per_stack) : Space(length, width),
m_number(number),
m_per_stack(per_stack)
{
m_stacks = m_number / m_per_stack;
if(m_number % m_per_stack > 0)
m_stacks += 1;
}
double getTotalArea(){ return m_stacks * getArea(); }
int getNumber() { return m_number; }
int getPerStack() { return m_per_stack; }
int getStacks() { return m_stacks; }
void setSpace(Space space)
{
int length_stack_count = (int)(space.getLength() / m_length);
int width_stack_count = (int)(space.getWidth() / m_width);
int area_stack_count = length_stack_count * width_stack_count;
double available_space = area_stack_count * getArea();
std::cout << "Space is able to fit " << area_stack_count << " stacks\n\n";
if(getTotalArea() > available_space)
{
std::cout << "Unable to fit all stacks in the room.\n"
<< "Stacks reduced by " << m_stacks - area_stack_count << "\n\n";
m_stacks = area_stack_count;
}
}
private:
int m_number, m_per_stack, m_stacks;
};
int main()
{
int num_chairs = 500;
int chairs_per_stack = 6;
Space Room(4, 5);
Stack Chairs(0.5, 0.6, num_chairs, chairs_per_stack);
std::cout << "Dimensions of room : " << Room.getLength() << " X " << Room.getWidth() << " m = " << Room.getArea() << " m^2\n"
<< "Dimensions of chair: " << Chairs.getLength() << " X " << Chairs.getWidth() << " m = " << Chairs.getArea() << " m^2\n\n";
std::cout << "Number of Chairs : " << Chairs.getNumber() << std::endl
<< "Chairs per stack : " << Chairs.getPerStack() << std::endl
<< "Total number of stacks: " << Chairs.getStacks() << "\n\n";
Chairs.setSpace(Room);
std::cout << "Remaining space: " << Room.getArea() - Chairs.getTotalArea() << " m^2\n";
return 0;
}
// File : ChairProblem.java
// Description: A problem involving a store room and some chairs...
//
// Author : AugFJTan
// Last Modified 08 Jan 2018 11:56 AM
class Space {
protected double m_length, m_width;
Space(double length, double width)
{
m_length = length;
m_width = width;
}
double getArea() { return m_length * m_width; }
double getLength() { return m_length; }
double getWidth() { return m_width; }
}
class Stack extends Space {
private int m_number, m_per_stack, m_stacks;
Stack(double length, double width, int number, int per_stack)
{
super(length, width);
m_number = number;
m_per_stack = per_stack;
m_stacks = m_number / m_per_stack;
if(m_number % m_per_stack > 0)
m_stacks += 1;
}
double getTotalArea(){ return m_stacks * getArea(); }
int getNumber() { return m_number; }
int getPerStack() { return m_per_stack; }
int getStacks() { return m_stacks; }
void setSpace(Space space)
{
int length_stack_count = (int)(space.getLength() / m_length);
int width_stack_count = (int)(space.getWidth() / m_width);
int area_stack_count = length_stack_count * width_stack_count;
double available_space = area_stack_count * getArea();
System.out.println("Space is able to fit " + area_stack_count + " stacks\n");
if(getTotalArea() > available_space)
{
System.out.println("Unable to fit all stacks in the room.");
System.out.println("Stacks reduced by " + (m_stacks - area_stack_count) + "\n");
m_stacks = area_stack_count;
}
}
}
public class ChairProblem {
public static void main(String args[]) {
int num_chairs = 500;
int chairs_per_stack = 6;
Space Room = new Space(4, 5);
Stack Chairs = new Stack(0.5, 0.6, num_chairs, chairs_per_stack);
System.out.println("Dimensions of room : " + Room.getLength() + " X " + Room.getWidth() + " m = " + Room.getArea() + " m^2");
System.out.println("Dimensions of room : " + Chairs.getLength() + " X " + Chairs.getWidth() + " m = " + Chairs.getArea() + " m^2\n");
System.out.println("Number of Chairs : " + Chairs.getNumber());
System.out.println("Chairs per stack : " + Chairs.getPerStack());
System.out.println("Total number of stacks: " + Chairs.getStacks() + "\n");
Chairs.setSpace(Room);
System.out.printf("Remaining space: %.2f m^2\n", Room.getArea() - Chairs.getTotalArea());
}
}
# File : ChairProblem.py
# Description: A problem involving a store room and some chairs...
#
# Author : AugFJTan
# Python 3.6.1
# Last Modified 08 Jan 2018 11:56 AM
import math
class Space:
def __init__(self, length, width):
self._length = length
self._width = width
def getArea(self):
return self._length * self._width
def getLength(self):
return self._length
def getWidth(self):
return self._width
class Stack(Space):
def __init__(self, length, width, number, per_stack):
super().__init__(length, width)
self.__number = number
self.__per_stack = per_stack
self.__stacks = self.__number // self.__per_stack # integer division
if self.__number % self.__per_stack > 0:
self.__stacks += 1
def getTotalArea(self):
return self.__stacks * self.getArea()
def getNumber(self):
return self.__number
def getPerStack(self):
return self.__per_stack
def getStacks(self):
return self.__stacks
def setSpace(self, space):
length_stack_count = math.floor(space.getLength() / self._length)
width_stack_count = math.floor(space.getWidth() / self._width)
area_stack_count = length_stack_count * width_stack_count
available_space = area_stack_count * self.getArea()
print("Space is able to fit {} stacks\n".format(area_stack_count))
if self.getTotalArea() > available_space:
print("Unable to fit all stacks in the room.")
print("Stacks reduced by {}\n".format(self.__stacks - area_stack_count))
self.__stacks = area_stack_count
num_chairs = 500
chairs_per_stack = 6
Room = Space(4, 5)
Chairs = Stack(0.5, 0.6, num_chairs, chairs_per_stack)
print("Dimensions of room : {} X {} m = {} m^2".format(Room.getLength(), Room.getWidth(), Room.getArea()))
print("Dimensions of chair: {} X {} m = {} m^2\n".format(Chairs.getLength(), Chairs.getWidth(), Chairs.getArea()))
print("Number of Chairs : {}".format(Chairs.getNumber()))
print("Chairs per stack : {}".format(Chairs.getPerStack()))
print("Total number of stacks: {}\n".format(Chairs.getStacks()))
Chairs.setSpace(Room)
print("Remaining space: {:.2f} m^2".format(Room.getArea() - Chairs.getTotalArea()))

The Chair Problem

Inspired by a real-world problem of packing stacked chairs into a store room, the Chair Problem aims to figure out how to optimize a limited amount of space (the store room) for a given amount of rigid objects (the stacked chairs). The problem assumes that the room and chairs are square or rectangular in shape, the chairs do not sag to one side upon stacking (thus occupying more space) and there is no gap between each chair stack.

Why OOP (Object-Oriented Programming)?

The problem lends itself quite naturally to the OOP model. Here, we try to model how the room and chairs interact with each other. The room is a space and the chairs occupy space. Thus, we can create a class called Space that accepts the length and width for a Space object. The chairs, on the other hand, consist of stacks that have several unique properties, including the total number of chairs, the number of chairs per stack and the number of stacks. We can extend the functionality of the Space class and include these properties with a new class called Stack. We also add a new method called setSpace() to determine the available space based on the dimensions of the stack and the number of stacks that can fit into that space.

Language Analysis

The problem was initially solved using C++. The C++ code was then rewritten into its equivalent Java and Python code for comparison. Unsurprisingly, the Java code was implemented with only slight changes from the original C++ code. However, the Python code needed more reworking due to the many differences in terms of syntax and Python's dynamic typing.

It is worth noting that Python does not have explicit private and protected variables (see access modifiers). Instead, these variables are denoted by convention with a double underscore (e.g. __foo) for private variables and single underscore (e.g. _bar) for protected variables. This is largely due to Python not having any actual data hiding and relying on the programmer to be responsible for not modifying any class variables by accident.

Recommendations

More stack objects can be added based on the Stack class. Chairs are merely used as an example. Stacks of different objects (e.g. tables, books, pancakes(!), etc.) could interact with each other in the room, but that is a topic for another day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment