KB: R script sample

coupon <- c("A","B","C","D")

generate_set<-function(coupon_set,i,n){
  if(i==n){
    cat(coupon_set)
    cat("\n")
  }else{
    for(c in coupon){
      generate_set(paste(coupon_set,c,sep=""),i+1,n)
    }
  }
}

sink("d:/temp/q6_10.txt")
generate_set("",0,10)
sink()

q6<-read.table(file="d:/temp/q6_10.txt")
q6$win<- grepl("A",q6$V1) & grepl("B",q6$V1) & grepl("C",q6$V1) & grepl("D",q6$V1)
summary(q6$win)
sum(q6$win)/nrow(q6)
Advertisements