From 809a1166a7c30911267a26cf565917398e61abe1 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 24 Mar 2019 16:40:10 +0100 Subject: [PATCH] Added sum of three for GA2 and cleanup --- 1st.py | 0 ga1_last.py => GA1/ga1_last.py | 0 GA2/sum_of_three.py | 38 ++++++++++++++++++++++++++++++++++ heapsort.py | 0 mergesort.py | 0 pair_equal.py | 0 partition.py | 0 7 files changed, 38 insertions(+) mode change 100644 => 100755 1st.py rename ga1_last.py => GA1/ga1_last.py (100%) mode change 100644 => 100755 create mode 100755 GA2/sum_of_three.py mode change 100644 => 100755 heapsort.py mode change 100644 => 100755 mergesort.py mode change 100644 => 100755 pair_equal.py mode change 100644 => 100755 partition.py diff --git a/1st.py b/1st.py old mode 100644 new mode 100755 diff --git a/ga1_last.py b/GA1/ga1_last.py old mode 100644 new mode 100755 similarity index 100% rename from ga1_last.py rename to GA1/ga1_last.py diff --git a/GA2/sum_of_three.py b/GA2/sum_of_three.py new file mode 100755 index 0000000..df04391 --- /dev/null +++ b/GA2/sum_of_three.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +import sys + +def search_two(A, sum2, i_skip): + i = 0 + j = len(A) - 1 + + while i < j: + if i == i_skip: + i = i + 1 + elif j == i_skip: + j = j - 1 + else: + cs = A[i] + A[j] + if cs == sum2: + return True + elif cs > sum2: + j = j - 1 + else: + i = i + 1 + + return False + +def sum_of_three(A, sum3): + A.sort() # assume using mergesort for worst case of O(n*log(n)) + l = len(A) + + for i in range(l): + if search_two(A, sum3 - A[i], i): + return True + + return False + +if __name__ == "__main__": + args = [int(x) for x in sys.argv[1:]] + print(sum_of_three(args[1:], args[0])) + diff --git a/heapsort.py b/heapsort.py old mode 100644 new mode 100755 diff --git a/mergesort.py b/mergesort.py old mode 100644 new mode 100755 diff --git a/pair_equal.py b/pair_equal.py old mode 100644 new mode 100755 diff --git a/partition.py b/partition.py old mode 100644 new mode 100755