diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-28 23:03:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-28 23:03:50 +0100 |
| commit | 78cbb834d63ffb2ed1135abf8d981d6fec27617e (patch) | |
| tree | 1291866404f033827c07a106f1b52c6ed8b6afa5 | |
| parent | 5bc6ce02e03374b99919bcf2db5e278ae47c8994 (diff) | |
| parent | 78f1b20764ce5961ac74f9fcb1bed919d6a691a6 (diff) | |
| download | perlweeklychallenge-club-78cbb834d63ffb2ed1135abf8d981d6fec27617e.tar.gz perlweeklychallenge-club-78cbb834d63ffb2ed1135abf8d981d6fec27617e.tar.bz2 perlweeklychallenge-club-78cbb834d63ffb2ed1135abf8d981d6fec27617e.zip | |
Merge pull request #8955 from ianrifkin/ianrifkin-challenge-240
Ianrifkin challenge 240
| -rw-r--r-- | challenge-240/ianrifkin/README | 1 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/README.md | 83 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/python/ch-2.py | 19 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/raku/ch-1.raku | 29 | ||||
| -rw-r--r-- | challenge-240/ianrifkin/raku/ch-2.raku | 23 |
9 files changed, 242 insertions, 1 deletions
diff --git a/challenge-240/ianrifkin/README b/challenge-240/ianrifkin/README deleted file mode 100644 index 2d26297fb9..0000000000 --- a/challenge-240/ianrifkin/README +++ /dev/null @@ -1 +0,0 @@ -Solution by Ian Rifkin.
\ No newline at end of file diff --git a/challenge-240/ianrifkin/README.md b/challenge-240/ianrifkin/README.md new file mode 100644 index 0000000000..a3d7bf545f --- /dev/null +++ b/challenge-240/ianrifkin/README.md @@ -0,0 +1,83 @@ +# A.A.B.A. (Acronym And Build Array) + +Challenge 240: https://theweeklychallenge.org/blog/perl-weekly-challenge-240/ + +## Task 1: Acronym + +> You are given an array of strings and a check string. + +> Write a script to find out if the check string is the acronym of the words in the given array. + +Example 1 +``` +Input: @str = ("Perl", "Python", "Pascal") + $chk = "ppp" +Output: true +``` + +Example 2 +``` +Input: @str = ("Perl", "Raku") + $chk = "rp" +Output: false +``` + +Example 3 +``` +Input: @str = ("Oracle", "Awk", "C") + $chk = "oac" +Output: true +``` + +I am sure there are plenty of more interesting ways to solve this but the way my mind works is to create a real acronym by looping through each word in the in the input array to get the first letter. + +``` +sub check_acronym { + my ($acronym, $words) = @_; + my $real_acronym; + foreach (@{$words}) { + $real_acronym .= substr($_, 0, 1); + } +``` + +With that in place just compare the strings, forcing them both into uppercase so that it's a case insensitive comparison. You could also do this with a simple RegEx compare but this feels simpler to write/read to my brain. + +``` +uc($acronym) eq uc($real_acronym) ? print "true\n" : print "false\n"; +``` + +I did a similar approach with Raku and Python. With Perl the final print statement is a conditional so that it will print the words "true" or "false" instead of 0 or 1 but that's not needed with Python and Raku. + +## Task 2: Build Array + +> You are given an array of integers. + +> Write a script to create an array such that new[i] = old[old[i]] where 0 <= i < new.length. + +Example 1 +``` +Input: @int = (0, 2, 1, 5, 3, 4) +Output: (0, 1, 2, 4, 5, 3) +``` +Example 2 +``` +Input: @int = (5, 0, 1, 2, 3, 4) +Output: (4, 5, 0, 1, 2, 3) +``` + +Doing `new[i] = old[old[i]]` seemed simple enough but I do have trouble mentally processing `where 0 <= i < new.length`. If `i` is a postion of the input array it is always going to be equal to or greater than 0 because of how arrays are numbered. Since each new[i] is mapped to a value calculated from the old array they should naturally end up the same length, so `i` should never be longer than new.length because `i` shouldn't be less than old.length either. + +With that out of the way, I did a simple for loop using the iterator variable `$i` -- within the loop I am literally just doing the exact mapping from the question: `new[i] = old[old[i]]` + +``` +for (my $i = 0; $i < @ints; $i++) { + $new_ints[$i] = $ints[$ints[$i]]; +} +``` + +That's it! After the loop I print the new array in the desired format: +``` +print "(" . join(', ', @new_ints) . ")\n"; +``` + +This task sounded more complicated so I will be curious to see if others took a more interesting approach.
\ No newline at end of file diff --git a/challenge-240/ianrifkin/blog.txt b/challenge-240/ianrifkin/blog.txt new file mode 100644 index 0000000000..bc61bb3de3 --- /dev/null +++ b/challenge-240/ianrifkin/blog.txt @@ -0,0 +1 @@ +https://github.com/ianrifkin/perlweeklychallenge-club/blob/ianrifkin-challenge-240/challenge-240/ianrifkin/README.md diff --git a/challenge-240/ianrifkin/perl/ch-1.pl b/challenge-240/ianrifkin/perl/ch-1.pl new file mode 100644 index 0000000000..da88d64ca9 --- /dev/null +++ b/challenge-240/ianrifkin/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use warnings; +use strict; + +# You are given an array of strings and a check string. +# Write a script to find out if the check string is the acronym of the words in the given array. + +# Example 1 +my @words = ("Perl", "Python", "Pascal"); +my $acronym = "ppp"; +check_acronym($acronym, \@words); + +# Example 2 +@words = ("Perl", "Raku"); +$acronym = "rp"; +check_acronym($acronym, \@words); + +# Example 3 +@words = ("Oracle", "Awk", "C"); +$acronym = "oac"; +check_acronym($acronym, \@words); + +sub check_acronym { + my ($acronym, $words) = @_; + my $real_acronym; + foreach (@{$words}) { + $real_acronym .= substr($_, 0, 1); + } + uc($acronym) eq uc($real_acronym) ? print "true\n" : print "false\n"; +} diff --git a/challenge-240/ianrifkin/perl/ch-2.pl b/challenge-240/ianrifkin/perl/ch-2.pl new file mode 100644 index 0000000000..2763cee04c --- /dev/null +++ b/challenge-240/ianrifkin/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use warnings; +use strict; + +# You are given an array of integers. +# Write a script to create an array such that new[i] = old[old[i]] where 0 <= i < new.length. + + +# Example 1 +my @ints = (0, 2, 1, 5, 3, 4); +create_new_array(@ints); + +# Example 2 +@ints = (5, 0, 1, 2, 3, 4); +create_new_array(@ints); + +sub create_new_array { + my (@ints) = @_; + my @new_ints; + # Loop through old array to create new array + for (my $i = 0; $i < @ints; $i++) { + $new_ints[$i] = $ints[$ints[$i]]; + } + # Note on above: This should naturally meet the requirement to + # "where 0 <= i < new.length" unless I am confused + + # print new array matching desired formatting + print "(" . join(', ', @new_ints) . ")\n"; +} + diff --git a/challenge-240/ianrifkin/python/ch-1.py b/challenge-240/ianrifkin/python/ch-1.py new file mode 100644 index 0000000000..a7e88fddd5 --- /dev/null +++ b/challenge-240/ianrifkin/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/local/bin/python3 + +# You are given an array of strings and a check string. +# Write a script to find out if the check string is the acronym of the words in the given array. + +def check_acronym (acronym, words): + real_acronym = '' + for word in words: + real_acronym += word[0] + print(acronym.upper() == real_acronym.upper()) + +# Example 1 +words = ("Perl", "Python", "Pascal") +acronym = "ppp" +check_acronym(acronym, words) + +# Example 2 +words = ("Perl", "Raku") +acronym = "rp" +check_acronym(acronym, words) + +# Example 3 +words = ("Oracle", "Awk", "C") +acronym = "oac" +check_acronym(acronym, words) + + diff --git a/challenge-240/ianrifkin/python/ch-2.py b/challenge-240/ianrifkin/python/ch-2.py new file mode 100644 index 0000000000..1fe9ec38f7 --- /dev/null +++ b/challenge-240/ianrifkin/python/ch-2.py @@ -0,0 +1,19 @@ +#!/usr/local/bin/python3 + +# You are given an array of integers. +# Write a script to create an array such that new[i] = old[old[i]] where 0 <= i < new.length. + +def create_new_array (ints): + new_ints = [] + # Loop through old array to create new array + for i, item in enumerate(ints): + new_ints.insert(i, ints[ints[i]]) + print(new_ints) + +# Example 1 +ints = (0, 2, 1, 5, 3, 4) +create_new_array(ints) + +# Example 2 +ints = (5, 0, 1, 2, 3, 4) +create_new_array(ints) diff --git a/challenge-240/ianrifkin/raku/ch-1.raku b/challenge-240/ianrifkin/raku/ch-1.raku new file mode 100644 index 0000000000..31e840c93a --- /dev/null +++ b/challenge-240/ianrifkin/raku/ch-1.raku @@ -0,0 +1,29 @@ +use v6; + +# You are given an array of strings and a check string. +# Write a script to find out if the check string is the acronym of the words in the given array. + +# Example 1 +my @words = ("Perl", "Python", "Pascal"); +my $acronym = "ppp"; +check_acronym($acronym, @words); + +# Example 2 +@words = ("Perl", "Raku"); +$acronym = "rp"; +check_acronym($acronym, @words); + +# Example 3 +@words = ("Oracle", "Awk", "C"); +$acronym = "oac"; +check_acronym($acronym, @words); + +sub check_acronym { + my ($acronym, @words) = @_; + my $real_acronym = ''; + for (@words) { + $real_acronym ~= substr($_, 0, 1); + } + say(uc($acronym) eq uc($real_acronym)); +} + diff --git a/challenge-240/ianrifkin/raku/ch-2.raku b/challenge-240/ianrifkin/raku/ch-2.raku new file mode 100644 index 0000000000..497833aaf6 --- /dev/null +++ b/challenge-240/ianrifkin/raku/ch-2.raku @@ -0,0 +1,23 @@ +use v6; + +# You are given an array of integers. +# Write a script to create an array such that new[i] = old[old[i]] where 0 <= i < new.length. + +# Example 1 +my @ints = (0, 2, 1, 5, 3, 4); +create_new_array(@ints); + +# Example 2 +@ints = (5, 0, 1, 2, 3, 4); +create_new_array(@ints); + +sub create_new_array { + my (@ints) = @_; + my @new_ints; + # Loop through old array to create new array + for @ints.kv -> $index, $item { + @new_ints[$index] = @ints[@ints[$index]]; + } + say( "(" ~ join(', ', @new_ints) ~ ")\n" ); +} + |
