You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I took the advice in the book to implement a faster vectorised score-calculation function using rowSums and vectorised selective updates. The method of decomposing the problem differs from that in the book. Namely, wild diamonds are handled by adding the diamond count to each check, as, per the problem description, the diamonds can be thought of as any other symbol (an exception is made for the cherry cases).
score.count.fast<-function(symbols) {
# counts of symbols in each sampledd<- rowSums(symbols=="DD")
x7<- rowSums(symbols=="7")
b3<- rowSums(symbols=="BBB")
b2<- rowSums(symbols=="BB")
b1<- rowSums(symbols=="B")
cc<- rowSums(symbols=="C")
# calculate prize with selective updating (higher prizes later to override lower ones)prize= integer(nrow(symbols)) # defaults to a number of 0sprize[cc==1] <-2prize[cc>0&cc+dd==2] <-5prize[b3+b2+b1+dd==3] <-5prize[cc+dd==3] <-10prize[b1+dd==3] <-10prize[b2+dd==3] <-25prize[b3+dd==3] <-40prize[x7+dd==3] <-80prize[dd==3] <-100# apply diamonds doubling effectprize* (2^dd)
}
The resulting function is about 3x shorter and, on my machine, about 3x faster than the book's example solution. In my opinion, it's also significantly simpler in the way it handles wild symbols, and I think the method could also easily be generalised to new similar sets of rules without significant alteration. A branch-based, non-vectorised version is also possible, which is of similar complexity and speed to the book's example (but it doesn't serve as a good demonstration of vector lookups).
I tested both versions and they give identical output to the book's code for all combinations of symbols. An rmarkdown script for testing and for profiling with microbenchmark is attached:
Post the alternative method here for others to learn from if interested
Contribute the simpler method in case the author would like to adapt it for use in future versions of the book
Invite others to further improve the efficiency of the method and share their approaches and results
My thanks to the author and all contributors for their work on making an excellent open source book that helped me efficiently learn the fundamentals of R.
The text was updated successfully, but these errors were encountered:
I took the advice in the book to implement a faster vectorised score-calculation function using
rowSums
and vectorised selective updates. The method of decomposing the problem differs from that in the book. Namely, wild diamonds are handled by adding the diamond count to each check, as, per the problem description, the diamonds can be thought of as any other symbol (an exception is made for the cherry cases).The resulting function is about 3x shorter and, on my machine, about 3x faster than the book's example solution. In my opinion, it's also significantly simpler in the way it handles wild symbols, and I think the method could also easily be generalised to new similar sets of rules without significant alteration. A branch-based, non-vectorised version is also possible, which is of similar complexity and speed to the book's example (but it doesn't serve as a good demonstration of vector lookups).
I tested both versions and they give identical output to the book's code for all combinations of symbols. An rmarkdown script for testing and for profiling with microbenchmark is attached:
This issue serves three purposes
My thanks to the author and all contributors for their work on making an excellent open source book that helped me efficiently learn the fundamentals of R.
The text was updated successfully, but these errors were encountered: