diff options
| author | Ysmael Ebreo <Ysmael.Ebreo@latticesemi.com> | 2020-04-12 13:34:15 +0800 |
|---|---|---|
| committer | Ysmael Ebreo <Ysmael.Ebreo@latticesemi.com> | 2020-04-12 13:34:15 +0800 |
| commit | b8473690a44cb00df2ee0c39c8c2cd907b4c006b (patch) | |
| tree | 701855d5916c8af8de2be91d90814baf045e47c4 /challenge-055/yet-ebreo/python/ch-1.py | |
| parent | 04ad75f4916cf2358f48c720b44e67b25f2da063 (diff) | |
| download | perlweeklychallenge-club-b8473690a44cb00df2ee0c39c8c2cd907b4c006b.tar.gz perlweeklychallenge-club-b8473690a44cb00df2ee0c39c8c2cd907b4c006b.tar.bz2 perlweeklychallenge-club-b8473690a44cb00df2ee0c39c8c2cd907b4c006b.zip | |
python solution + perl math::comb + benchmarks
Diffstat (limited to 'challenge-055/yet-ebreo/python/ch-1.py')
| -rw-r--r-- | challenge-055/yet-ebreo/python/ch-1.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-055/yet-ebreo/python/ch-1.py b/challenge-055/yet-ebreo/python/ch-1.py new file mode 100644 index 0000000000..6bf2fc0c52 --- /dev/null +++ b/challenge-055/yet-ebreo/python/ch-1.py @@ -0,0 +1,47 @@ +import sys + +if len(sys.argv) < 2: + bin_str = '010' +else: + bin_str = sys.argv[1] + +size = len(bin_str) +num = int(bin_str, 2) + +res = [] +max = 0 + +for left in list(range(0,size)): + for right in list(range(left,size)): + bin_int = num + for number in list(range(left,right+1)): + bin_int ^= 1 << size - number -1 + + ones = bin(bin_int).count("1") + + if ones > max: + max = ones + res.clear() + + if ones == max: + res.append([left,right]) + +print ("Pair of L-R (one's = " + str(max) + "):") +for out in res: + print (out) + +""" +python .\ch-1.py +Pair of L-R (one's = 2): +[0, 0] +[0, 2] +[2, 2] + +python .\ch-1.py 0101101101 +Pair of L-R (one's = 7): +[0, 0] +[0, 2] +[2, 2] +[5, 5] +[8, 8] +""" |
