By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,278 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,278 IT Pros & Developers. It's quick & easy.

Data Manipulation with pop, push, and shift.

P: 2
My instructor has asked me to use the pop, shift, and push functions to write a script that sufficiently "shuffles" a simulated deck of cards before printing the top five cards.

I came up with the following code but the problem I am having is that I can't seem to shuffle by just using pop, push, and shift. I think the problem for me is trying to understand what constitutes a shuffle because there are many different ways to shuffle a deck of cards. I also am aware there is a shuffle function but we are not allowed to use this for the exercise. So if I had a better understanding of how the algorithm worked I might get a little further than where I am at.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
  4. "9 H","10 H","J H","Q H","K H",
  5. "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
  6. "9 D","10 D","J D","Q D","K D",
  7. "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
  8. "9 C","10 C","J C","Q C","K C",
  9. "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
  10. "9 S","10 S","J S","Q S","K S");
  11. push(@startingdeck, shift(@startingdeck));
  12. push(@startingdeck, pop(@startingdeck));
  13. foreach my $card (@startingdeck) { print "Card: $card\n"}
Mar 28 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
My instructor has asked me to use the pop, shift, and push functions to write a script that sufficiently "shuffles" a simulated deck of cards before printing the top five cards.

I came up with the following code but the problem I am having is that I can't seem to shuffle by just using pop, push, and shift. I think the problem for me is trying to understand what constitutes a shuffle because there are many different ways to shuffle a deck of cards. I also am aware there is a shuffle function but we are not allowed to use this for the exercise. So if I had a better understanding of how the algorithm worked I might get a little further than where I am at.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
  4. "9 H","10 H","J H","Q H","K H",
  5. "A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
  6. "9 D","10 D","J D","Q D","K D",
  7. "A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
  8. "9 C","10 C","J C","Q C","K C",
  9. "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
  10. "9 S","10 S","J S","Q S","K S");
  11. push(@startingdeck, shift(@startingdeck));
  12. push(@startingdeck, pop(@startingdeck));
  13. foreach my $card (@startingdeck) { print "Card: $card\n"}
Use a loop so you push/pop/shift more than once. I will not post any code for you though since you are a student and this is your class assignment.
Mar 28 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.