Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.10 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. %% David Tam
  2. % June 18 2011
  3. % IBM Ponder This
  4. % This simulation actually runs the experiement
  5. % and averages out the solution per run. Running this for runs = 1E6
  6. % the expected average is approximately 43 cars
  7. function avgParked(runs)
  8. runData = zeros(1,runs);
  9. for runCount=1:runs
  10. x = 1:100;
  11. count = 0;
  12. while hasFreeSpace(x)
  13.     lengthX = length(x);
  14.     ChosenSpace = ceil(rand()*length(x));aa
  15.     if ChosenSpace + 1 > length(x) && x(1) == 1
  16.         x(ChosenSpace) = [];
  17.         x(1) = [];
  18.         count = count + 1;
  19.     elseif ChosenSpace + 1 > length(x) && x(1) ~= 1
  20.         continue
  21.     elseif x(ChosenSpace) + 1 == x(ChosenSpace+1)
  22.         x(ChosenSpace) = [];
  23.         x(ChosenSpace) = [];
  24.         count = count + 1;
  25.     end
  26.     count;
  27. end
  28. runData(runCount) = count;
  29. end
  30. meanSpaces = mean(runData)
  31.  
  32. function flag=hasFreeSpace(circle)
  33. flag = false;
  34. for n=1:length(circle)
  35.     if n + 1 > length(circle) && circle(1) == 1
  36.         flag = true;
  37.         break;
  38.     end
  39.     if n + 1 > length(circle) && circle(1) ~= 1
  40.         continue;
  41.     end
  42.     if circle(n) + 1 == circle(n+1)
  43.         flag = true;
  44.         break;
  45.     end
  46. end