aboutsummaryrefslogtreecommitdiff
path: root/challenge-194
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2022-12-11 15:18:07 +1100
committerSimon Green <mail@simon.green>2022-12-11 15:18:07 +1100
commitcd0c1c2f37f184497d8dc4990d2ab50bf04a4052 (patch)
tree73b836480e9378cd12e49ae7e5e5bf4b3da7f971 /challenge-194
parent8d4ad39acceae6916068d7661648e075877837cc (diff)
downloadperlweeklychallenge-club-cd0c1c2f37f184497d8dc4990d2ab50bf04a4052.tar.gz
perlweeklychallenge-club-cd0c1c2f37f184497d8dc4990d2ab50bf04a4052.tar.bz2
perlweeklychallenge-club-cd0c1c2f37f184497d8dc4990d2ab50bf04a4052.zip
Simon's solution to challenge 194
Diffstat (limited to 'challenge-194')
-rw-r--r--challenge-194/sgreen/README.md4
-rw-r--r--challenge-194/sgreen/blog.txt1
-rw-r--r--challenge-194/sgreen/perl/ch-1.pl30
-rw-r--r--challenge-194/sgreen/perl/ch-2.pl46
-rwxr-xr-xchallenge-194/sgreen/python/ch-1.py23
-rwxr-xr-xchallenge-194/sgreen/python/ch-2.py39
6 files changed, 141 insertions, 2 deletions
diff --git a/challenge-194/sgreen/README.md b/challenge-194/sgreen/README.md
index f9ff4adc2a..f3687cb8b9 100644
--- a/challenge-194/sgreen/README.md
+++ b/challenge-194/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 193
+# The Weekly Challenge 194
-Blog [The odd binary string](https://dev.to/simongreennet/the-odd-binary-string-p64)
+Blog [Digital frequency](https://dev.to/simongreennet/digital-frequency-l56)
diff --git a/challenge-194/sgreen/blog.txt b/challenge-194/sgreen/blog.txt
new file mode 100644
index 0000000000..ca6832d6c1
--- /dev/null
+++ b/challenge-194/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/digital-frequency-l56 \ No newline at end of file
diff --git a/challenge-194/sgreen/perl/ch-1.pl b/challenge-194/sgreen/perl/ch-1.pl
new file mode 100644
index 0000000000..24e6929741
--- /dev/null
+++ b/challenge-194/sgreen/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main($time) {
+ my $value = '';
+ if (substr($time, 0, 1) eq '?') {
+ # 2 if the second hour value is <= 3 else 1
+ $value = substr(time, 1, 1) <= 3 ? 2 : 1;
+ }
+ elsif (substr(time, 1, 1) eq '?') {
+ # 3 if the first hour value is 2 else 9
+ $value = substr(time, 1, 1) == 2 ? 3 : 9;
+ }
+ elsif (substr(time, 3, 1) eq '?') {
+ # The maximum first minute value is always 5
+ $value = 5;
+ }
+ else {
+ # The maximum second minute value is always 9
+ $value = 9;
+ }
+
+ say $value;
+}
+
+main($ARGV[0]); \ No newline at end of file
diff --git a/challenge-194/sgreen/perl/ch-2.pl b/challenge-194/sgreen/perl/ch-2.pl
new file mode 100644
index 0000000000..e507fe4963
--- /dev/null
+++ b/challenge-194/sgreen/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main($s) {
+ # Calculate the frequency of each letter
+ my %letter_freq = ();
+ foreach my $letter (split //, $s) {
+ $letter_freq{$letter} += 1;
+ }
+
+ # Calculate the frequency of frequencies :)
+ my %freq = ();
+ foreach my $i (values(%letter_freq)) {
+ $freq{$i} += 1;
+ }
+
+ my $solution = 0;
+ # A solution is only possible if there are two different frequencies ...
+ if (scalar keys(%freq) == 2) {
+ my $min_freq = min(keys(%freq));
+ my $max_freq = max(keys(%freq));
+
+ # ... and the minimum frequency only occurs once,
+ if ($min_freq == 1 and $freq{1} == 1) {
+ $solution = 1;
+ }
+
+ # ... or the difference between them is 1, and the higher frequency
+ # only occurs once.
+ elsif ($min_freq == $max_freq - 1 and $freq{$max_freq} == 1) {
+ $solution = 1;
+ }
+ }
+ elsif (scalar keys(%freq) == 1 && exists($freq{1})) {
+ # ... or the only thing we have is single letters
+ $solution = 1;
+ }
+
+ say $solution;
+}
+
+main($ARGV[0]) \ No newline at end of file
diff --git a/challenge-194/sgreen/python/ch-1.py b/challenge-194/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..add4e38257
--- /dev/null
+++ b/challenge-194/sgreen/python/ch-1.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python3
+
+import sys
+
+def main(time):
+ value = None
+ if time[0] == '?':
+ # 2 if the second hour value is <= 3 else 1
+ value = 2 if int(time[1]) <= 3 else 1
+ elif time[1] == '?':
+ # 3 if the first hour value is 2 else 9
+ value = 3 if time[0] == '2' else 9
+ elif time[3] == '?':
+ # The maximum first minute value is always 5
+ value = 5
+ else:
+ # The maximum second minute value is always 9
+ value = 9
+
+ print(value)
+
+if __name__ == '__main__':
+ main(sys.argv[1]) \ No newline at end of file
diff --git a/challenge-194/sgreen/python/ch-2.py b/challenge-194/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..dfa570c4cf
--- /dev/null
+++ b/challenge-194/sgreen/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(s):
+ # Calculate the frequency of each letter
+ letter_freq = {}
+ for letter in s:
+ letter_freq[letter] = letter_freq.get(letter, 0) + 1
+
+ # Calculate the frequency of frequencies :)
+ freq = {}
+ for i in letter_freq.values():
+ freq[i] = freq.get(i, 0) + 1
+
+ solution = 0
+ # A solution is only possible if there are two different frequencies ...
+ if len(freq) == 2:
+ min_freq = min(freq.keys())
+ max_freq = max(freq.keys())
+
+ # ... and the minimum frequency only occurs once,
+ if min_freq == 1 and freq[1] == 1:
+ solution = 1
+
+ # ... or the difference between them is 1, and the higher frequency
+ # only occurs once.
+ elif min_freq == max_freq - 1 and freq[max_freq] == 1:
+ solution = 1
+ elif len(freq) == 1 and 1 in freq:
+ # ... or the only thing we have is single letters
+ solution = 1
+
+ print(solution)
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])