Skip to content

Commit a0c7bb4

Browse files
committed
challenge 048 perl and raku
1 parent f027276 commit a0c7bb4

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#Survivor
2+
#There are 50 people standing in a circle in position 1 to 50. The person standing at position 1 has a sword. He kills the next person i.e. standing at position 2 and pass on the sword to the immediate next i.e. person standing at position 3. Now the person at position 3 does the same and it goes on until only one survives.
3+
#
4+
#Write a script to find out the survivor.
5+
use strict;
6+
use warnings;
7+
8+
my $numberPeople = 50;
9+
my $numberPeopleAlive = $numberPeople;
10+
my @aPeople = ();
11+
foreach my $position (1..$numberPeople){
12+
my $nextPosition = $position == $numberPeople ? 1 : $position +1;
13+
my %hTmp = ( 'nextPosition' => $nextPosition);
14+
push (@aPeople, \%hTmp);
15+
}
16+
17+
18+
my $swordPosition = 1;
19+
while ($numberPeopleAlive > 1){
20+
21+
my $killPosition = $aPeople[ $swordPosition - 1 ]->{'nextPosition'};
22+
$aPeople[ $swordPosition - 1 ]->{'nextPosition'} = $aPeople[ $killPosition - 1 ]->{'nextPosition'};
23+
$swordPosition = $aPeople[ $killPosition - 1 ]->{'nextPosition'};
24+
$numberPeopleAlive--;
25+
26+
}
27+
28+
print "Last Position Alive : $swordPosition\n";
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Palindrome Dates
2+
#Write a script to print all Palindrome Dates between 2000 and 2999.
3+
#The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001.
4+
5+
use strict;
6+
use warnings;
7+
use DateTime;
8+
use Try::Tiny;
9+
use English;
10+
11+
12+
foreach my $date (2000..2999){
13+
my $palindromeDate = reverse($date).$date;
14+
if (isValidDate ($palindromeDate )){
15+
print "Palindrome Date $palindromeDate \n";
16+
}
17+
}
18+
19+
sub isValidDate {
20+
my $dateToValidate = shift;
21+
my $validDate = 1;
22+
my $dt = try {
23+
DateTime->new(
24+
year => substr($dateToValidate, 4,4),
25+
month => substr($dateToValidate, 0,2),
26+
day => substr($dateToValidate, 2,2));
27+
} catch {
28+
if ($ARG){
29+
$validDate = 0;
30+
}
31+
};
32+
return $validDate;
33+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use v6;
2+
3+
4+
5+
my $numberPeople = 50;
6+
my $numberPeopleAlive = $numberPeople;
7+
my @aPeople;
8+
for 1..$numberPeople {
9+
my $nextPosition = $_ == $numberPeople ?? 1 !! $_ +1;
10+
@aPeople.push({'nextPosition' => $nextPosition});
11+
}
12+
13+
my $swordPosition = 1;
14+
while $numberPeopleAlive > 1 {
15+
my $killPosition = @aPeople[ $swordPosition-1 ]<nextPosition>;
16+
@aPeople[ $swordPosition-1 ]<nextPosition> = @aPeople[ $killPosition-1 ]<nextPosition>;
17+
$swordPosition = @aPeople[ $killPosition-1 ]<nextPosition>;
18+
$numberPeopleAlive--;
19+
20+
}
21+
22+
print "Last Position Alive : $swordPosition\n";
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Palindrome Dates
2+
#Write a script to print all Palindrome Dates between 2000 and 2999.
3+
#The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001.
4+
5+
use v6;
6+
7+
for 2000..2999 {
8+
my $mmdd = $_.flip;
9+
my $date = Date.new($_, $mmdd.substr(0,2), $mmdd.substr(2,2));
10+
CATCH {
11+
default { }
12+
}
13+
say $mmdd ~ $_;
14+
}

0 commit comments

Comments
 (0)