[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Card tricks with Ruby

grrr

3/16/2006 8:49:00 PM

So suppose you have a deck of cards, that might have some number of cards.

First the cards are shuffled, in effect placed in random order.

Then the deck is split, ie. some number of cards are lifted from the top
of the deck and placed under the remaining cards.

How would one implement this? I was thinking of using an array, but how to
shuffle the deck, and how to split the deck?

grrr

31 Answers

James Gray

3/16/2006 9:04:00 PM

0

On Mar 16, 2006, at 2:53 PM, grrr wrote:

> how to shuffle the deck

>> cards = %w{A 2 3 4 5 6 7 8 9 T J Q K A}
=> ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K",
"A"]
>> cards = cards.sort_by { rand } # shuffle
=> ["6", "T", "Q", "5", "A", "J", "2", "7", "4", "8", "A", "9", "K",
"3"]

> and how to split the deck?

>> cards = cards.values_at(3..-1, 0..2) # cut
=> ["5", "A", "J", "2", "7", "4", "8", "A", "9", "K", "3", "6", "T",
"Q"]

Hope that gives you some new ideas.

James Edward Gray II


joey__

3/16/2006 9:05:00 PM

0

grrr wrote:
> So suppose you have a deck of cards, that might have some number of
> cards.
>
> First the cards are shuffled, in effect placed in random order.
>
> Then the deck is split, ie. some number of cards are lifted from the top
> of the deck and placed under the remaining cards.
>
> How would one implement this? I was thinking of using an array, but how
> to
> shuffle the deck, and how to split the deck?
>
> grrr

class Array
def shuffle
self.sort_by{rand}
end

def cut(index)
(self-self[0..index]).push(*self[0..index])
end
end

--
Posted via http://www.ruby-....


dblack

3/16/2006 9:05:00 PM

0

matthew.moss.coder

3/16/2006 9:07:00 PM

0

deck = (1..52).to_a

# shuffle
deck = deck.sort_by { rand }

# cut
x = rand(52)
deck = deck[x..-1] + deck[0...x] # notice differing amounts of dots


Wilson Bilkovich

3/16/2006 9:21:00 PM

0

On 3/16/06, grrr <grrr@toto.maatti> wrote:
> So suppose you have a deck of cards, that might have some number of cards

benjohn

3/16/2006 11:58:00 PM

0

> >> cards = cards.values_at(3..-1, 0..2) # cut
> => ["5", "A", "J", "2", "7", "4", "8", "A", "9", "K", "3", "6",
> "T", "Q"]

:) Heh, cool - I didn't know values_at did that. Nice :)

> Hope that gives you some new ideas.

:) I'm sure it'll be useful!

Stephen Waits

3/17/2006 6:11:00 AM

0

Matthew Moss wrote:
> deck = deck[x..-1] + deck[0...x] # notice differing amounts of dots

I find myself constantly pointing this out too.. is anyone else bothered
by it? Has Matz weighed in on this in the past?

It seems error-prone to me. Extraordinarily more error-prone than other
"stuff" in Ruby.

--Steve



francisrammeloo@hotmail.com

3/17/2006 12:40:00 PM

0

>> cards = cards.sort_by { rand }

How does this work??
I thought the code block should yield either -1, 0 or +1, but rand
yields a number between 0 and 1. Or am I mistaken?

Best regards,
Francis

dblack

3/17/2006 1:01:00 PM

0

Jim Weirich

3/17/2006 1:02:00 PM

0

francisrammeloo@hotmail.com wrote:
>>> cards = cards.sort_by { rand }
>
> How does this work??
> I thought the code block should yield either -1, 0 or +1, but rand
> yields a number between 0 and 1. Or am I mistaken?
>

You are thing of the normal sort method which looks like this:

cards.sort { |a, b|
(a.suit <=> b.suit).nonzero? || (a.value <=> b.value)
}

With sort_by, you need to return a key value that will be used as the
comparison key for that particular value.

cards.sort_by { |card| [card.suit, card.value] }

Sort_by actually does a "Schwartzian Transform"[1] on the array to
perform the sorting.

--
-- Jim Weirich

[1] A trick developed by Randall Schwartz to speed sorting in Perl. See
http://en.wikipedia.org/wiki/Schwartzian...

--
Posted via http://www.ruby-....