aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-09 21:04:06 +0100
committerGitHub <noreply@github.com>2023-05-09 21:04:06 +0100
commit189470d0cd197ae7a5dc90857102c11e8992f564 (patch)
tree9208787cd3daf06f721e3fd899b7ee935bedfc0f
parent3171a82cd469a9b4c591ccad5d3fe76d3abbb856 (diff)
parent21822c27d35f4472b29174740f233cc1378827b5 (diff)
downloadperlweeklychallenge-club-189470d0cd197ae7a5dc90857102c11e8992f564.tar.gz
perlweeklychallenge-club-189470d0cd197ae7a5dc90857102c11e8992f564.tar.bz2
perlweeklychallenge-club-189470d0cd197ae7a5dc90857102c11e8992f564.zip
Merge pull request #8044 from manfredi/challenge-216
Perl and Python Solutions for Task #1
-rwxr-xr-xchallenge-216/manfredi/perl/ch-1.pl50
-rwxr-xr-xchallenge-216/manfredi/python/ch-1.py39
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-216/manfredi/perl/ch-1.pl b/challenge-216/manfredi/perl/ch-1.pl
new file mode 100755
index 0000000000..9bcf6ef831
--- /dev/null
+++ b/challenge-216/manfredi/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use v5.36;
+
+say "challenge-216-task1";
+
+# Task 1: Registration Number
+# You are given a list of words and a random registration number.
+# Write a script to find all the words in the given list that has every letter in the given registration number.
+
+# Example 1
+# Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD'
+# Output: ('abcd')
+
+# Example 2
+# Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB'
+# Output: ('job', 'bjorg')
+
+# Example 3
+# Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2'
+# Output: ('crack', 'rac')
+
+sub registration_number {
+ my $reg = shift;
+ my @words = @{+shift};
+ my @out = ();
+ say "Input: \@words = (@words) , \$reg = '$reg'";
+ my %hreg = ();
+ my @hreg = grep { /[a-z]/ } split //, lc $reg;
+ $hreg{$_} = 1 for @hreg;
+ for my $word (@words) {
+ my %hword = ();
+ $hword{$_} = 1 for (split //, lc $word);
+ my @i = grep { exists $hreg{$_} } keys %hword;
+ push @out, $word if $#i == $#hreg;
+ }
+ say "Output: (@out)\n";
+}
+
+while (<DATA>) {
+ chomp;
+ my ($reg, @words) = split ',';
+ registration_number($reg, \@words);
+}
+
+
+__DATA__
+AB1 2CD,abc,abcd,bcd
+007 JB,job,james,bjorg
+C7 RA2,crack,road,rac
diff --git a/challenge-216/manfredi/python/ch-1.py b/challenge-216/manfredi/python/ch-1.py
new file mode 100755
index 0000000000..fcee0dbc5d
--- /dev/null
+++ b/challenge-216/manfredi/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+# Python 3.9.2 on Debian GNU/Linux 11 (bullseye)
+
+print('challenge-216-task1')
+
+# Task 1: Registration Number
+# You are given a list of words and a random registration number.
+# Write a script to find all the words in the given list that has every letter in the given registration number.
+
+# Example 1
+# Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD'
+# Output: ('abcd')
+
+# Example 2
+# Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB'
+# Output: ('job', 'bjorg')
+
+# Example 3
+# Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2'
+# Output: ('crack', 'rac')
+
+def registration_number(reg: str, words: list[str]) -> list[str]:
+ out = []
+ print(f"Input: words = ({words}), reg = '{reg}'")
+ r = set( [ i for i in reg.lower() if i.isalpha() ])
+ for word in words:
+ w = set(word.lower())
+ if r.issubset(w): out.append(word)
+ return out
+
+
+def main():
+ print("Output: ", registration_number('AB1 2CD', ['abc', 'abcd', 'bcd']), "\n")
+ print("Output: ", registration_number('007 JB', ['job', 'james', 'bjorg']), "\n")
+ print("Output: ", registration_number('C7 RA2', ['crack', 'road', 'rac']), "\n")
+
+
+if __name__ == '__main__':
+ main()