0

I'm working on the following problem

"Assume that a dealer deals cards one by one from a shuffled deck. Write a function that takes a shuffled deck as an argument and returns the number of cards dealt when the first pair arises. Simulate 10 000 dealings."

So far I've managed to create 2 different functions. One for dealing cards out of the deck and an other one for shuffling the deck.

dealing cards:

deal <- function() { 
  # draw cards from the deck   
  card <-deck[1, ]
  assign("deck", deck[-1, ], envir = globalenv())
  return(card) 
} 

deal()

shuffling the deck (deck_orginal is the deck but with 52 cards instead of 51):

shuffle <- function() {   
  shuffle_idx <- sample(seq_len(52), size = 52)   
  assign("deck", deck_orginal[shuffle_idx, ], envir = globalenv())
} 
shuffle()
Robin Gertenbach
  • 10,316
  • 3
  • 25
  • 37
  • 3
    Please post the output of `dput(deck_orginal)` in the question. – Rui Barradas Oct 03 '21 at 15:21
  • It is unclear what the actual question is. Additionally, as mentioned in the comment above please make sure your question contains reproducible code. – dario Oct 03 '21 at 15:46
  • The output seems to long to post... – 2manynmaestaken Oct 03 '21 at 15:47
  • You don't *need* to post the whole output. As long as it illustrates your problem and is reproducible it is going to be fine. Adding a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). and an example of the desired output (in code form, not tables and pictures) makes it much easier for others to find and test an answer to your question. – dario Oct 03 '21 at 15:52
  • 2
    The question is asking for [this RStudio post](https://rstudio-education.github.io/hopr/project-2-playing-cards.html) and even includes code taken from [section 8](https://rstudio-education.github.io/hopr/environments.html). – Rui Barradas Oct 03 '21 at 15:53

1 Answers1

0

First, let's do a function that returns a shuffled deck of cards.

sampleDeck = function() {
  c("Ace Clubs", "2 Clubs", "3 Clubs", "4 Clubs", "5 Clubs", "6 Clubs", "7 Clubs",
    "8 Clubs", "9 Clubs", "10 Clubs", "Jack Clubs", "Queen Clubs", "King Clubs",
    "Ace Diamonds", "2 Diamonds", "3 Diamonds", "4 Diamonds", "5 Diamonds",
    "6 Diamonds", "7 Diamonds", "8 Diamonds", "9 Diamonds", "10 Diamonds",
    "Jack Diamonds", "Queen Diamonds", "King Diamonds", "Ace Hearts", "2 Hearts",
    "3 Hearts", "4 Hearts", "5 Hearts", "6 Hearts", "7 Hearts", "8 Hearts",
    "9 Hearts", "10 Hearts", "Jack Hearts", "Queen Hearts", "King Hearts",
    "Ace Spades", "2 Spades", "3 Spades", "4 Spades", "5 Spades", "6 Spades",
    "7 Spades", "8 Spades", "9 Spades", "10 Spades", "Jack Spades",
    "Queen Spades", "King Spades")[sample(1:52,52)]}

sampleDeck()

output

 [1] "7 Clubs"        "3 Spades"       "2 Clubs"        "8 Diamonds"     "Queen Hearts"   "Ace Diamonds"   "5 Diamonds"    
 [8] "10 Hearts"      "4 Hearts"       "8 Clubs"        "6 Hearts"       "2 Spades"       "8 Hearts"       "8 Spades"      
[15] "Jack Hearts"    "10 Diamonds"    "7 Hearts"       "9 Clubs"        "7 Diamonds"     "4 Diamonds"     "Queen Clubs"   
[22] "4 Spades"       "Jack Clubs"     "9 Diamonds"     "5 Clubs"        "Ace Clubs"      "Queen Diamonds" "9 Spades"      
[29] "Ace Spades"     "3 Diamonds"     "6 Clubs"        "Ace Hearts"     "Jack Diamonds"  "6 Diamonds"     "2 Diamonds"    
[36] "10 Clubs"       "King Clubs"     "King Hearts"    "7 Spades"       "King Spades"    "Jack Spades"    "2 Hearts"      
[43] "5 Hearts"       "9 Hearts"       "4 Clubs"        "10 Spades"      "King Diamonds"  "3 Hearts"       "5 Spades"      
[50] "Queen Spades"   "3 Clubs"        "6 Spades"      

Now it is the turn of the function that will return a shuffled deck of cards and the number of cards to be dealt to get a pair.

Deal_to_the_first_pair = function(){
  deck = sampleDeck()
  deck_card = str_split_fixed(deck, " ", 2)[,1]
  i=2
  while(length(unique(deck_card[1:i]))==length(deck_card[1:i])) i=i+1
  tibble(
    deck = list(tibble(deck = deck)),
    ncards_dealt = i
  )
}

d = Deal_to_the_first_pair()

output

# A tibble: 1 x 2
  deck              ncards_dealt
  <list>                   <dbl>
1 <tibble [52 x 1]>            6

As you can see, we put out 6 cards. Let's see what the cards were.

d$deck[[1]]$deck[1:d$ncards_dealt]

output

[1] "8 Clubs"     "King Hearts" "5 Diamonds"  "3 Hearts"    "2 Clubs"     "3 Spades"   

As you can see we have a couple of threes

Finally, let's do 10,000 tries.

library(tidyverse)
set.seed(1111)
ndealings = 10000
df = tibble(i = 1:ndealings) %>% 
  mutate(data = map(i, ~Deal_to_the_first_pair())) %>% 
  unnest(data)

df

output

# A tibble: 10,000 x 3
       i deck              ncards_dealt
   <int> <list>                   <dbl>
 1     1 <tibble [52 x 1]>            7
 2     2 <tibble [52 x 1]>            8
 3     3 <tibble [52 x 1]>            5
 4     4 <tibble [52 x 1]>            9
 5     5 <tibble [52 x 1]>            5
 6     6 <tibble [52 x 1]>            8
 7     7 <tibble [52 x 1]>            5
 8     8 <tibble [52 x 1]>            5
 9     9 <tibble [52 x 1]>            3
10    10 <tibble [52 x 1]>            2
# ... with 9,990 more rows

Let us visualize the achieved result

df %>% ggplot(aes(ncards_dealt)) +
  geom_boxplot()

enter image description here

Update 1

A little update. To be fully compatible with your task, I have slightly improved the Deal_to_the_first_pair function so that it takes a shuffled deck of cards as an argument and returns the number of cards until the first pair occurs.

Deal_to_the_first_pair2 = function(deck){
  deck_card = str_split_fixed(deck, " ", 2)[,1]
  i=2
  while(length(unique(deck_card[1:i]))==length(deck_card[1:i])) i=i+1
  i
}

set.seed(1111)
df2 = tibble(i = 1:ndealings) %>% 
  mutate(deck = map(i, ~sampleDeck())) %>% 
  mutate(ncards_dealt = map(deck, ~Deal_to_the_first_pair2(.x))) %>% 
  unnest(ncards_dealt)
df2

output

# A tibble: 10,000 x 3
       i deck       ncards_dealt
   <int> <list>            <dbl>
 1     1 <chr [52]>            7
 2     2 <chr [52]>            8
 3     3 <chr [52]>            5
 4     4 <chr [52]>            9
 5     5 <chr [52]>            5
 6     6 <chr [52]>            8
 7     7 <chr [52]>            5
 8     8 <chr [52]>            5
 9     9 <chr [52]>            3
10    10 <chr [52]>            2
# ... with 9,990 more rows

Now you have it just like in your homework :-).

Marek Fiołka
  • 4,825
  • 1
  • 5
  • 20
  • 2
    It's a fun problem to think through and your answer works, but the OP's question does not provide enough information to know if this solution is valid for them. I wrote (and then deleted) a solution that produces the same result but works completely differently (a "deck" is a vector of integers, and no loops are used). – jdobres Oct 03 '21 at 16:29
  • I do not understand. My solution does exactly what @2manynmaestaken expects. The task does not specify how each card should be represented. As you can see, the solution works. Not only that, it worked quickly. For me, 10 000 draws took 1.5 seconds. – Marek Fiołka Oct 03 '21 at 16:38
  • 2
    Right, but if the OP comes back and says, "a deck is a list of integers valued 0 through 51", or, "a deck is an R object class with several methods," your solution would not be valid. They did not provide enough information. – jdobres Oct 03 '21 at 16:41
  • 3
    That's why we should sometimes wait until the question is clear... rushing to answer when we don't even know what the question is leads just to frustration ;) (Also, since this is about homework, it's probably better to just guide them in the general direction than providing a finished recipe ;) – dario Oct 03 '21 at 16:54