Monday Math Madness #28: Monday Mac Madness Returns

mmm

red_airplanes.jpg

The Problem Statement

Daniel: Hey Quan, we just received a shipment of the latest Green MacBook Pros from Apple Inc. Unfortunately, they heard about what we did to the last shipment, so they only sent 2 this time. One for you, and one for me.

Quan: Hm, what are you going to do with your laptop?

Daniel: Well, I just moved into a new apartment complex that is 36 stories high. I’ve always pondered how durable these Green MacBooks really are. And I was wondering what the highest floor is from which I can drop this MacBook, and still have it operational . . .

Quan: Well, since you only have one MacBook, you could start at the first floor. If you drop it there, and it doesn’t break, you can move on up to the second floor. Using this method, you can work your way up to the 36th floor. The worst case scenario is that it could take 36 iterations, but eventually you’ll find out the highest floor that the MacBook can survive from!

Daniel: Yes, I could do that, but my apartment complex doesn’t have an elevator, and I would get tired of running up and down the stairs all day. Now, if I had two Green MacBooks at my disposal, I could perform this task in a much more efficient manner.

Quan:

Daniel: Don’t worry Quan, I have it all figured out. In the worst case scenario, it will only take us ____ iterations to figure out what the highest floor is!


How many iterations do Quan and Daniel have to perform to determine the highest floor that a Green MacBook can be dropped and still be operational?

You may assume the following:

  • A MacBook that survives a fall can be used again.
  • A broken MacBook must be discarded.
  • The effect of a fall is the same for all MacBooks.
  • If an MacBook breaks when dropped, then it would break if dropped from a higher window.
  • If an MacBook survives a fall, then it would survive a shorter fall.
  • It is not ruled out that the first-floor windows break MacBooks, nor is it ruled out that the 36th-floor windows do not cause a MacBook to break.

The Answer

See the answer and winner here.

Continue Reading »

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MATLAB Geocache - Another Chance To Snag Some Bling

I recently participated in a geocache find that Sam and Loren at the MathWorks organized. Although I didn’t have to do anything too challenging to find it (as shown below), it still turned out to be an enjoyable little diversion. So I took the liberty of extending the fun.

rock_climbing.jpg

The Bounty has been Relocated . . . in Denver!

With the help of my good friends Pete and Heather, the goods have been re-cached here. If you’re in the Denver area, go get yourself a cool Mathworks laser pointer / flashlight. Once you have secured the prize, post your findings back here. Remember to take some pictures of your find and to send them through the contact form, I’ll be sure to post your pictures!

geocache.jpg

A Hint for the Treasure Hunters

Besides the geohash link above, here’s a little hint on the hiding spot:

“It’s parked down low, like Fred Flintstone’s car.”

Yabba Dabba Doo! Happy Hunting!

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MATLAB - Batch Processing a Group of Files

Matlab LogoIf there’s one thing that computers are good at, it’s performing a task many times over. In my experience, being able to write code that automates a process is a gigantic time saver. In this tutorial, we will discuss how to take a specific process, and how to loop it effectively so that the process can be repeated as many times as necessary.

Contents

The Task: Renaming 265 Files

My boss recently gave me the less than enviable task of renaming 265 files. I was told to append each filename with “_test_data”. So for example, if the original data file was called, water_02252009.txt, it was supposed to be renamed to water_02252009_test_data.txt.

You can imagine that doing such a thankless task would require a lot of time if it were done manually. Luckily, MATLAB is available for sticky situations such as these.

Do it Once, but Do it Perfectly

The power of computers allows us to replicate a task as many times as we need it. Thus, it’s important to create a function that can do the desired task to a single file, and it’s equally important to make sure that it works perfectly. Let’s write a function that will append a given file name with our desired text.

function processFile(fileName,inputDir,outputDir)
%fileName is the name of the input file name
%inputDir is the directory where the input file is located
%outputDir is the directory where we'll put the renamed file

%the fullfile command makes sure that the file path is created correctly with
%the appropiate number of "/"
fullFilePath_input = fullfile(inputDir,fileName);

%get the different parts of the file name
[pathstr,name,ext] = fileparts(fullFilePath_input);

%append the new file name
newFileName = [name 'text_data.' ext];

