Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Last active April 30, 2016 03:05
Show Gist options
  • Save bhnascar/2055e57503a84a914e1d8036bb5dd1f1 to your computer and use it in GitHub Desktop.
Save bhnascar/2055e57503a84a914e1d8036bb5dd1f1 to your computer and use it in GitHub Desktop.
Free response #3
One thing Stanford students get too much of is free T-shirts. There's
free T-shirts from career fairs, from your academic department, from
dorm events, from campus events, from student groups, etc. etc. etc.
So one thing we like to do with our old T-shirts is make quilts out of
them!
Imagine we had 9 T-shirts. We could stitch them together a 3x3 quilt,
where each square unit is a T-shirt. So something like:
__________________________________
| | | |
| Tshirt | Tshirt | Tshirt |
| | | |
|__________|__________|__________|
| | | |
| Tshirt | Tshirt | Tshirt |
| | | |
|__________|__________|__________|
| | | |
| Tshirt | Tshirt | Tshirt |
| | | |
|__________|__________|__________|
Okay...so let's turn this into a programming question. Each T-shirt is
going to be a String (where the String is the T-shirt text). I want
you to write a class QuiltMaker that takes a bunch of T-shirts (i.e. a
bunch of Strings) and returns a 2D array that represents how they
would be arranged in a quilt.
So I need to be able to do the following:
QuiltMaker qm = new QuiltMaker();
qm.addShirt("Quicksilver");
qm.addShirt("Class of 2011");
qm.addShirt("Symphony Orchestra '15");
....
boolean canMakeQuilt = qm.canMakeQuilt();
....
String[][] layout = qm.getQuilt();
Write all the necessary things to make the above code compile. Besides
whatever class stuff and plumbing you need to setup, there are two
methods you need to write: "canMakeQuilt" and "getQuilt".
"canMakeQuilt" returns whether or not the QuiltMaker currently has
enough T-shirts to make a quilt. A quilt must always be 3 T-shirts
wide and have at least three rows (this means you need at least nine
T-shirts to make a quilt). Furthermore the quilt needs to be
rectangular so you can't make something jagged like:
__________________________________
| | | |
| Tshirt | Tshirt | Tshirt |
| | | |
|__________|__________|__________|
| | | |
| Tshirt | Tshirt | Tshirt |
| | | |
|__________|__________|__________|
| | | |
| Tshirt | Tshirt | Tshirt |
| | | |
|__________|__________|__________|
| | |
| Tshirt | Tshirt |
| | |
|__________|__________|
This means that you can't make a quilt with 10 or 11 T-shirts. More
generally, you can only make a quilt when the number of T-shirts you
have is greater than 9 and divisible by 3 (do you see how this follows
from the rules above?).
"getQuilt" returns a 2D array of the T-shirt layout (which means a 2D
array of Strings). The shirts can be arranged in any way you like.
If it's not possible to make a quilt (i.e. you don't have the right
number of shirts), then just return null.
@DukeOfXor
Copy link

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