Solved

shuffle cards in Perl

Posted on 2011-02-27
4
1,221 Views
Last Modified: 2012-05-11
Using pop, shift, push, and the starting code below, write a script that sufficiently "shuffles" a simulated deck of cards before printing the top five cards.

#!/usr/bin/perl

@startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
                 "9 H","10 H","J H","Q H","K H",
                 "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
                 "9 D","10 D","J D","Q D","K D",
                 "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
                 "9 C","10 C","J C","Q C","K C",
                 "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
                 "9 S","10 S","J S","Q S","K S");
0
Comment
Question by:craziestfire73
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 125 total points
ID: 34994908
see
perldoc -q shuffle
0
 

Author Comment

by:craziestfire73
ID: 34996172
Thanks ozo:

I was able to shuffle the cards but I'm only able to print out the top 16 cards and not the top 5. here is what I have:

#!/usr/bin/perl

@startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
                 "9 H","10 H","J H","Q H","K H",
                 "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
                 "9 D","10 D","J D","Q D","K D",
                 "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
                 "9 C","10 C","J C","Q C","K C",
                 "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
                 "9 S","10 S","J S","Q S","K S");
@random_card = shuffle(@startingdeck);

foreach $line (@random_card){
       push(@startingdeck, shift(@random_card));
       push(@startingdeck,  pop(@random_card));
}
print 'random top 5 cards: ' .  join(',  ', @random_card) . "\n";

any guidance would be appreciated
0
 

Author Closing Comment

by:craziestfire73
ID: 34996183
I was not looking for the answer but a guide to my problem and the answer helped a lot.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 34997883
To print only the top 5 cards, you change the final print to:

print 'random top 5 cards: ', join(', ', @random_card[0..4]), "\n";
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now