%create the full path of the output file name
fullFilePath_output = fullfile(outputDir,newFileName);

%create a copy in the output folder, so that the original
%file remains intact
copyfile(fullFilePath_input,fullFilePath_output)

If you noticed, the CD command was never used throughout the code. There is no need to change directories if you use the absolute path name of a file, instead of the relative path name. The code would have been just fine if we had used the CD command, but I find that it can be sloppy and unnecessary. More on this in a later post!

The Input and Output Directory

Since the input and output directory are two of the inputs to the function we just wrote, we’re going to need a way to query the user for this information.

This part is easily taken care of with the following two commands:

%prompt the user to select the input directory
inputDir= uigetdir;

%prompt the user to select the output directory
outputDir = uigetdir;

The DIR Command

Next, we need to get a list of the files within the input directory. Let’s assume that we have the following files located in some folder:

water_02212009.txt
water_02222009.txt
water_02232009.txt
water_02242009.txt
water_02252009.txt
water_02262009.txt
.
.
.
water_08142009.txt

Let’s say that we wanted to process each file within that folder.

%obtain a structure that contains all the text files within the directory
%pwd is the present working directory
fileList = dir(pwd,'*.txt');

%the fileList structure will only take the text files that start with "water"
%if we do the following:
fileList = dir(pwd,'water*.txt');

%this gives added flexibility in your file selection

Creating the Wrapper

Now that we have all the inputs to our processFile function, we’re ready to complete the rest of the code. The rest of the code is referred to as the “Wrapper”, as it’s the high level code used to run the processFile function. Writing a wrapper function is convenient because it can be used again in the future, saving you from re-inventing the wheel. Let’s say your boss decided that he wanted you to do 10 different operations to each of the 265 files. With the wrapper function intact, it’s just a matter of writing some more modular functions that can perform each of those 10 operations.

My wrapper function looks like this:

function wrapperFunction

%prompt the user to select the input directory
inputDir= uigetdir;

%prompt the user to select the output directory
outputDir = uigetdir;

%pull the list of files from the input directory
fileList = dir(inputDir,'water*.txt');

for x=1:length(fileList)

    %get the filename of the first file within the input directory
    fileName = fileList(x).name;

    processFile(fileName,inputDir,outputDir);
end

Alternative: The UIGETFILE Command

Another great way to obtain a list of files that you want to process is by using the UIGETFILE command. It has a cool feature called “multiselect” which allows you to select as many files as you desire.

[files2get path2get] = uigetfile('*.txt','MultiSelect','on');

This is an extremely flexible command that lets the user choose which files to process. If you want to have greater control on which files are selected, this is probably your best bet. It should be noted that the list of file names that are returned by this command will be stored in a cell array. In addition, if only ONE file is selected, than the data will NOT be stored as a cell array, but as a string, so be careful!

Next Time

Did you notice that the command cd was never used? In my experience, using this command leads to more trouble than it’s worth. By using full file paths, you can prevent a lot of heartache in the future. More to come on that topic next time!

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

Monday Math Madness #27: Simple Sum?

mmm

This is a cross post from http://wildaboutmath.com/:

The Problem Statement

Given: mmm27a.gif

Simplify this expression:
mmm27.gif

Rules for the contest

1. Email your answers with solutions to mondaymathmadness at gmail dot com.
2. Only one entry per person.
3. Each person may only win one prize per 12 month period. But, do submit your solutions even if you are not eligible.
4. Your answer must be explained. You must show your work! Wild About Math! and Blinkdagger will be the final judges on whether an answer was properly explained or not.
5. The deadline to submit answers is Tuesday, March 10, 12:01AM, Pacific Time. (That’s Tuesday morning, not Tuesday night.) Do a Google search for “time California” to know what the current Pacific Time is.)
6. The winner will be chosen randomly from all timely well-explained and correct submissions, using a random number generator.
7. The winner will be announced Friday, March 13, 2009.
8. The winner (or winners) will receive a Rubik’s Revolution or a $10 gift certificate to Amazon.com or $10 USD via PayPal. For those of you who don’t want a prize I’ll donate $10 to your favorite charity.
9. Comments for this post should only be used to clarify the problem. Please do not discuss ANY potential solutions.
10. I may post names and website/blog links for people submitting timely correct well-explained solutions. I’m more likely to post your name if your solution is unique.

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

