2

I have the following Python function,

import random
import math

deck = [2,3,4,5,6,7,8,9,10,10,10,10,11]

def draw_card(deck):
        card = deck.pop(random.sample(range(len(deck)), 1)[0])
        return(card)

print(deck,'\n')
my_draw = draw_card(deck)
print(my_draw, '\n')
print(deck, '\n')

The output is below,

[2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] 

3 

[2, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] 

I need to convert this to R but I understand there is no equivalent Python list.pop function in R. I am not worried about random pick as I can do a rep in R and am able to emulate the drawing of card in R by,

DrawCard <- function(card){
  card <- tail(deck,1)
  return(card)
}

Also I am able remove the same drawn card by head(deck, -1) but I do not know how to feed the 2 in 1 function like in Python that will return both the card value and also the shorter length deck.

Please help.

Thanks, Lobbie

1
  • 1
    R mostly doesn't do in-place mutation; i.e. a function doesn't alter an object, but instead returns a version of that object with which it can be overwritten. The R-esqe version of list.pop would be to subset twice. See ?sample for shuffling. Commented May 6, 2016 at 15:05

2 Answers 2

1

I'm not suggesting that you do this (returning a list of the card and the new deck is a better idea), but you can have the function alter the global deck variable like so:

draw_card <- function(deck){
  card <- tail(deck,1)
  deck <<- head(deck, length(deck)-1)
  return(card)
}

print(deck)
# [1]  2  3  4  5  6  7  8  9 10 10 10 10 11

print(draw_card(deck))
# [1] 11

print(deck)
#  [1]  2  3  4  5  6  7  8  9 10 10 10 10

For random sampling (thanks to Ben Bolker) and a function that will work with any deck variable:

draw_card <- function(deck){
  s <- sample(length(deck),size=1)
  card <- deck[s]
  n <- deparse(substitute(deck))
  assign(n, deck[-s], envir=globalenv())
  return(card)
}
Sign up to request clarification or add additional context in comments.

7 Comments

this shouldn't be head/tail, to match OP's question it should be something like s <- sample(length(deck),size=1); card <- deck[s]; deck <- deck[-s]
Hi CactusWoman and Ben, one of my ideas was to create another ReDeck func for head(deck,-1) to be called after DrawCard() but both your suggestions are good and with Ben's, I can actually random draw, I think. will try it out and let you know how I go. thanks.
Hi again, thank you and both of your answers worked!
@Lobbie Well, if you want to use this method (I still think you should return a list), check out the edit I made; the previous code I wrote would overwrite the global variable deck, not the variable you pass into the function.
thanks but am a bit confused now between the previous one and this latest one. both assign the value back to the global variable deck isn't it? also, why do u think I should return a list? any particular reasons? thanks.
|
1

With your logic, return as list

DrawCard <- function(deck){
  card <- tail(deck,1)
  remain<-head(deck,9)
  return(list(card,remain))
}

cc<-1:10
DrawCard(cc)

A better logic may be,

DrawCard <- function(deck){
  ind<-1:length(deck)
  choose<-sample(ind,1)
  card<-deck[choose]
  remain<-deck[-choose]
  return(list(card,remain))
}

Appended: to answer loop

sum=0
ll<-length(cc)
deck_rem<-cc
for(x in 1:ll){

  a<-DrawCard(deck_rem)
  deck_rem=a[[2]]
  sum=sum+a[[1]]
  print(a[[1]])
  print(a[[2]])
  print(sum)

}

3 Comments

thanks but this function is to be in a loop and the card value will be use for summing and the remaining deck will be used for drawing over and over again so am not sure a list will be useful here. thank you no less for responding.
I don't want to dig into whether algorithm is correct or not. but its doable. See my appended answer.
thank you, Ananta and yes, it is definitely doable. CatusWoman's and Ben's answers above are simpler and suit my needs. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.