aboutsummaryrefslogtreecommitdiff
path: root/challenge-216
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-10 11:50:45 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-10 11:50:45 +0100
commit722527ed475e56e5717e60f8d3b52d9bbcef492c (patch)
tree81befdc86fae021847066ce54b4a56d554c45ec3 /challenge-216
parentc265c11df5b158f65eb8699298fd6a9b89467ce7 (diff)
downloadperlweeklychallenge-club-722527ed475e56e5717e60f8d3b52d9bbcef492c.tar.gz
perlweeklychallenge-club-722527ed475e56e5717e60f8d3b52d9bbcef492c.tar.bz2
perlweeklychallenge-club-722527ed475e56e5717e60f8d3b52d9bbcef492c.zip
- Added solutions by Lubos Kolouch.
- Added solutions by Robert DiCicco. - Added solutions by W. Luis Mochan.
Diffstat (limited to 'challenge-216')
-rw-r--r--challenge-216/lubos-kolouch/perl/ch-1.pl31
-rw-r--r--challenge-216/lubos-kolouch/perl/ch-2.pl63
-rw-r--r--challenge-216/lubos-kolouch/python/ch-1.py22
-rw-r--r--challenge-216/lubos-kolouch/python/ch-2.py60
-rw-r--r--challenge-216/robert-dicicco/julia/ch-1.jl61
-rw-r--r--challenge-216/robert-dicicco/perl/ch-1.pl61
-rw-r--r--challenge-216/robert-dicicco/python/ch-1.py48
-rw-r--r--challenge-216/robert-dicicco/raku/ch-1.raku58
-rw-r--r--challenge-216/robert-dicicco/ruby/ch-1.rb60
9 files changed, 464 insertions, 0 deletions
diff --git a/challenge-216/lubos-kolouch/perl/ch-1.pl b/challenge-216/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..c0b3f01798
--- /dev/null
+++ b/challenge-216/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub matching_words {
+ my ($words, $reg) = @_;
+ $reg = uc($reg);
+ my %letters;
+ for my $letter (grep {/[A-Z]/} split //, $reg) {
+ $letters{$letter} = 1;
+ }
+ my @matches;
+ for my $word (@$words) {
+ my $upper_word = uc($word);
+ my $matched = 1;
+ for my $letter (keys %letters) {
+ unless (index($upper_word, $letter) != -1) {
+ $matched = 0;
+ last;
+ }
+ }
+ push @matches, $word if $matched;
+ }
+ return \@matches;
+}
+
+my @words = ('job', 'james', 'bjorg');
+my $reg = '007 JB';
+my $matches = matching_words(\@words, $reg);
+print "(", join(", ", map { "'$_'" } @$matches), ")\n";
+
diff --git a/challenge-216/lubos-kolouch/perl/ch-2.pl b/challenge-216/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..f439b78eed
--- /dev/null
+++ b/challenge-216/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,63 @@
+use strict;
+use warnings;
+use List::Util qw(min);
+use List::MoreUtils qw(any);
+
+sub min_stickers_needed {
+ my ( $stickers, $target ) = @_;
+ my %target_counts;
+ $target_counts{$_}++ for split //, $target;
+ my @stickers_counts = map { my %cnt; $cnt{$_}++ for split //, $_; \%cnt } @$stickers;
+
+ # Filter out stickers that don't have any characters in common with the target word
+ my @filtered_stickers_counts = grep {
+ my $sticker = $_;
+ any { exists $sticker->{$_} } keys %target_counts
+ } @stickers_counts;
+
+ return min_stickers_helper( \@filtered_stickers_counts, \%target_counts, 0 );
+}
+
+sub min_stickers_helper {
+ my ( $stickers_counts, $target_counts, $used_stickers ) = @_;
+
+ return $used_stickers unless keys %$target_counts;
+
+ my $min_stickers = 'inf';
+ for my $sticker_counts (@$stickers_counts) {
+
+ # Try to fulfill the remaining character requirements of the target word
+ my %new_target_counts = %$target_counts;
+ my $used_current_sticker = 0;
+ for my $char ( keys %$sticker_counts ) {
+ if ( $new_target_counts{$char} ) {
+ $new_target_counts{$char} -= $sticker_counts->{$char};
+ delete $new_target_counts{$char} if $new_target_counts{$char} <= 0;
+ $used_current_sticker = 1;
+ }
+ }
+
+ if ($used_current_sticker) {
+ $min_stickers = min( $min_stickers,
+ min_stickers_helper( $stickers_counts, \%new_target_counts, $used_stickers + 1 ) );
+ }
+ }
+
+ return $min_stickers == 'inf' ? -1 : $min_stickers;
+}
+
+my $stickers = [ 'perl', 'raku', 'python' ];
+my $word = 'peon';
+print min_stickers_needed( $stickers, $word ), "\n"; # Output: 2
+
+$stickers = [ 'love', 'hate', 'angry' ];
+$word = 'goat';
+print min_stickers_needed( $stickers, $word ), "\n"; # Output: 3
+
+$stickers = [ 'come', 'nation', 'delta' ];
+$word = 'accommodation';
+print min_stickers_needed( $stickers, $word ), "\n"; # Output: 4
+
+$stickers = [ 'come', 'country', 'delta' ];
+$word = 'accommodation';
+print min_stickers_needed( $stickers, $word ), "\n"; # Output: -1
diff --git a/challenge-216/lubos-kolouch/python/ch-1.py b/challenge-216/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..c52a4df31f
--- /dev/null
+++ b/challenge-216/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def matching_words(words: List[str], reg: str) -> List[str]:
+ reg = reg.upper()
+ letters = set(letter for letter in reg if letter.isalpha())
+ matches = []
+ for word in words:
+ upper_word = word.upper()
+ matched = all(letter in upper_word for letter in letters)
+ if matched:
+ matches.append(word)
+ return matches
+
+
+words = ['job', 'james', 'bjorg']
+reg = '007 JB'
+matches_list = matching_words(words, reg)
+print("(", ", ".join(f"'{match}'" for match in matches_list), ")")
diff --git a/challenge-216/lubos-kolouch/python/ch-2.py b/challenge-216/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..9cb331adf1
--- /dev/null
+++ b/challenge-216/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,60 @@
+from collections import Counter
+import sys
+
+
+def min_stickers_needed(stickers, target):
+ target_counts = Counter(target)
+ stickers_counts = [Counter(sticker) for sticker in stickers]
+
+ # Filter out stickers that don't have any characters in common with the target word
+ filtered_stickers_counts = [
+ sticker
+ for sticker in stickers_counts
+ if any(sticker[char] > 0 for char in target_counts)
+ ]
+
+ return min_stickers_helper(filtered_stickers_counts, target_counts, 0)
+
+
+def min_stickers_helper(stickers_counts, target_counts, used_stickers):
+ if not target_counts:
+ return used_stickers
+
+ min_stickers = sys.maxsize
+ for sticker_counts in stickers_counts:
+ # Try to fulfill the remaining character requirements of the target word
+ new_target_counts = target_counts.copy()
+ used_current_sticker = False
+ for char, count in sticker_counts.items():
+ if new_target_counts[char] > 0:
+ new_target_counts[char] -= count
+ if new_target_counts[char] <= 0:
+ del new_target_counts[char]
+ used_current_sticker = True
+
+ if used_current_sticker:
+ min_stickers = min(
+ min_stickers,
+ min_stickers_helper(
+ stickers_counts, new_target_counts, used_stickers + 1
+ ),
+ )
+
+ return min_stickers if min_stickers != sys.maxsize else 0
+
+
+stickers = ["perl", "raku", "python"]
+word = "peon"
+print(min_stickers_needed(stickers, word)) # Output: 2
+
+stickers = ["love", "hate", "angry"]
+word = "goat"
+print(min_stickers_needed(stickers, word)) # Output: 3
+
+stickers = ["come", "nation", "delta"]
+word = "accommodation"
+print(min_stickers_needed(stickers, word)) # Output: 4
+
+stickers = ["come", "country", "delta"]
+word = "accommodation"
+print(min_stickers_needed(stickers, word)) # Output: -1
diff --git a/challenge-216/robert-dicicco/julia/ch-1.jl b/challenge-216/robert-dicicco/julia/ch-1.jl
new file mode 100644
index 0000000000..477f717ce3
--- /dev/null
+++ b/challenge-216/robert-dicicco/julia/ch-1.jl
@@ -0,0 +1,61 @@
+#!/usr/bin/env julia
+#=
+--------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-05-08
+Challenge 216 Registration Number ( Julia )
+--------------------------------------------
+=#
+using Printf
+
+words = [["abc", "abcd", "bcd", "AB1 2CD"], ["job", "james", "bjorg", "007 JB"], ["crack", "road", "rac", "C7 RA2"]]
+
+flag = 0
+out = []
+
+function CheckWords(wd, rg)
+ arr = split(rg,"")
+ flag = 0
+ for lett in arr
+ if ! occursin(lett, wd)
+ flag = 1
+ end
+ end
+ if flag == 0
+ push!(out, wd)
+ end
+end
+
+for wds in words
+ global out
+ len = length(wds)
+
+ reg = wds[end]
+ @printf("Input: @words = %s, \$reg = %s\n", wds[begin:end-1],reg)
+ r = r"0|1|2|3|4|5|6|7|8|9| "
+ reg = lowercase(replace(reg,r => "" ))
+ x = 1
+ while x < len
+ CheckWords(wds[x], reg)
+ x += 1
+ end
+ println("Output: ["*join(out,",")*"]\n")
+ out = []
+end
+
+#=
+--------------------------------------------
+SAMPLE OUTPUT
+julia .\Registration.jl
+Input: @words = ["abc", "abcd", "bcd"], $reg = AB1 2CD
+Output: [abcd]
+
+Input: @words = ["job", "james", "bjorg"], $reg = 007 JB
+Output: [job,bjorg]
+
+Input: @words = ["crack", "road", "rac"], $reg = C7 RA2
+Output: [crack,rac]
+--------------------------------------------
+=#
+
+
diff --git a/challenge-216/robert-dicicco/perl/ch-1.pl b/challenge-216/robert-dicicco/perl/ch-1.pl
new file mode 100644
index 0000000000..4450672b23
--- /dev/null
+++ b/challenge-216/robert-dicicco/perl/ch-1.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+=begin comment
+--------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-05-08
+Challenge 216 Registration Number ( Perl )
+--------------------------------------------
+=cut
+use strict;
+use warnings;
+use feature 'say';
+
+my @words = (['abc', 'abcd', 'bcd', 'AB1 2CD'], ['job', 'james', 'bjorg', '007 JB'], ['crack', 'road', 'rac', 'C7 RA2']);
+
+my @out = ();
+
+sub CheckWords {
+ my $wd = shift; # word to be checked
+ my $rg = shift; # registration
+ my @arr = split("",$rg); # split the registration into chars
+ my $flag = 0; # the match flag, set to 1 if reg char is not in word
+ foreach my $let (@arr) {
+ if ($wd !~ /$let/) {
+ $flag = 1; # set flag to 1 if no match
+ }
+ }
+ push(@out, $wd) if ($flag == 0); # save word if all match
+}
+
+for (@words) {
+ my @wds = @{$_}; # get list of words
+ my $len = scalar @wds; # and its length
+ print("Input: @wds[0..($len-2)], ");
+ my $reg = $wds[$len - 1]; # registration is last entry in words
+ print(" \$reg = $reg\n");
+ $reg =~ tr/A-Z/a-z/; # convert all to lower case
+ $reg =~ tr/0-9 //d; # remove spaces and numbers
+ for (my $x = 0; $x < $len - 1;$x++) {
+ CheckWords($wds[$x],$reg);
+ }
+ print("Output: @out\n\n");
+ @out = ();
+ }
+
+=begin comment
+ --------------------------------------------
+ SAMPLE OUTPUT
+perl .\Registration.pl
+Input: abc abcd bcd, $reg = AB1 2CD
+Output: abcd
+
+Input: job james bjorg, $reg = 007 JB
+Output: job bjorg
+
+Input: crack road rac, $reg = C7 RA2
+Output: crack rac
+--------------------------------------------
+=cut
+
+
+
diff --git a/challenge-216/robert-dicicco/python/ch-1.py b/challenge-216/robert-dicicco/python/ch-1.py
new file mode 100644
index 0000000000..1e3344bd90
--- /dev/null
+++ b/challenge-216/robert-dicicco/python/ch-1.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+# ------------------------------------------
+# AUTHOR: Robert DiCicco
+# DATE : 2023-05-09
+# Challenge 216 Registration Number ( Python )
+# ------------------------------------------
+
+words = [["abc", "abcd", "bcd", "AB1 2CD"], ["job", "james", "bjorg", "007 JB"], ["crack", "road", "rac", "C7 RA2"]]
+out = []
+
+def CheckWords(wd, rg):
+ flag = 0
+ arr = [x for x in rg]
+ for lett in arr:
+ if wd.count(lett) == 0:
+ flag = 1
+ if flag == 0:
+ out.append(wd)
+
+for wds in words:
+ wds_only = wds[0:-1]
+ reg = wds[-1]
+ print("Input: ",wds_only,", $reg = ", reg)
+ reg = reg.translate({ord(i): None for i in '1234567890 '}).lower()
+ x = 0
+ cnt = len(wds) - 1
+ while x < cnt:
+ CheckWords(wds[x],reg)
+ x += 1
+ print("Output: ",out,"\n")
+ out = []
+
+# ------------------------------------------
+# SAMPLE OUTPUT
+# python .\Registration.py
+# Input: ['abc', 'abcd', 'bcd'] , $reg = AB1 2CD
+# Output: ['abcd']
+
+# Input: ['job', 'james', 'bjorg'] , $reg = 007 JB
+# Output: ['job', 'bjorg']
+
+# Input: ['crack', 'road', 'rac'] , $reg = C7 RA2
+# Output: ['crack', 'rac']
+# ------------------------------------------
+
+
+
+
diff --git a/challenge-216/robert-dicicco/raku/ch-1.raku b/challenge-216/robert-dicicco/raku/ch-1.raku
new file mode 100644
index 0000000000..3be62a7fea
--- /dev/null
+++ b/challenge-216/robert-dicicco/raku/ch-1.raku
@@ -0,0 +1,58 @@
+#!/usr/bin/env raku
+=begin comment
+--------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-05-08
+Challenge 216 Registration Number ( Raku )
+--------------------------------------------
+=end comment
+
+use v6;
+
+my @words = ('abc', 'abcd', 'bcd', 'AB1 2CD'), ('job', 'james', 'bjorg', '007 JB'), ('crack', 'road', 'rac', 'C7 RA2');
+my @out = ();
+
+sub CheckWords($wd, $rg) {
+ my @arr = $rg.comb;
+ my $flag = 0;
+ for (@arr) -> $let {
+ if ($wd !~~ /$let/) {
+ $flag = 1; # set flag to 1 if no match
+ }
+ }
+ @out.push($wd) if $flag == 0;
+}
+
+
+for (@words) -> @wds {
+ my $reg = @wds.[*-1];
+ my $cnt = @wds.elems - 2;
+ my @wds_only = @wds[0..$cnt];
+ print "Input: [",@wds_only,"] \$reg = ",$reg,"\n";
+ $reg ~~ tr/A..Z/a..z/;
+ $reg ~~ s:g/\d|\s//; # remove spaces and numbers
+ my $x = 0;
+ while $x <= $cnt {
+ CheckWords(@wds[$x], $reg);
+ $x += 1;
+ }
+ print("Output: ",@out,"\n\n");
+ @out = ();
+}
+
+=begin comment
+--------------------------------------------
+SAMPLE OUTPUT
+raku .\Registration.rk
+Input: [abc abcd bcd] $reg = AB1 2CD
+Output: abcd
+
+Input: [job james bjorg] $reg = 007 JB
+Output: job bjorg
+
+Input: [crack road rac] $reg = C7 RA2
+Output: crack rac
+--------------------------------------------
+=end comment
+
+
diff --git a/challenge-216/robert-dicicco/ruby/ch-1.rb b/challenge-216/robert-dicicco/ruby/ch-1.rb
new file mode 100644
index 0000000000..fe5cb76e9d
--- /dev/null
+++ b/challenge-216/robert-dicicco/ruby/ch-1.rb
@@ -0,0 +1,60 @@
+#!/usr/bin/env ruby
+=begin
+--------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 2023-05-08
+Challenge 216 Registration Number ( Ruby )
+--------------------------------------------
+=end
+
+@words = [['abc', 'abcd', 'bcd', 'AB1 2CD'], ['job', 'james', 'bjorg', '007 JB'], ['crack', 'road', 'rac', 'C7 RA2']]
+
+$out = []
+def CheckWords(wd, rg)
+ arr = rg.split(//)
+ flag = 0
+ arr.each do |lett|
+ if ! wd.include? lett
+ flag = 1; # set flag to 1 if no match
+ end
+ end
+ if flag == 0
+ $out.push(wd)
+ end
+end
+
+@words.each do |wds|
+ $out = []
+ len = wds.length()
+ wds_only = wds.slice(0,len-1)
+ reg = wds.slice(-1,1)
+ reg = reg.to_s
+ reg.delete! '[]"0123456789 '
+ reg = reg.to_s.tr('A-Z', 'a-z')
+ puts("Input: @words = #{wds_only} $reg = #{reg}")
+ x = 0
+ cnt = len - 2
+ while x <= cnt
+ CheckWords(wds_only[x], reg)
+ x += 1
+ end
+ puts("Output: #{$out}")
+ puts('')
+end
+
+=begin
+--------------------------------------------
+SAMPLE OUTPUT
+ruby .\Registration.rb
+Input: @words = ["abc", "abcd", "bcd"] $reg = abcd
+Output: ["abcd"]
+
+Input: @words = ["job", "james", "bjorg"] $reg = jb
+Output: ["job", "bjorg"]
+
+Input: @words = ["crack", "road", "rac"] $reg = cra
+Output: ["crack", "rac"]
+--------------------------------------------
+=end
+
+