diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-03 00:33:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-03 00:33:57 +0100 |
| commit | 6ea8c7be696ebc4d75992ba766f888b07bd1ad5e (patch) | |
| tree | d75fa793611f96593ccfbae41d07049992822654 | |
| parent | d83e5c7e9b38aa40d7e2aa984ec0d5ddc34f3e7e (diff) | |
| parent | 6f4d4188e4b1e3be2a3fdeb5da7f3a7a9f8794d8 (diff) | |
| download | perlweeklychallenge-club-6ea8c7be696ebc4d75992ba766f888b07bd1ad5e.tar.gz perlweeklychallenge-club-6ea8c7be696ebc4d75992ba766f888b07bd1ad5e.tar.bz2 perlweeklychallenge-club-6ea8c7be696ebc4d75992ba766f888b07bd1ad5e.zip | |
Merge pull request #7821 from jeanluc2020/jeanluc-210
Add solution 210
| -rw-r--r-- | challenge-210/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-210/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-210/jeanluc2020/perl/ch-1.pl | 73 | ||||
| -rwxr-xr-x | challenge-210/jeanluc2020/perl/ch-2.pl | 112 |
4 files changed, 187 insertions, 0 deletions
diff --git a/challenge-210/jeanluc2020/blog-1.txt b/challenge-210/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..07079042b2 --- /dev/null +++ b/challenge-210/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-210-1.html diff --git a/challenge-210/jeanluc2020/blog-2.txt b/challenge-210/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..0c5d20dcff --- /dev/null +++ b/challenge-210/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-210-2.html diff --git a/challenge-210/jeanluc2020/perl/ch-1.pl b/challenge-210/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..eae517eb0c --- /dev/null +++ b/challenge-210/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,73 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-210/#TASK1 +# +# Task 1: Kill and Win +# ==================== +# +# You are given a list of integers. +# +# Write a script to get the maximum points. You are allowed to take out (kill) +# any integer and remove from the list. However if you do that then all +# integers exactly one-less or one-more would also be removed. Find out the +# total of integers removed. +# +## Example 1 +## +## Input: @int = (2, 3, 1) +## Output: 6 +## +## First we delete 2 and that would also delete 1 and 3. So the maximum points +## we get is 6. +# +## Example 2 +## +## Input: @int = (1, 1, 2, 2, 2, 3) +## Output: 11 +## +## First we delete 2 and that would also delete both the 1's and the 3. Now we +## have (2, 2). +## Then we delete another 2 and followed by the third deletion of 2. So the +## maximum points we get is 11. +# +############################################################ +## +## discussion +## +############################################################ +# +# We can check each element from the list as the "kill" integer. +# For that integer, we calculate the points. +# Then we output the maximum points. + +use strict; +use warnings; +use List::Util qw(max); + +kill_and_win(2, 3, 1); +kill_and_win(1, 1, 2, 2, 2, 3); +kill_and_win(1, 2, 3, 4, 8, 4, 3); + +sub kill_and_win { + my @list = @_; + print "Input: (" . join(", ", @list) . ")\n"; + my $results; + foreach my $elem (@list) { + next if $results->{$elem}; # no need to recalculate duplicates + # calculate the sum for the deleted elements of the list if we + # kill $elem + my $result = kill_from_list($elem, @list); + $results->{$elem} = $result; + } + print "Output: " . max(values(%$results)) . "\n"; +} + +sub kill_from_list { + my ($what, @list) = @_; + my $sum = 0; + foreach my $elem (@list) { + $sum += $elem if $elem == $what; + $sum += $elem if $elem == $what + 1; + $sum += $elem if $elem == $what - 1; + } + return $sum; +} diff --git a/challenge-210/jeanluc2020/perl/ch-2.pl b/challenge-210/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ca45ba77de --- /dev/null +++ b/challenge-210/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,112 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-210/#TASK2 +# +# Task 2: Number Collision +# ======================== +# +# You are given an array of integers which can move in right direction if it is +# positive and left direction when negative. If two numbers collide then the +# smaller one will explode. And if both are same then they both explode. We +# take the absolute value in consideration when comparing. +# +# All numbers move at the same speed, therefore any 2 numbers moving in the +# same direction will never collide. +# +# Write a script to find out who survives the collision. +# +## Example 1: +## +## Input: @list = (2, 3, -1) +## Output: (2, 3) +## +## The numbers 3 and -1 collide and -1 explodes in the end. So we are left with (2, 3). +# +## Example 2: +## +## Input: @list = (3, 2, -4) +## Output: (-4) +## +## The numbers 2 and -4 collide and 2 explodes in the end. That gives us (3, -4). +## Now the numbers 3 and -4 collide and 3 explodes. Finally we are left with -4. +# +## Example 3: +## +## Input: @list = (1, -1) +## Output: () +## +## The numbers 1 and -1 both collide and explode. Nothing left in the end. +# +############################################################ +## +## discussion +############################################################ +# +# Negative numbers at the left and positive numbers at the right of the array +# are finished moving. Numbers can only move if a negative number is right of a +# positive number. + +use strict; +use warnings; + +number_collision(2, 3, -1); +number_collision(3, 2, -4); +number_collision(1, -1); + +sub number_collision { + my @list = @_; + print "Input: (" . join(", ", @list) . ")\n"; + my @result = explode(@list); + print "Output: (" . join(", ", @result) . ")\n"; +} + +sub explode { + my @list = @_; + my @result = (); + while(@list && $list[0] < 0) { + push @result, shift @list; + } + my $exploded = 0; + foreach my $i (0..$#list-1) { + if($list[$i] >= 0 && $list[$i+1] < 0) { + if($list[$i] == abs($list[$i+1])) { + # both explode + $i++; + $exploded = 1; + next; + } elsif ($list[$i] > abs($list[$i+1])) { + push @result, $list[$i]; + $i++; + $exploded = 1; + next; + } else { + push @result, $list[$i+1]; + $i++; + $exploded = 1; + next; + } + } elsif ($list[$i] == 0 && $list[$i+1] >= 0) { + push @result, $list[$i]; + next; + } elsif ($list[$i] > 0 && $list[$i+1] == 0) { + push @result, $list[$i]; + $i++; # skip the following 0 + $exploded = 1; + next; + } elsif($list[$i] > 0 && $list[$i+1] > 0) { + push @result, $list[$i]; + if($i == $#list - 1) { + push @result, $list[$i+1]; + } + next; + } else { # $list[$i] < 0 + push @result, $list[$i]; + next; + } + } + if($exploded) { + return explode(@result); + } else { + return @result; + } +} + |
