diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-09 22:10:09 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-09 22:10:09 +0100 |
| commit | 0d07992c8e9958afc2c941ca0b5f034bd426cdba (patch) | |
| tree | bed7025f7868f200b2b097ef1df68d69a667c641 | |
| parent | 451d779420e30ac64a61beadea91da317b8dc6b2 (diff) | |
| download | perlweeklychallenge-club-0d07992c8e9958afc2c941ca0b5f034bd426cdba.tar.gz perlweeklychallenge-club-0d07992c8e9958afc2c941ca0b5f034bd426cdba.tar.bz2 perlweeklychallenge-club-0d07992c8e9958afc2c941ca0b5f034bd426cdba.zip | |
Add Python solution to challenge 055
| -rw-r--r-- | challenge-055/paulo-custodio/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-055/paulo-custodio/python/ch-2.py | 40 |
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-055/paulo-custodio/python/ch-1.py b/challenge-055/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..1eb05bf163 --- /dev/null +++ b/challenge-055/paulo-custodio/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# Challenge 055 +# +# TASK #1 +# 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 change 0 to 1 and vice-versa. +# +# For example, given the binary number 010, the possible flip pair results are +# listed below: +# +# L=0, R=0 the result binary: 110 +# L=0, R=1 the result binary: 100 +# L=0, R=2 the result binary: 101 +# L=1, R=1 the result binary: 000 +# L=1, R=2 the result binary: 001 +# L=2, R=2 the result binary: 011 +# 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 sys + +bin = sys.argv[1] +max_1s = 0 +max_1s_pairs = [] + +for l in range(0, len(bin)-1+1): + for r in range(l, len(bin)-1+1): + bits = [int(x) for x in bin] + for i in range(l, r+1): + bits[i] = 1-bits[i] + _1s = len(list(filter(lambda x:x==1, bits))) + if _1s > max_1s: + max_1s = _1s + max_1s_pairs = [[l,r]] + elif _1s == max_1s: + max_1s_pairs.append([l,r]) + +out = [] +for x in max_1s_pairs: + out.append("(L="+str(x[0])+", R="+str(x[1])+")") +print(", ".join(out)) diff --git a/challenge-055/paulo-custodio/python/ch-2.py b/challenge-055/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..ab2ebac891 --- /dev/null +++ b/challenge-055/paulo-custodio/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 055 +# +# TASK #2 +# 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. + +import sys + +def show_waves(wave, next): + if len(next) == 0: + print(" ".join([str(x) for x in wave])) + elif len(wave) == 0: + for i in range(len(next)): + show_waves(wave+[next[i]], next[:i]+next[i+1:]) + elif len(wave) % 2 == 1: # going down + for i in range(len(next)): + if wave[-1] >= next[i]: + show_waves(wave+[next[i]], next[:i]+next[i+1:]) + else: # going up + for i in range(len(next)): + if wave[-1] <= next[i]: + show_waves(wave+[next[i]], next[:i]+next[i+1:]) + +n = [int(x) for x in sys.argv[1:]] +show_waves([], n) |
