aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-17 08:47:14 +0000
committerGitHub <noreply@github.com>2022-12-17 08:47:14 +0000
commit9a00c0db343f39d408c5abb673d06b7fc2f3f96f (patch)
tree33a8b48a514fdbcd40c184e037c1e5863c171d34
parentbd11b98b4bebd5b7dcd7bc0765fc37dc3678d02c (diff)
parent416feb177859e04c27a3318b5cc3afdbfeb9c832 (diff)
downloadperlweeklychallenge-club-9a00c0db343f39d408c5abb673d06b7fc2f3f96f.tar.gz
perlweeklychallenge-club-9a00c0db343f39d408c5abb673d06b7fc2f3f96f.tar.bz2
perlweeklychallenge-club-9a00c0db343f39d408c5abb673d06b7fc2f3f96f.zip
Merge pull request #7260 from LoneWolfiNTj/rh195
Rh195
-rwxr-xr-xchallenge-195/LoneWolfiNTj/perl/ch-1.pl52
-rwxr-xr-xchallenge-195/LoneWolfiNTj/perl/ch-2.pl75
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-195/LoneWolfiNTj/perl/ch-1.pl b/challenge-195/LoneWolfiNTj/perl/ch-1.pl
new file mode 100755
index 0000000000..1191c8cc60
--- /dev/null
+++ b/challenge-195/LoneWolfiNTj/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#! /usr/bin/perl
+
+# ch-1.pl
+# Robbie Hatley's Perl solution for Challenge 195-1
+
+=pod
+
+Task 1: Special Integers
+Submitted by: Mohammad S Anwar
+Given a positive integer $n, write a script to print the count
+of all "special" integers from 1 through $n. (An integer is
+"special" when all of its digits are unique.)
+Example 1: Input: 15 Output: 14
+Example 2: Input: 35 Output: 32
+
+=cut
+
+# NOTE: Input is via built-in array or command-line args.
+# If using args, they should be positive integers.
+
+# NOTE: Output is to stdout and will be each input number
+# $n followed by the count of all "special" integers
+# from 1 through $n.
+
+# PRELIMINARIES:
+use v5.36;
+
+# SUBROUTINES:
+
+# Is a string "special" (has no repeated characters)?
+sub is_special ($x)
+{
+ my $l = length($x); # number of characters
+ for my $i (0..$l-2){
+ for my $j ($i+1..$l-1){
+ substr($x,$i,1) eq substr($x,$j,1) and return 0}}
+ return 1;
+}
+
+# DEFAULT INPUT:
+my @numbers = (15, 35);
+
+# NON-DEFAULT INPUT:
+if (@ARGV) {@numbers = @ARGV;}
+
+# MAIN BODY OF SCRIPT:
+for my $n (@numbers)
+{
+ my $count = 0; # count of "special" integers in (1..$n).
+ for my $x (1..$n) {is_special($x) and ++$count}
+ say "There are $count special integers from 1 through $n."
+}
diff --git a/challenge-195/LoneWolfiNTj/perl/ch-2.pl b/challenge-195/LoneWolfiNTj/perl/ch-2.pl
new file mode 100755
index 0000000000..2d4594c7d1
--- /dev/null
+++ b/challenge-195/LoneWolfiNTj/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#! /usr/bin/perl
+
+# ch-2.pl
+# Robbie Hatley's Perl solution for Challenge 195-2
+
+=pod
+
+Task 2: Most Frequent Even
+Submitted by: Mohammad S Anwar
+Given a list of integers, write a script to find the most-frequent
+even number in the list. If you get more than one even number with
+the same frequency, return the smallest. If no even numbers are
+present, return -1.
+
+Example 1: Input: (1,1,2,6,2) Output: 2
+Example 2: Input: (1,3,5,7) Output: -1
+Example 3: Input: (6,4,4,6,1) Output: 4
+
+=cut
+
+# NOTE: Input is via built-in array of arrays, or command-line args.
+# If using args, they should be integers, and will be assumed
+# to be a single array.
+
+# NOTE: Output is to stdout and will be each input array followed by
+# smallest most-frequent even number, or -1 if no evens.
+
+# PRELIMINARIES:
+use v5.36;
+
+# DEFAULT INPUT:
+my @arrays =
+(
+ [1,1,2,6,2],
+ [1,3,5,7],
+ [6,4,4,6,1]
+);
+
+# NON-DEFAULT INPUT:
+if (@ARGV) {@arrays = ([@ARGV])}
+
+# MAIN BODY OF SCRIPT:
+for (@arrays)
+{
+ # Get array of numbers:
+ my @array = @{$_};
+
+ # Get hash of frequencies of even numbers:
+ my %freq;
+ for (@array) {++$freq{$_} unless $_ % 2}
+
+ # Get array of even numbers, in decreasing order of frequency:
+ my @evens = reverse sort {$freq{$a} <=> $freq{$b}} keys %freq;
+
+ # Find minimum even with highest frequency (or -1 if no evens):
+ my $min;
+ if (!@evens)
+ {
+ $min = -1;
+ }
+ else
+ {
+ $min = $evens[0];
+ for (@evens)
+ {
+ last if $freq{$_} < $freq{$evens[0]};
+ $min = $_ if $_ < $min;
+ }
+ }
+
+ # Display results:
+ say '';
+ say "array = @array";
+ say "smallest most-frequent even = $min";
+}