-
Notifications
You must be signed in to change notification settings - Fork 0
/
misanthropic_neighbors.R
78 lines (56 loc) · 2.64 KB
/
misanthropic_neighbors.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Performs a stochastic simulation of FiveThirtyEight.com's 'The Riddler' puzzle
# for 4/22/16: Misanthropic Neighbors.
# (see: https://fivethirtyeight.com/features/can-you-solve-the-puzzle-of-your-misanthropic-neighbors/)
#
# Args:
# houses: Number of houses in the row
# trials: Number of trials to perform & average over
#
# Returns: the mean proportion of occupied houses at the end of all trials,
# and plots a histogram of proportions of occupied houses after each trial.
mishouse <- function(houses, trials) {
# initialize vectors for entire program
avgoccupied = 0 # sums proportion of occupied houses after each trial
n = houses
proplist = c() # compiles a list of proportions after each trial to plot later
#occlist = c() another method is to track by # of occupied houses vice proportion
for (t in 1:trials){
# for each trial, initialize:
# "row": a representation of rows of houses. Unoccupied houses = 'o',
# occupied = 'x'
# "choices": a vector consisting of sequential numeric designations for
# each house in the row, used to eliminate houses from consideration
# that can no longer be occupied
row = rep('o', n)
choices = 1:n
# while loop only works when there is > 1 element in 'choices', because
# of how sample() works on vectors of length 1
while (length(choices) > 1) {
pick = sample(choices, 1)
# pick random available house, mark occupied, remove it &
# next-door houses from 'choices'
row[pick] = 'x'
choices = choices[choices != (pick+1)]
choices = choices[choices != (pick-1)]
choices = choices[choices != pick]
}
# when only one choice is left, sample() is not needed, & wouldn't work
if (length(choices) == 1) {
pick = choices[1]
row[pick] = 'x'
choices = choices[choices != pick]
}
occupied = sum(row=='x') # determine # of occupied houses
propocc = occupied / n # compute proportion
proplist[length(proplist) + 1] = propocc # append proportion for each trial
#occlist[length(occlist) + 1] = occupied
avgoccupied = avgoccupied + propocc # sum each trial's proportion
}
#print(row)
hist(proplist, col = 'blue', xlim = c(.20, .60),
main = '',
xlab = paste('Proportion of', n, 'Houses That Are Occupied'),
ylab = paste('Out of', trials, 'Trials'))
return (paste('Mean Proportion of Occupied Houses Over', trials, 'Trials:',
(avgoccupied / trials)))
}