diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-12-17 15:12:25 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-17 15:12:25 +0000 |
| commit | bf69018ef17e5cbf1d02acdc71d29016aef4abc7 (patch) | |
| tree | d2e5e91d7f7fca86f052cc481d06d2e93414b2a2 | |
| parent | 59354579c0cb166a9df5641f0108b199a40f8fe9 (diff) | |
| parent | a388d97f03af1520294feb0b5b8a65ff9331f726 (diff) | |
| download | perlweeklychallenge-club-bf69018ef17e5cbf1d02acdc71d29016aef4abc7.tar.gz perlweeklychallenge-club-bf69018ef17e5cbf1d02acdc71d29016aef4abc7.tar.bz2 perlweeklychallenge-club-bf69018ef17e5cbf1d02acdc71d29016aef4abc7.zip | |
Merge pull request #9247 from simongreen-net/master
Simon's solution to challenge 245 and 247
| -rw-r--r-- | challenge-245/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-245/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-245/sgreen/perl/ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-245/sgreen/perl/ch-2.pl | 44 | ||||
| -rwxr-xr-x | challenge-245/sgreen/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-245/sgreen/python/ch-2.py | 36 | ||||
| -rw-r--r-- | challenge-246/sgreen/README.md | 3 | ||||
| -rw-r--r-- | challenge-247/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-247/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-247/sgreen/perl/ch-1.pl | 70 | ||||
| -rwxr-xr-x | challenge-247/sgreen/perl/ch-2.pl | 31 | ||||
| -rwxr-xr-x | challenge-247/sgreen/python/ch-1.py | 60 | ||||
| -rwxr-xr-x | challenge-247/sgreen/python/ch-2.py | 29 |
13 files changed, 334 insertions, 7 deletions
diff --git a/challenge-245/sgreen/README.md b/challenge-245/sgreen/README.md index fb592c5c30..ba6bc079ce 100644 --- a/challenge-245/sgreen/README.md +++ b/challenge-245/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 244 +# The Weekly Challenge 245 -Blog: [Weekly Challenge 244](https://dev.to/simongreennet/weekly-challenge-244-jim) +Blog: [Sorting by threes](https://dev.to/simongreennet/sorting-by-threes-ocf) diff --git a/challenge-245/sgreen/blog.txt b/challenge-245/sgreen/blog.txt new file mode 100644 index 0000000000..41beaed6c6 --- /dev/null +++ b/challenge-245/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/sorting-by-threes-ocf
\ No newline at end of file diff --git a/challenge-245/sgreen/perl/ch-1.pl b/challenge-245/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..d2fcf98fe9 --- /dev/null +++ b/challenge-245/sgreen/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'zip'; + +sub main (@inputs) { + # We need an even number of inputs + if ( scalar(@inputs) % 2 == 1 ) { + die "We require an even number of inputs\n"; + } + + # Split the input into words and places + my $half = scalar(@inputs) / 2; + my @words = @inputs[ 0 .. $half - 1 ]; + my @places = @inputs[ $half .. $#inputs ]; + + # Create a dict where the key is the place, and the value is the word + my %rankings = ( map { @$_ } zip( \@places, \@words ) ); + + # Order the words by their ranking + my @solution = ( map { $rankings{$_} } sort { $a <=> $b } keys %rankings ); + say join ', ', @solution; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-245/sgreen/perl/ch-2.pl b/challenge-245/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..8055448867 --- /dev/null +++ b/challenge-245/sgreen/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Algorithm::Combinatorics 'permutations'; +use List::Util 'sum'; + +sub largest_number (@numbers) { + # Calculate the largest number using the all supplied numbers + my $largest = 0; + my $iter = permutations( \@numbers ); + while ( my $i = $iter->next ) { + my $n = join( '', @$i ); + if ( $n > $largest ) { + $largest = $n; + } + } + + return $largest; +} + +sub main (@ints) { + my $largest = -1; + foreach my $bitwise ( 1 .. 2**scalar(@ints) - 1 ) { + my @numbers = + ( map { $ints[$_] } grep { $bitwise & ( 2**$_ ) } ( 0 .. $#ints ) ); + if ( sum(@numbers) % 3 != 0 ) { + # There is no possible solution with this set of numbers + next; + } + + my $this_largest = largest_number(@numbers); + if ( $this_largest > $largest ) { + $largest = $this_largest; + } + } + + say $largest; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-245/sgreen/python/ch-1.py b/challenge-245/sgreen/python/ch-1.py new file mode 100755 index 0000000000..a39f9980eb --- /dev/null +++ b/challenge-245/sgreen/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def main(words, places): + # Create a dict where the key is the place, and the value is the word + rankings = { + place: word for place, word in zip(places, words) + } + + # Order the words by their ranking + solution = [rankings[place] for place in sorted(rankings)] + print(*solution, sep=', ') + + +if __name__ == '__main__': + inputs = sys.argv[1:] + + # We need an even number of inputs + if len(inputs) % 2 == 1: + raise ValueError('We require an even number of inputs') + + # Split the input into words and places + half = len(inputs) // 2 + words = inputs[:half] + # Convert input into integers + places = [int(n) for n in inputs[half:]] + main(words, places) diff --git a/challenge-245/sgreen/python/ch-2.py b/challenge-245/sgreen/python/ch-2.py new file mode 100755 index 0000000000..20098f5788 --- /dev/null +++ b/challenge-245/sgreen/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +from itertools import permutations +import sys + + +def largest_number(numbers): + # Calculate the largest number using the all supplied numbers + largest = 0 + for i in permutations(map(lambda x: str(x), numbers)): + n = int(''.join(i)) + if n > largest: + largest = n + + return largest + + +def main(ints): + largest = -1 + for bitwise in range(1, 2 ** len(ints)): + numbers = [n for i, n in enumerate(ints) if bitwise & (2 ** i)] + if sum(numbers) % 3 != 0: + # There is no possible solution with this set of numbers + continue + + this_largest = largest_number(numbers) + if this_largest > largest: + largest = this_largest + + print(largest) + + +if __name__ == '__main__': + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + main(array) diff --git a/challenge-246/sgreen/README.md b/challenge-246/sgreen/README.md deleted file mode 100644 index fb592c5c30..0000000000 --- a/challenge-246/sgreen/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# The Weekly Challenge 244 - -Blog: [Weekly Challenge 244](https://dev.to/simongreennet/weekly-challenge-244-jim) diff --git a/challenge-247/sgreen/README.md b/challenge-247/sgreen/README.md index fb592c5c30..191a192e6e 100644 --- a/challenge-247/sgreen/README.md +++ b/challenge-247/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 244 +# The Weekly Challenge 247 -Blog: [Weekly Challenge 244](https://dev.to/simongreennet/weekly-challenge-244-jim) +Blog: [The one about frequency](https://dev.to/simongreennet/the-one-about-frequency-la6) diff --git a/challenge-247/sgreen/blog.txt b/challenge-247/sgreen/blog.txt new file mode 100644 index 0000000000..f76ab26514 --- /dev/null +++ b/challenge-247/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-one-about-frequency-la6
\ No newline at end of file diff --git a/challenge-247/sgreen/perl/ch-1.pl b/challenge-247/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..91b6fd58ef --- /dev/null +++ b/challenge-247/sgreen/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub get_person ( $family_members, $exclude ) { + # Sort the family dict by frequency, largest first + my @sorted_surname = sort { + scalar( @{ $family_members->{$b} } ) <=> + scalar( @{ $family_members->{$a} } ) + } keys %$family_members; + + # If we want to exclude a surname, chose the second option if the exclude + # name is first, and there is a second option + if ( $exclude + and $sorted_surname[0] eq $exclude + and $#sorted_surname > 0 + and scalar( @{ $family_members->{ $sorted_surname[1] } } ) > 0 ) + { + return $sorted_surname[1]; + } + + return $sorted_surname[0]; +} + +sub get_surname ($name) { + my $i = rindex( $name, ' ' ); + if ( $i != -1 ) { + return substr( $name, $i + 1 ); + } + + # A person with nothing before their surname + return $name; +} + +sub main (@people) { + my @chain = (); + + # Calculate the frequency of each family + my %family_members = (); + foreach my $p (@people) { + my $surname = get_surname($p); + push @{ $family_members{$surname} }, $p; + } + + # Get the first person, and seed the chain + my $current_surname = get_person( \%family_members, undef ); + my $first_person = pop( @{ $family_members{$current_surname} } ); + push @chain, $first_person; + + foreach ( 1 .. $#people ) { + my $next_surname = get_person( \%family_members, $current_surname ); + push @chain, shift( @{ $family_members{$next_surname} } ); + + # Repeat for the next person + $current_surname = $next_surname; + } + + # The last person gives to the first person + push @chain, $chain[0]; + + # Print the results + foreach my $i ( 0 .. $#people ) { + say "$chain[$i] -> $chain[$i+1]"; + } +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-247/sgreen/perl/ch-2.pl b/challenge-247/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..05520f9c32 --- /dev/null +++ b/challenge-247/sgreen/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($string) { + # The alphabet + my $alphabet = join '', 'a' .. 'z'; + + # Store the best result found + my $solution = undef; + my $occurrences = 0; + + foreach my $i ( 0 .. 24 ) { + # Get the two consecutive letters and the number of occurrences + my $letters = substr( $alphabet, $i, 2 ); + my $count = () = $string =~ /$letters/g; + + # If it is greater than the current count, store it + if ( $count > $occurrences ) { + $occurrences = $count; + $solution = $letters; + } + } + + say $solution // "No consecutive letters found!"; +} + +main( $ARGV[0] );
\ No newline at end of file diff --git a/challenge-247/sgreen/python/ch-1.py b/challenge-247/sgreen/python/ch-1.py new file mode 100755 index 0000000000..14a0595cb2 --- /dev/null +++ b/challenge-247/sgreen/python/ch-1.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +import sys + + +def get_person(family_members, exclude=None): + # Sort the family dict by frequency, largest first + sorted_surname = sorted( + family_members, + key=lambda i: len(family_members[i]), + reverse=True + ) + + # If we want to exclude a surname, chose the second option if the exclude + # name is first, and there is a second option + if exclude and sorted_surname[0] == exclude and len(sorted_surname) > 1 \ + and len(family_members[sorted_surname[1]]) > 0: + return sorted_surname[1] + + return sorted_surname[0] + + +def get_surname(name): + i = name.rfind(' ') + if i is not None: + return name[i+1:] + else: + return name + + +def main(people): + # Calculate the frequency of each family + family_members = {} + for p in people: + surname = get_surname(p) + if surname not in family_members: + family_members[surname] = [] + family_members[surname].append(p) + + # Get the first person, and seed the chain + current_surname = get_person(family_members) + first_person = family_members[current_surname].pop() + chain = [first_person] + + for _ in range(len(people)-1): + next_surname = get_person(family_members, current_surname) + chain.append(family_members[next_surname].pop()) + # Repeat for the next person + current_surname = next_surname + + # The last person gives to the first person + chain.append(chain[0]) + + # Print the results + for i in range(len(people)): + print(f'{chain[i]} -> {chain[i+1]}') + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/challenge-247/sgreen/python/ch-2.py b/challenge-247/sgreen/python/ch-2.py new file mode 100755 index 0000000000..f108d2e7bf --- /dev/null +++ b/challenge-247/sgreen/python/ch-2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys +import string + + +def main(s): + # The alphabet + alphabet = string.ascii_lowercase + + # Store the best result found + solution = None + occurrences = 0 + + for i in range(25): + # Get the two consecutive letters and the number of occurrences + letters = alphabet[i:i+2] + count = s.count(letters) + + # If it is greater than the current count, store it + if count > occurrences: + occurrences = count + solution = letters + + print(solution or "No consecutive letters found!") + + +if __name__ == '__main__': + main(sys.argv[1]) |
