We are going to re-write the card shuffling script again, this time copy the scr
ID: 667974 • Letter: W
Question
We are going to re-write the card shuffling script again, this time copy the script, obj12.pl, as obj13-2.pl. Remove the code that does the shuffling and include it as another function in obj13-lib.pl. After the first "hand", first five elements of the first shuffled array, is dealt(printed), call the shuffling function again before "dealing"(printing) another, different "hand"(first five elements of the second shuffled array). Make sure all cards(elements) printed are unique. Because there is a built-in Perl function named "shuffle" do not call your new function "shuffle".
I already completed obj12.pl...here it is...
#!/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");
for ($i=0; $i<26; $i++){
$left = shift(@startingdeck);
push (@shuffled, $left);
$right = pop(@startingdeck);
push (@shuffled, $right);
}
foreach $card (@shuffled){
print "$card ";
}
my @print = @shuffled[0 .. 4];
for(@print){
s/H/Hearts/;
s/C/Clubs/;
s/D/Diamonds/;
s/S/Spades/;
}
print "The top five cards are @print ";
Explanation / Answer
modified perl script below:
#!/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");
sub shuff
{
for ($i=0; $i<26; $i++){
$left = shift(@startingdeck);
push (@shuffled, $left);
$right = pop(@startingdeck);
push (@shuffled, $right);
}
foreach $card (@shuffled){
print "$card ";
}
}
shuff();
my @print = @shuffled[0 .. 4];
for(@print){
s/H/Hearts/;
s/C/Clubs/;
s/D/Diamonds/;
s/S/Spades/;
}
print "The top five cards are @print ";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.