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 /challenge-055/user-person/python | |
| 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.
Diffstat (limited to 'challenge-055/user-person/python')
| -rwxr-xr-x | challenge-055/user-person/python/ch-1.py | 89 | ||||
| -rwxr-xr-x | challenge-055/user-person/python/ch-2.py | 70 |
2 files changed, 159 insertions, 0 deletions
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() |
