473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Data Manipulation with pop, push, and shift.

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
1 2238
KevinADC
4,059 Expert 2GB
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

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

Similar topics

1
by: dmb000006 | last post by:
Hello, I have a database style data structure, each record has several fields. I would like to create a nested data structure that would let me 'query' the data on the value of certain...
5
by: Jeff | last post by:
I've created a beast! Here is my data structure: $VAR1 = 'bunkers'; $VAR2 = { 'items' => , \ ] }; $VAR3 = 'simpsons'; $VAR4 = {
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
1
by: Da-Breegster | last post by:
Hi. I'm attempting to write a roguelike (think nethack, crawl, angband, tome, adom, and yes, I suppose even rogue) in Perl and I've ran into a problem regarding the data structure for the map and...
8
by: ml | last post by:
My employers currently use Access for processing large volumes of data for reporting and simple modelling, which involves a lot of make table/update queries etc. I have been asked to work on a...
4
by: Schwarty | last post by:
I hope I posted this to the correct group. If not, please let me know and I will get it posted in the correct section. I have a web application developed in ASP.NET using C# for the code behind....
8
by: Eric.Medlin | last post by:
I am reading in 1bit data to a buffer that contains over 30000 bits and I would like to beable to bitshift the entire buffer and AND and OR it with other buffers of the same size. I though I could...
8
by: John Salerno | last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a question. I did the translation as such: import string alphabet = string.lowercase code = string.lowercase + 'ab'...
1
by: deepuceg | last post by:
Hi everybody, I had generated the links from and stored the web pages as text and i need to extract some fields from that text file using pattern matching some portion of my text file is ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.