
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.10 KB | hits: 17 | expires: Never
%% David Tam
% June 18 2011
% IBM Ponder This
% This simulation actually runs the experiement
% and averages out the solution per run. Running this for runs = 1E6
% the expected average is approximately 43 cars
function avgParked(runs)
runData = zeros(1,runs);
for runCount=1:runs
x = 1:100;
count = 0;
while hasFreeSpace(x)
lengthX = length(x);
ChosenSpace = ceil(rand()*length(x));aa
if ChosenSpace + 1 > length(x) && x(1) == 1
x(ChosenSpace) = [];
x(1) = [];
count = count + 1;
elseif ChosenSpace + 1 > length(x) && x(1) ~= 1
continue
elseif x(ChosenSpace) + 1 == x(ChosenSpace+1)
x(ChosenSpace) = [];
x(ChosenSpace) = [];
count = count + 1;
end
count;
end
runData(runCount) = count;
end
meanSpaces = mean(runData)
function flag=hasFreeSpace(circle)
flag = false;
for n=1:length(circle)
if n + 1 > length(circle) && circle(1) == 1
flag = true;
break;
end
if n + 1 > length(circle) && circle(1) ~= 1
continue;
end
if circle(n) + 1 == circle(n+1)
flag = true;
break;
end
end