DS4PS / cpp-527-fall-2020

http://ds4ps.org/cpp-527-fall-2020/
0 stars 1 forks source link

Lab01 - Step03 #4

Open gzbib opened 4 years ago

gzbib commented 4 years ago

Hello Sir @lecy

I am confused regarding step03, after several randomizations, I am getting the same number for the initial pick and the open_goat_door () . Kindly check the below code and a sample answer:

open_goat_door <- function( game, a.pick)


{
   doors <- c(1,2,3)

   possible.doors <- doors[ (game == "goat" & doors != a.pick)]

   opened.door <- sample( possible.doors, size=1 )
   return( opened.door ) 
}

#Test
this.game <- create_game()
this.game
my.initial.pick <- select_door()
my.initial.pick

open_goat_door(this.game,my.initial.pick )

image

lecy commented 4 years ago

Congratulations, you are the first one to catch this bug :-)

Your code currently is FINE to receive credit for the lab. The logic is intact, and the function is structured properly.

I'm not going to tell you the issue just yet, but it is a case of a function not operating as expected when you encounter certain edge cases. I will explain more after labs have been submitted.

Feel free to keep looking to see if you can identify the source of the error (it is very subtle).

But no problem if you can't see it. This is a bug I leave in the lab to emphasize the importance of these unit tests. Most students don't catch it. I'll explain the problem and the fix in the solutions file.

MeghanPaquette commented 4 years ago

Hi Professor Lecy, I am working through how to fix the bug for step 3. I noticed it looks like you may have to identify the parts of the function (they were left in the code without the <- part). I tried a couple of different options, but am a bit stuck.

Below is the code I am using as well as the error. Do you have any hints?

Error in sample.int(length(x), size, replace, prob) : invalid first argument

Step 3: HOST OPENS GOAT DOOR

open_goat_door <- function(game, a.pick)
{
   game <- c("goat","goat","car")
   a.pick <- c(1,2,3) 
   doors <- c(1,2,3)
   possible.doors <- doors[game == "goat" & doors != a.pick]
   opened.door <- sample(possible.doors, 
                         size = 1)
   return(opened.door) # number between 1 and 3
}