From 1d1515048145eb2257eca3c6a02de75491e15848 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 4 Dec 2022 21:13:15 +1100 Subject: Simon's solution to challenge 193 --- challenge-193/sgreen/README.md | 4 ++-- challenge-193/sgreen/blog.txt | 1 + challenge-193/sgreen/perl/ch-1.pl | 19 ++++++++++++++++ challenge-193/sgreen/perl/ch-2.pl | 45 +++++++++++++++++++++++++++++++++++++ challenge-193/sgreen/python/ch-1.py | 18 +++++++++++++++ challenge-193/sgreen/python/ch-2.py | 39 ++++++++++++++++++++++++++++++++ 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 challenge-193/sgreen/blog.txt create mode 100755 challenge-193/sgreen/perl/ch-1.pl create mode 100755 challenge-193/sgreen/perl/ch-2.pl create mode 100755 challenge-193/sgreen/python/ch-1.py create mode 100755 challenge-193/sgreen/python/ch-2.py diff --git a/challenge-193/sgreen/README.md b/challenge-193/sgreen/README.md index 4a7b1a13f0..f9ff4adc2a 100644 --- a/challenge-193/sgreen/README.md +++ b/challenge-193/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 192 +# The Weekly Challenge 193 -Blog [Weekly Challenge 192](https://dev.to/simongreennet/weekly-challenge-192-30a6) +Blog [The odd binary string](https://dev.to/simongreennet/the-odd-binary-string-p64) diff --git a/challenge-193/sgreen/blog.txt b/challenge-193/sgreen/blog.txt new file mode 100644 index 0000000000..2678c6c5b5 --- /dev/null +++ b/challenge-193/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-odd-binary-string-p64 \ No newline at end of file diff --git a/challenge-193/sgreen/perl/ch-1.pl b/challenge-193/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..7776636d26 --- /dev/null +++ b/challenge-193/sgreen/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($n) { + # The format we want (a binary string of a given length) + my $fmt = "\%0${n}b"; + say $fmt; + # Make a list with all possible binary values + my @l = ( map { sprintf( $fmt, $_ ) } ( 0 .. 2**$n - 1 ) ); + + # Show the list + say join( ', ', @l ); +} + +main( $ARGV[0] ); \ No newline at end of file diff --git a/challenge-193/sgreen/perl/ch-2.pl b/challenge-193/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..851868d12c --- /dev/null +++ b/challenge-193/sgreen/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'mesh'; + +sub main (@s) { + # Create letter to number mapping + my %letter_map = mesh [ 'a' .. 'z' ], [ 0 .. 25 ]; + + my %occurrences = (); + foreach my $word (@s) { + # Calculate the differences between characters, and store this + # as a space separated string + my $diff = join( + ' ', + map { + $letter_map{ substr( $word, $_, 1 ) } - + $letter_map{ substr( $word, $_ - 1, 1 ) } + } ( 1 .. length($word) - 1 ) + ); + + # Add the word to the occurrences hash + push @{ $occurrences{$diff} }, $word; + } + + # Find the unique words + my @unique_words = + ( map { $_->[0] } grep { $#$_ == 0 } values(%occurrences) ); + + if ( $#unique_words > 0 ) { + say 'More than one unique string!'; + } + elsif ( $#unique_words == -1 ) { + say 'No unique strings!'; + } + else { + say $unique_words[0]; + } +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-193/sgreen/python/ch-1.py b/challenge-193/sgreen/python/ch-1.py new file mode 100755 index 0000000000..7497e1c61d --- /dev/null +++ b/challenge-193/sgreen/python/ch-1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # The format we want (a binary string of a given length) + fmt = f'0{n}b' + + # Make a list with all possible binary values + l = [format(x, fmt) for x in range(2**n)] + + # Show the list + print(*l, sep=', ') + + +if __name__ == '__main__': + main(int(sys.argv[1])) diff --git a/challenge-193/sgreen/python/ch-2.py b/challenge-193/sgreen/python/ch-2.py new file mode 100755 index 0000000000..cdc2d772f6 --- /dev/null +++ b/challenge-193/sgreen/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import string +import sys + + +def main(s): + # Create letter to number mapping + letter_map = {} + for k, v in enumerate(string.ascii_lowercase): + letter_map[v] = k + + occurrences = {} + for word in s: + # Calculate the differences between characters, and store this + # as a space separated string + diff = ' '.join([ + str(letter_map[word[i]] - letter_map[word[i-1]]) + for i in range(1, len(word)) + ]) + + # Add the word to the occurrences dict + if diff not in occurrences: + occurrences[diff] = [] + occurrences[diff].append(word) + + # Find the unique words + unique_words = [v[0] for v in occurrences.values() if len(v) == 1] + + if len(unique_words) > 1: + print('More than one unique string!') + elif len(unique_words) == 0: + print('No unique strings!') + else: + print(unique_words[0]) + + +if __name__ == '__main__': + main(sys.argv[1:]) -- cgit