Monday Math Madness #26 Winner - Around the World

MMM #26 Winner

mmmwin.jpg

The winner for this edition of MMM is Tom Mayo. This problem is probably one of the top five problems that I have enjoyed the most out of all the MMMs that we have done. I hope you guys enjoyed it as much as I did. A lot of people wonder where these problems originate from, and all I can say is that I hear about these problems mostly from friends and colleagues, as they also love to solve these little brain teasers and riddles. So for the most part, these problems are not created by us folks.

The number of submissions were about the same, but the comments for this problem were up. It looks like we need to do a better job promoting MMM. We’ll try to continue to present more problems like this, which will contrast nicely with what Sol does over at wildaboutmath.com.

The Answer by Robert LaKamWa

3 total planes (traveller included) are required to allow one airplane to travel all around the world.

In order to minimize planes, we want to make sure that we limit the number of planes that are distant from the island. This means we want the traveler to be alone in covering the far half of the earth. The question is how to get him there (the 25% point) with a full tank of gas and how to fuel him again when he comes back around (the 75% point).

The most efficient way to fuel him at the quarter-way point is to send 2 extra planes, sending one of them 1/8 of the way around the world, donating 1/2 of its fuel to the others (1/4 in each of the others, who would be at 3/4 tank), then returning to the island. Both remaining planes would have a full tank at the 12.5% point. When they reach the 25% point, they will each have a 3/4 tank again, so the remaining extra plane will donate 1/4 of its fuel, leaving it enough to return, while also fueling the traveler plane to a full tank.
(It is clear that this fueling is not possible with only 1 extra plane because when it reaches the 25% point, it will not have anything to donate if it wishes to return to the island.)

In order to meet the plane at the 75% point we will have to send 2 extra planes travelling in the opposite direction of the traveler plane. Send them 1/8 of the way around, then let one of them donate a quarter of a tank to the other one (who should be at 3/4 tank) then let the donor return to the island. When the other extra plane meets the traveller, he will have 3/4 tank and the traveller will be at empty. The extra plane can donate 1/4 tank to the traveller and still have enough to return. The traveller will be able to make it to the 87.5% point now without needing to refuel. Luckily, the plane that returned from 1/8 of the way around will have time to come back around and donate fuel again at this point, giving the traveller enough fuel to make the trip and for all planes to return safely. (Again, it requires 2 extra planes for this part of the trip because a single tank is not big enough to hold the extra fuel required for the return of the traveler and itself.)

This may seem like it requires 4 extra planes, in addition to the traveler. However, while the traveler is going around the other side of the world, the other planes have the time to return to the island, fuel up and go in the opposite direction to make the meeting trip. Thus, the 2 extra planes from the first half of the trip can be used on the second half of the trip. This means only 2 extra planes are required.

Therefore,
3 total planes (traveler included) are required to allow one airplane to travel all around the world.

Post-refill tank statuses are in square brackets. [A,B,C]
Waypoint 0 (0): Planes A,B,C leave the island clockwise. [100%,100%,100%]
Waypoint 1 (pi/4): Plane B donates one quarter-tank to each of the others so B and C have full tanks. Plane B begins to return. [100%,25%,100%]
Waypoint 2 (pi/2): Plane B arrives at island. Plane C donates a quarter-tank to A, giving C a full-tank. Plane C begins to return. [100%,100%,50%]
Waypoint 3 (3pi/4): Plane A continues its travels. Plane B is sitting at the island. Plane C is still en route to island. [75%,100%, 25%]
Waypoint 4 (pi): Plane A is on other side of world from island. Plane C arrives at island. Planes B,C leave island counter-clockwise. [50%,100%, 100%]
Waypoint 5 (5pi/4): Plane B gives one quarter-tank to C so C has a full tank. Plane B begins to return. [25%,50%, 100%]
Waypoint 6 (3pi/2): Plane C gives a quarter-tank to Plane A, begins to return. Plane B arrives at island, immediately fuels up and leaves. [25%,100%, 50%]
Waypoint 7 (7pi/4): Plane B meets A and C, gives a quarter-tank to A, then all return. [25%, 50%, 25%]
Waypoint 8 (2pi): All planes arrive at island. [100%,100%,100%]

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

« Prev - Next »