diff options
| author | User Person <usermanperson+github@gmail.com> | 2020-04-12 12:30:18 -0400 |
|---|---|---|
| committer | User Person <usermanperson+github@gmail.com> | 2020-04-12 12:30:18 -0400 |
| commit | 7c560e808f463375ea74da4fbbafbcf5129aaef1 (patch) | |
| tree | c2a0e4a3f06562dedb9aee8285cb2559563dfce1 | |
| parent | d24a293c623ca2b2e08f2ef506e032ce38c49015 (diff) | |
| download | perlweeklychallenge-club-7c560e808f463375ea74da4fbbafbcf5129aaef1.tar.gz perlweeklychallenge-club-7c560e808f463375ea74da4fbbafbcf5129aaef1.tar.bz2 perlweeklychallenge-club-7c560e808f463375ea74da4fbbafbcf5129aaef1.zip | |
User-person's solutions for challenge 55.
| -rwxr-xr-x | challenge-055/user-person/perl/ch-1.pl | 89 | ||||
| -rwxr-xr-x | challenge-055/user-person/perl/ch-2.pl | 74 | ||||
| -rwxr-xr-x | challenge-055/user-person/python/ch-1.py | 89 | ||||
| -rwxr-xr-x | challenge-055/user-person/python/ch-2.py | 70 |
4 files changed, 322 insertions, 0 deletions
diff --git a/challenge-055/user-person/perl/ch-1.pl b/challenge-055/user-person/perl/ch-1.pl new file mode 100755 index 0000000000..19d13b0475 --- /dev/null +++ b/challenge-055/user-person/perl/ch-1.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/ # +# # +# Flip Binary # +# # +# You are given a binary number B, consisting of N binary digits # +# 0 or 1: s0, s1, ..., s(N-1). # +# # +# Choose two indices L and R such that 0 <= L <= R < N and flip the # +# digits s(L),s(L+1), ... , s(R) By flipping, we mean changing 0 to 1 # +# and vice-versa. # +# # +# Write a script to find the indices (L,R) that results in a # +# binary number with maximum number of 1s. If you find more than one # +# maximal pair L,R then print all of them. # +# # +# Continuing our example, note that we had three pairs (L=0, R=0), # +# (L=0, R=2), and (L=2, R=2) that resulted in a binary number with # +# two 1s, which was the maximum. So we would print all three pairs. # +# # +########################################################################### + +use strict; +use warnings; + +my $originalString = "010"; + +if (@ARGV) { + $originalString = "$ARGV[0]"; + if ( $originalString =~ m{[^01]} ) { + print STDERR "Arguments can only contain 1s and 0s.\n"; + exit(1); + } +} else { + print STDERR "No arguments given. Using example data: 010\n"; +} + +my $n = length $originalString; + +my $lowerBound = 0; +my $upperBound = $n; + +my $max = 0; +my @maxes = (); + +my $hasZeroes = 0; +if ($originalString =~ m{0} ) { + $hasZeroes = 1; +} + +if ( $originalString =~ m{\A(1+)} and $hasZeroes) { + $lowerBound = length $1; +} + +if ( $originalString =~ m{(1+)\Z} and $hasZeroes) { + my $difference = length $1; + $upperBound = $n - $difference; +} + +for (my $left = $lowerBound; $left <= $upperBound; ++$left) { + for (my $right = $left+1; $right <= $upperBound; ++$right) { + + my $binaryString = $originalString; + my $offset = $right - $left; + + my $replace = substr $binaryString, $left, $offset; + $replace =~ tr{01}{10}; + substr $binaryString, $left, $offset, $replace; + + my $num = () = $binaryString =~ m{1}g; + + if ($num > $max) { + $max = $num; + @maxes = (); + push @maxes, sprintf("(L=%d, R=%d)", $left, $right-1); + } elsif ($num == $max) { + push @maxes, sprintf("(L=%d, R=%d)", $left, $right-1); + } + } +} + +print "$_ " foreach @maxes; +print "\n"; diff --git a/challenge-055/user-person/perl/ch-2.pl b/challenge-055/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..8da157ea64 --- /dev/null +++ b/challenge-055/user-person/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/ # +# # +# Wave Array # +# Any array N of non-unique, unsorted integers can be arranged into a # +# wave-like array such that n1 => n2 <= n3 => n4 <= n5 and so on. # +# For example, given the array [1, 2, 3, 4], possible wave arrays # +# include [2, 1, 4, 3] or [4, 1, 3, 2], since 2 => 1 <= 4 => 3 <= # +# and 4 => 1 <= 3 => 2. This is not a complete list. # +# Write a script to print all possible wave arrays for an integer array # +# N of arbitrary length. # +# # +# Notes: # +# When considering N of any length, note that the first element is # +# always greater than or equal to the second, and then the <= => <= # +# sequence alternates until the end of the array. # +# # +########################################################################### + +use strict; +use warnings; + +use List::Permutor; + +my @n = (1,2,3,4); + +if (@ARGV) { + @n = (); + my $argString = "@ARGV"; + + my %pair = ( '[' => ']', '(' => '\)', '{' => '}', '<' => '>'); + $argString =~ s{\A\s*([[(\{<])\s*}{}; + $argString =~ s{\s*$pair{$1}\s*\Z}{} if $1; + $argString =~ s{[, ]+}{ }g; + + foreach (split m{ }, $argString) { + if ($_ =~ m{\d+}) { + push @n, $_; + } else { + print STDERR "Non-numeric input detected.\n"; + exit(1); + } + } + +} else { + print STDERR "No arguments given. Using example data: [1, 2, 3, 4]\n"; +} + +my $perm = new List::Permutor @n; + +ARRAY_LOOP: +while (my @set = $perm->next) { + my $prev; + + ITEM_LOOP: + for (my $i = 0; $i <= $#set; ++$i) { + next ITEM_LOOP if $i == 0; + + if ($i % 2 == 0) { # EVEN + next ARRAY_LOOP if $set[$i-1] > $set[$i]; + } else { # ODD + next ARRAY_LOOP if $set[$i-1] < $set[$i]; + } + } + + print "$_ " foreach @set; + print "\n"; +} diff --git a/challenge-055/user-person/python/ch-1.py b/challenge-055/user-person/python/ch-1.py new file mode 100755 index 0000000000..270827665a --- /dev/null +++ b/challenge-055/user-person/python/ch-1.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/ # +# # +# Flip Binary # +# # +# You are given a binary number B, consisting of N binary digits # +# 0 or 1: s0,s1, ..., s(N-1). # +# # +# Choose two indices L and R such that 0 <= L <= R < N and flip the # +# digits s(L),s(L+1), ... , s(R) By flipping, we mean changing 0 to 1 # +# and vice-versa. # +# # +# Write a script to find the indices (L,R) that results in a # +# binary number with maximum number of 1s. If you find more than one # +# maximal pair L,R then print all of them. # +# # +# Continuing our example, note that we had three pairs (L=0, R=0), # +# (L=0, R=2), and (L=2, R=2) that resulted in a binary number with # +# two 1s, which was the maximum. So we would print all three pairs. # +# # +########################################################################### + +import re +import sys + +originalString = "010" + +if len(sys.argv) > 1: + originalString = ''.join(sys.argv[1:]) + if re.search(r'[^01]', str(originalString)): + sys.stderr.write('Arguments can only contain 1s and 0s.\n') + sys.exit(1) +else: + sys.stderr.write('No arguments given. Using example data: 010\n') + +n = len(originalString) +lowerBound = 0 +upperBound = n +max = 0 +maxes = [] + +hasZeroes = False +if re.search(r'0', originalString): + hasZeroes = True + +if re.search(r'\A1+', originalString) and hasZeroes: + front = re.search(r'\A(1+)', originalString) + lowerBound = len(front.group(0)) + +if re.search(r'1+\Z', originalString) and hasZeroes: + back = re.search(r'(1+)\Z', originalString) + difference = len(back.group(0)) + upperBound = n - difference + +left = lowerBound +while left <= upperBound: + + right = left+1 + + while right <= upperBound: + binarySet = list(originalString) + for x in range(left,right,1): + if binarySet[x] == '0': + binarySet[x] ='1' + elif binarySet[x] == '1': + binarySet[x] = '0' + + num = binarySet.count('1') + + if num > max: + max = num + maxes = [] + maxes.append('(L=%d, R=%d)' % (left, right-1)) + elif num == max: + maxes.append('(L=%d, R=%d)' % (left, right-1)) + + right += 1 + left += 1 + +for coord in maxes: + print(coord,end=' ') +else: + print() diff --git a/challenge-055/user-person/python/ch-2.py b/challenge-055/user-person/python/ch-2.py new file mode 100755 index 0000000000..b0e3232bc8 --- /dev/null +++ b/challenge-055/user-person/python/ch-2.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-055/ # +# # +# Wave Array # +# Any array N of non-unique, unsorted integers can be arranged into a # +# wave-like array such that n1 => n2 <= n3 => n4 <= n5 and so on. # +# For example, given the array [1, 2, 3, 4], possible wave arrays # +# include [2, 1, 4, 3] or [4, 1, 3, 2], since 2 => 1 <= 4 => 3 <= # +# and 4 => 1 <= 3 => 2. This is not a complete list. # +# Write a script to print all possible wave arrays for an integer array # +# N of arbitrary length. # +# # +# Notes: # +# When considering N of any length, note that the first element is # +# always greater than or equal to the second, and then the <= => <= # +# sequence alternates until the end of the array. # +# # +########################################################################### + +from itertools import permutations +import re +import sys + +n = [ 1, 2, 3, 4] + +if len(sys.argv) > 1: + argString = ' '.join(sys.argv[1:]) + + matchObj = re.match(r'\A\s*([[(\{<])\s*', argString) + if matchObj: + pair = { '[' : ']', '(' : ')', '{' : '}', '<' : '>'} + pairMatch = pair[ matchObj.group(0) ] + argString = re.sub(r'\A\s*[[(\{<]\s*','',argString) + argString = re.sub('\s*'+re.escape(pairMatch)+'\s*\Z', '', argString) + + argString = re.sub(r'\s*,\s*',' ',argString) + n = [int(x) for x in argString.split(" ")] + +else: + sys.stderr.write('No arguments given. Using example data: [1, 2, 3, 4]\n') + +for num in n: + if type(num) != int: + sys.stderr.write('Non-numeric input detected.\n') + sys.exit(1) + +for set in permutations(n): + i = 0 + + for i in range(len(set)): + if i == 0: + continue + + if i % 2 == 0: # EVEN + if set[i-1] > set[i]: + break + else: # ODD + if set[i-1] < set[i]: + break + else: + for x in set: + print(x,end=' ') + else: + print() |
