Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

shuffle cards in Perl

Posted on 2011-02-27
4
Medium Priority
?
1,288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

730 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