Skip to content

Commit 0fd4beb

Browse files
committed
- Added solutions by Jitu Keshwani.
1 parent 6966171 commit 0fd4beb

16 files changed

+1646
-1501
lines changed

challenge-048/jitu-keshwani/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Solutions by Jitu Keshwani.
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env perl
2+
3+
=pod
4+
5+
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.
6+
7+
This is the script to find out the survivor.
8+
Problem from :: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/
9+
10+
=cut
11+
12+
## ** publish_list sub starts here
13+
sub publish_list {
14+
15+
my $cnt = shift;
16+
my $prefix = shift;
17+
my $list_ref = shift;
18+
19+
if (scalar(@$list_ref) != 0){
20+
die "List reference passed, doesn't point to empty list.\nPlease ensure the list reference passed, points to empty list.\n";
21+
}
22+
23+
my $name;
24+
foreach my $ind (0..$cnt-1){
25+
$name = $prefix . ++$ind;
26+
push (@$list_ref, $name);
27+
}
28+
29+
return @$list_ref;
30+
}
31+
## ** publish_list sub ends here
32+
33+
## ** purge_members sub starts here
34+
## returns reference to the newly created list
35+
sub purge_members {
36+
37+
## purge_sequence : 0 means purge even entries, 1 means purge odd entries
38+
my $purge_sequence = shift;
39+
my $ref_people_list = shift;
40+
my @new_people_list=();
41+
my $people_cnt = scalar(@$ref_people_list);
42+
if ($people_cnt < 2){
43+
return $ref_people_list;
44+
}
45+
##
46+
## print "List arrived in purge_members has $people_cnt members which are @$ref_people_list\n";
47+
for (my $ind=0; $ind <= $people_cnt-1; $ind++) {
48+
#print "loop: $ind adding $$ref_people_list[$ind] to the list\n";
49+
push(@new_people_list, $$ref_people_list[$ind]);
50+
if ($ind == $people_cnt-1){
51+
#print "This is the last person with sword. removing the first entry\n";
52+
shift(@new_people_list);
53+
}
54+
else{
55+
$ind++;
56+
}
57+
}
58+
return \@new_people_list;
59+
}
60+
## ** purge_members sub ends here
61+
62+
## ** complete_purge_cycle sub starts here
63+
sub complete_purge_cycle {
64+
my $ref_people_list = shift;
65+
for (my $ind=0;scalar(@$ref_people_list)>0; $ind++){
66+
$ref_people_list = purge_members(1,$ref_people_list);
67+
if(scalar(@$ref_people_list) == 1){
68+
print "Survivor is @$ref_people_list\n ";
69+
exit;
70+
}
71+
}
72+
}
73+
74+
75+
print "Enter the number of people in the line\n";
76+
sub main {
77+
my $people_cnt = <STDIN>;
78+
my @people;
79+
my $ref_list;
80+
81+
## Publish the list with names of people
82+
publish_list($people_cnt, "Man", \@people);
83+
print "Published list: @people\n";
84+
complete_purge_cycle(\@people);
85+
}
86+
87+
main();
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/perl
2+
3+
=pod
4+
Palindrome Dates
5+
Write a script to print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001.
6+
Challenge from [Task 2; https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/]
7+
=cut
8+
9+
10+
use strict;
11+
use warnings;
12+
use POSIX;
13+
14+
sub is_pallindrome{
15+
16+
my $original_string = shift;
17+
my $rev_string = reverse $original_string;
18+
19+
if ($original_string == $rev_string){
20+
print "$original_string\n";
21+
}
22+
23+
}
24+
25+
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
26+
my $epoch = time();
27+
my $yrs30 = (30 * 365 * 24 * 60 * 60)+(7 * 24 * 60 * 60);
28+
my $jan0100 = localtime($yrs30);
29+
30+
my $date_string = strftime "%m%d%Y", localtime($yrs30);
31+
##my $this_date_string = strftime "%d%m%Y", localtime;
32+
my $this_date_string = "12312999";
33+
for (my $index=1;$date_string != $this_date_string; $index++){
34+
#print "Checking $date_string\n";
35+
is_pallindrome($date_string);
36+
$date_string = strftime "%m%d%Y", localtime($yrs30+($index * 24 * 3600));
37+
}

members.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"jeff" : "Jeff",
5959
"jeremy-carman" : "Jeremy Carman",
6060
"jim-bacon" : "Jim Bacon",
61+
"jitu-keshwani" : "Jitu Keshwani",
6162
"jj-merelo" : "JJ Merelo",
6263
"jo-christian-oterhals" : "Jo Christian Oterhals",
6364
"joelle-maslak" : "Joelle Maslak",

0 commit comments

Comments
 (0)