aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-08-23 12:47:33 +0100
committerGitHub <noreply@github.com>2020-08-23 12:47:33 +0100
commite0022c13f2c9687e671fed5071425368fd03d92f (patch)
tree25eb400d026def02818138776cc0f447d5f2af72
parent71fd465b4068f074590bfcba989648b2777216cd (diff)
parentf7b9c4b8040ca20100b70fd75243b265d97918a3 (diff)
downloadperlweeklychallenge-club-e0022c13f2c9687e671fed5071425368fd03d92f.tar.gz
perlweeklychallenge-club-e0022c13f2c9687e671fed5071425368fd03d92f.tar.bz2
perlweeklychallenge-club-e0022c13f2c9687e671fed5071425368fd03d92f.zip
Merge pull request #2127 from simongreen-net/swg-074
sgreen solutions to challenge 074
-rw-r--r--challenge-074/sgreen/README.md41
-rw-r--r--challenge-074/sgreen/blog.txt1
-rwxr-xr-xchallenge-074/sgreen/perl/ch-1.pl28
-rwxr-xr-xchallenge-074/sgreen/perl/ch-2.pl34
4 files changed, 88 insertions, 16 deletions
diff --git a/challenge-074/sgreen/README.md b/challenge-074/sgreen/README.md
index a272d6ba1c..83c8300508 100644
--- a/challenge-074/sgreen/README.md
+++ b/challenge-074/sgreen/README.md
@@ -1,30 +1,39 @@
-# Perl Weekly Challenge 073
+# Perl Weekly Challenge 074
Solution by Simon Green.
-## TASK #1 › Min Sliding Window
+## TASK #1 › Majority Element
-I try not to use CPAN modules, since there is no guarantee that the user running the code has it installed. However, [List::Util](https://metacpan.org/pod/List::Util) is part of the perl core these days, so it should be safe.
+This was a relatively straight forward task, with the following steps.
-The hardest part of the challenge was taking a string and converting it into an array of integers. Originally I had `split /[^\d\-]+/`, but this didn't work as expected as the first and value would be `undef` if the first character was not a number. Nothing that a quick [search on Stack Overflow](https://stackoverflow.com/questions/2884549/how-can-i-capture-multiple-matches-from-the-same-perl-regex) doesn't fix :)
-
-Once the input is processed, I then work through the array and get the [minimum value](https://metacpan.org/pod/List::Util#min) for each subset of the array.
+1. Count the number of times each elements occurs, and put it in a hash
+2. Go through the hash, and return the hash key if the hash value (number of times it appears) is greater than half the length of the list. We can also exit, since only one value can meet the condition.
+3. If we haven't exited, return '-1' as no value appears more than half the time.
### Examples
+ » ./ch-1.pl 1 2 2 3 2 4 2
+ 2
+
+ » ./ch-1.pl 1 3 1 2 4 5
+ -1
+
- » ./ch-1.pl "(1, 5, 0, 2, 9, 3, 7, 6, 4, 8)" 3
- 0, 0, 0, 2, 3, 3, 4, 4
+## TASK 2 › FNR Character
-## TASK 2 › Smallest Neighbour
+This taks requires a bit more thought about how to solve it. This of course is a good thing since we don't want all tasks to be too easy.
-Originally I didn't understand what was required, so I reached out to Mohammad to clarify the task. In particular, I couldn't understand how the third value in the first example was '0'. I should have read the task more clearly.
+This is how I attacked it. It will be interesting if other contributors took a different approach.
-Like with the first task, I walked through the list. If the current value was less than the previous minimum values, I would add a '0' to the result and set the new `$current_min` value. If it was not less, I would add the `$current_min` value to the result.
+1. Split the string into an array called `@letters`.
+2. Work through the list (left to right) from `0` to `$#letters`. `$#` is a short cut for one less than the length of the array, assuming you don't mess with `$[` (and you NEVER want to do that!)
+3. Add that letter to the `%used` array to count the number of times it is used.
+4. Work backwards (right to left) from the current letter to the first letter. For each letter, if it has been used once, print it. If no letters are found, print '#'
+5. Print the new line character `\n`
-## Example
+## Examples
- » ./ch-2.pl 7 8 3 10 12
- 0, 7, 0, 3, 3
+ » ./ch-2.pl ababc
+ abb#c
- » ./ch-2.pl 4 6 5
- 0, 4, 4 \ No newline at end of file
+ » ./ch-2.pl xyzzyx
+ xyzyx#
diff --git a/challenge-074/sgreen/blog.txt b/challenge-074/sgreen/blog.txt
new file mode 100644
index 0000000000..cce767c44d
--- /dev/null
+++ b/challenge-074/sgreen/blog.txt
@@ -0,0 +1 @@
+https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-074/sgreen/README.md
diff --git a/challenge-074/sgreen/perl/ch-1.pl b/challenge-074/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..d3c10cf11b
--- /dev/null
+++ b/challenge-074/sgreen/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use 5.10.1;
+
+sub main (@) {
+ my @list = @_;
+
+ # Count the number of times each values appear
+ my %count = ();
+ foreach my $item (@list) {
+ $count{$item}++;
+ }
+
+ # Go through the values and see if any appear more than half the times
+ while ( my ( $item, $count ) = each %count ) {
+ if ( $count > scalar(@list) / 2 ) {
+ say $item;
+ return;
+ }
+ }
+
+ # Nothing found, so return -1
+ say -1;
+}
+
+main(@ARGV);
diff --git a/challenge-074/sgreen/perl/ch-2.pl b/challenge-074/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..f29f6de845
--- /dev/null
+++ b/challenge-074/sgreen/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use 5.10.1;
+
+sub main (@) {
+ # Get the string, and separate it by letter
+ my $string = shift;
+ my @letters = split //, $string;
+
+ # Count the number of times each character is used
+ my %used = ();
+
+ # Go through the letters
+ for my $i ( 0 .. $#letters ) {
+ # Count the letter
+ $used{ $letters[$i] }++;
+
+ # Work backwards finding the first unique character
+ my $match = '#';
+ for ( my $j = $i ; $j >= 0 ; $j-- ) {
+ if ( $used{ $letters[$j] } == 1 ) {
+ $match = $letters[$j];
+ last;
+ }
+ }
+ print $match;
+ }
+ print "\n";
+}
+
+main(@ARGV);
+