diff options
| author | Simon Green <mail@simon.green> | 2023-12-31 23:48:43 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2023-12-31 23:48:43 +1100 |
| commit | d9ce532fa5b8cdc38662dab6b8347489820e38ca (patch) | |
| tree | e0335cff14b3715c7e00cd38ccc2de344bcd53bc | |
| parent | 6ba11a7b1f5ede2a9ce66870c6ed091fde9ad40b (diff) | |
| download | perlweeklychallenge-club-d9ce532fa5b8cdc38662dab6b8347489820e38ca.tar.gz perlweeklychallenge-club-d9ce532fa5b8cdc38662dab6b8347489820e38ca.tar.bz2 perlweeklychallenge-club-d9ce532fa5b8cdc38662dab6b8347489820e38ca.zip | |
Simon's solution to challenge 249
| -rw-r--r-- | challenge-249/sgreen/README.md | 4 | ||||
| -rwxr-xr-x | challenge-249/sgreen/perl/ch-1.pl | 31 | ||||
| -rwxr-xr-x | challenge-249/sgreen/perl/ch-2.pl | 30 | ||||
| -rwxr-xr-x | challenge-249/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-249/sgreen/python/ch-2.py | 27 |
5 files changed, 115 insertions, 3 deletions
diff --git a/challenge-249/sgreen/README.md b/challenge-249/sgreen/README.md index 296c098161..d2fd2ac699 100644 --- a/challenge-249/sgreen/README.md +++ b/challenge-249/sgreen/README.md @@ -1,3 +1 @@ -# The Weekly Challenge 247 - -Blog: [A string, a character and a matrix...](https://dev.to/simongreennet/a-string-a-character-and-a-matrix-5bc9) +# The Weekly Challenge 249 diff --git a/challenge-249/sgreen/perl/ch-1.pl b/challenge-249/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..11cdc03be6 --- /dev/null +++ b/challenge-249/sgreen/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'any'; + +sub main (@ints) { + my %freq = (); + foreach my $i (@ints) { + ++$freq{$i}; + } + + if (any {$_ % 2 == 1 } values(%freq)) { + say '()'; + return; + } + + my @solution = (); + foreach my $i (keys %freq) { + foreach (1 .. $freq{$i} / 2) { + push @solution, "($i, $i)"; + } + } + + say join ', ', @solution; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-249/sgreen/perl/ch-2.pl b/challenge-249/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..1e1fbb2c40 --- /dev/null +++ b/challenge-249/sgreen/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($s) { + # Start at zero + my $d_count = 0; + my $i_count = 0; + my @solution = ( 0 ); + + foreach my $c (split //, $s) { + if ($c eq 'D') { + # We want a lower number + push @solution, --$d_count; + } + else { + # We want a higher number + push @solution, ++$i_count; + } + } + + # Rebase the list to start with zero + @solution = map { $_ - $d_count } @solution; + say join ', ', @solution; +} + +main($ARGV[0]);
\ No newline at end of file diff --git a/challenge-249/sgreen/python/ch-1.py b/challenge-249/sgreen/python/ch-1.py new file mode 100755 index 0000000000..8b419eff83 --- /dev/null +++ b/challenge-249/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def main(ints): + freq = {} + for i in ints: + freq[i] = freq.get(i, 0) + 1 + + if any(True for f in freq if freq[f] % 2 == 1): + print('()') + return + + solution = [] + for i in freq: + for _ in range(freq[i]//2): + solution.append(f'({i}, {i})') + + print(*solution, sep=', ') + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) diff --git a/challenge-249/sgreen/python/ch-2.py b/challenge-249/sgreen/python/ch-2.py new file mode 100755 index 0000000000..313912db48 --- /dev/null +++ b/challenge-249/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def main(s): + # Start at zero + d_count = 0 + i_count = 0 + solution = [ 0 ] + + for c in s: + if c == 'D': + # We want a lower number + d_count -= 1 + solution.append(d_count) + else: + # We want a higher number + i_count += 1 + solution.append(i_count) + + # Rebase the list to start with zero + solution = map(lambda i: i - d_count, solution) + print(*solution, sep = ', ') + +if __name__ == '__main__': + main(sys.argv[1]) |
