|
| 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(); |
0 commit comments