From b8473690a44cb00df2ee0c39c8c2cd907b4c006b Mon Sep 17 00:00:00 2001 From: Ysmael Ebreo Date: Sun, 12 Apr 2020 13:34:15 +0800 Subject: python solution + perl math::comb + benchmarks --- challenge-055/yet-ebreo/python/ch-1.py | 47 ++++++++++++++++++++++ challenge-055/yet-ebreo/python/ch-2.py | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 challenge-055/yet-ebreo/python/ch-1.py create mode 100644 challenge-055/yet-ebreo/python/ch-2.py (limited to 'challenge-055/yet-ebreo/python') 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] +""" diff --git a/challenge-055/yet-ebreo/python/ch-2.py b/challenge-055/yet-ebreo/python/ch-2.py new file mode 100644 index 0000000000..8d40a6f06e --- /dev/null +++ b/challenge-055/yet-ebreo/python/ch-2.py @@ -0,0 +1,71 @@ +from itertools import permutations +import sys +import time + +start_time = time.time() + +if len(sys.argv) < 2: + narray = [1, 2, 3, 4] +else: + narray = list(map(int, sys.argv[1:])) + +narray.sort() +dict = {} +for elem in list(permutations(narray)): + flag = 1 + for e in list(range(1, len(elem))): + flag &= (elem[e] >= elem[e-1], elem[e] <= elem[e-1])[e % 2 > 0] + if not flag: + break + + if flag > 0: + hold = str(elem) + if (not hold in dict): + print (hold) + dict[hold] = 1 + +print("Execution Time: %s seconds" % (time.time() - start_time)) +""" +python .\ch-2.py +(2, 1, 4, 3) +(3, 1, 4, 2) +(3, 2, 4, 1) +(4, 1, 3, 2) +(4, 2, 3, 1) +Execution Time: 0.02042078971862793 seconds + +python .\ch-2.py 1 2 2 3 4 +(2, 1, 3, 2, 4) +(2, 1, 4, 2, 3) +(2, 2, 3, 1, 4) +(2, 2, 4, 1, 3) +(3, 1, 2, 2, 4) +(3, 1, 4, 2, 2) +(3, 2, 2, 1, 4) +(3, 2, 4, 1, 2) +(4, 1, 2, 2, 3) +(4, 1, 3, 2, 2) +(4, 2, 2, 1, 3) +(4, 2, 3, 1, 2) +Execution Time: 0.10867619514465332 seconds + +python .\ch-2.py 1 2 3 4 5 6 7 8 9 +... +(9, 7, 8, 4, 6, 3, 5, 1, 2) +(9, 7, 8, 5, 6, 1, 3, 2, 4) +(9, 7, 8, 5, 6, 1, 4, 2, 3) +(9, 7, 8, 5, 6, 2, 3, 1, 4) +(9, 7, 8, 5, 6, 2, 4, 1, 3) +(9, 7, 8, 5, 6, 3, 4, 1, 2) +Execution Time: 14.091261386871338 seconds + +python .\ch-2.py 1 2 3 4 5 6 7 8 9 10 +... +(10, 8, 9, 6, 7, 3, 4, 1, 5, 2) +(10, 8, 9, 6, 7, 3, 4, 2, 5, 1) +(10, 8, 9, 6, 7, 3, 5, 1, 4, 2) +(10, 8, 9, 6, 7, 3, 5, 2, 4, 1) +(10, 8, 9, 6, 7, 4, 5, 1, 3, 2) +(10, 8, 9, 6, 7, 4, 5, 2, 3, 1) +Execution Time: 145.47170519828796 seconds +""" \ No newline at end of file -- cgit