aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-12-24 15:30:31 +1100
committerSimon Green <mail@simon.green>2022-12-24 15:30:31 +1100
commite2ca6572844d7b98a7c374ac74d87992a0dd6862 (patch)
tree83b042d86b5efd4a5235a15594c6a1a8cb598342
parentcfa0021bbaa682829341bf134823454b9c4d148f (diff)
downloadperlweeklychallenge-club-e2ca6572844d7b98a7c374ac74d87992a0dd6862.tar.gz
perlweeklychallenge-club-e2ca6572844d7b98a7c374ac74d87992a0dd6862.tar.bz2
perlweeklychallenge-club-e2ca6572844d7b98a7c374ac74d87992a0dd6862.zip
Simon's solution to challenge 195
-rw-r--r--challenge-195/sgreen/README.md4
-rw-r--r--challenge-195/sgreen/perl/ch-1.pl29
-rw-r--r--challenge-195/sgreen/perl/ch-2.pl31
-rwxr-xr-xchallenge-195/sgreen/python/ch-1.py31
-rwxr-xr-xchallenge-195/sgreen/python/ch-2.py29
5 files changed, 122 insertions, 2 deletions
diff --git a/challenge-195/sgreen/README.md b/challenge-195/sgreen/README.md
index f3687cb8b9..a12c4d2028 100644
--- a/challenge-195/sgreen/README.md
+++ b/challenge-195/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 194
+# The Weekly Challenge 195
-Blog [Digital frequency](https://dev.to/simongreennet/digital-frequency-l56)
+Sorry, no blog this week.
diff --git a/challenge-195/sgreen/perl/ch-1.pl b/challenge-195/sgreen/perl/ch-1.pl
new file mode 100644
index 0000000000..8cc80c83e5
--- /dev/null
+++ b/challenge-195/sgreen/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub is_simple_number ($n) {
+ # Determine if the number is simple
+ my %seen = ();
+ foreach my $i ( split //, $n ) {
+ return if $seen{$i}++;
+ }
+ # It is simple
+ return 1;
+}
+
+sub main ($n) {
+ # Iterate through the list counting the number of simple numbers
+ my $simple_count = 0;
+ foreach my $i ( 1 .. $n ) {
+ $simple_count++ if is_simple_number($i);
+ }
+
+ # Print the result
+ say $simple_count;
+}
+
+main( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-195/sgreen/perl/ch-2.pl b/challenge-195/sgreen/perl/ch-2.pl
new file mode 100644
index 0000000000..d830d8bc24
--- /dev/null
+++ b/challenge-195/sgreen/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+use List::Util qw(max min);
+
+sub main (@list) {
+ # Count the frequency of even numbers
+ my %evens = ();
+ foreach my $i (@list) {
+ $evens{$i}++ if $i % 2 == 0;
+ }
+
+ if ( scalar keys(%evens) == 0 ) {
+ # If no evens, return -1
+ say -1;
+ }
+ else {
+ # Find the evens that occur most often
+ my $m = max( values(%evens) );
+ my @max_evens = grep { $evens{$_} == $m } keys(%evens);
+
+ # The the minimum even that occurs most often
+ my $min_max_evens = min(@max_evens);
+ say $min_max_evens;
+ }
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-195/sgreen/python/ch-1.py b/challenge-195/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..4b3831a16c
--- /dev/null
+++ b/challenge-195/sgreen/python/ch-1.py
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+
+import sys
+
+
+def is_simple_number(n):
+ # Determine if the number is simple
+ seen = {}
+ for i in str(n):
+ if i in seen:
+ # We've seen the digit before. It's not simple
+ return False
+ seen[i] = 1
+
+ # It is simple
+ return True
+
+
+def main(n):
+ # Iterate through the list counting the number of simple numbers
+ simple_count = 0
+ for i in range(1, n+1):
+ if is_simple_number(i):
+ simple_count += 1
+
+ # Print the result
+ print(simple_count)
+
+
+if __name__ == '__main__':
+ main(int(sys.argv[1]))
diff --git a/challenge-195/sgreen/python/ch-2.py b/challenge-195/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..0ba621b63f
--- /dev/null
+++ b/challenge-195/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(array):
+ # Count the frequency of even numbers
+ evens = {}
+ for i in array:
+ if i % 2 == 0:
+ evens[i] = evens.get(i, 0) + 1
+
+ if not evens:
+ # If no evens, return -1
+ print(-1)
+ else:
+ # Find the evens that occur most often
+ m = max(evens.values())
+ max_evens = [i for i, v in evens.items() if v == m]
+
+ # The the minimum even that occurs most often
+ min_max_evens = min(max_evens)
+ print(min_max_evens)
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)