From 864d111326187ac0971f2ee4fd4d3110b28f5e1d Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 5 Mar 2019 13:23:46 +0100 Subject: [PATCH] 2019-03-05 in class --- histogram.py | 33 +++++++++++++++------------------ insertion_sort.py | 10 +++++----- mergesort.py | 34 ++++++++++++++++++++++++++++++++++ notes.md | 5 +++++ 4 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 mergesort.py diff --git a/histogram.py b/histogram.py index 2a671f8..74c3367 100755 --- a/histogram.py +++ b/histogram.py @@ -1,30 +1,27 @@ #!/usr/bin/env python3 +# vim: set sw=4 ts=4 et tw=80: import sys def histogram(data): - height_max = max(data) - height_min = min(min(data), 0) - current_line = height_max + top = max(max(data), 0) + bottom = min(min(data), 0) + i = top - while current_line >= height_min: + while i >= bottom: to_print = "" - if current_line == 0: - print("-" * len(data)) - else: - for elem in data: - if current_line > 0 and elem >= current_line: - to_print += "#" - elif current_line > 0: - to_print += " " - elif current_line < 0 and elem <= current_line: - to_print += "#" - else: - to_print += " " - print(to_print) + for elem in data: + if i == 0: + to_print += "-" + elif (i > 0 and elem >= i) or \ + (i < 0 and elem <= i): + to_print += "#" + else: + to_print += " " + print(to_print) - current_line = current_line - 1 + i = i - 1 def main(): args = [int(x) for x in sys.argv[1:]] diff --git a/insertion_sort.py b/insertion_sort.py index f369757..28afbac 100755 --- a/insertion_sort.py +++ b/insertion_sort.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# vim: set ts=4 sw=4 et tw=80: import sys @@ -10,11 +11,10 @@ def insertionsort(A): print(A) print(A) i = j - 1 - tomove = A[j] - while i >= 0 and tomove < A[i]: - if A[i] > A[i+1]: - # Swap A[i] and A[j] - A[i+1], A[i] = A[i], A[i+1] + # while swapping is necessary or at the end of array + while i >= 0 and A[i+1] < A[i]: + # Swap A[i] and A[j] + A[i+1], A[i] = A[i], A[i+1] i = i - 1 return A diff --git a/mergesort.py b/mergesort.py new file mode 100644 index 0000000..a1b6a32 --- /dev/null +++ b/mergesort.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# vim: set ts=4 sw=4 et tw=80: + +import sys +import math + +def find(A,x): + for a in A: + if a == x: + return True + return False + +def merge(A,B): + X = [] + for a in A: + if not find(X, a): + X.append(a) + for b in B: + if not find(X, b): + X.append(b) + return X + +def mergesort(A): + l = len(A) + if l < 2: + return A + else: + lh = len(A) // 2 + h1 = mergesort(A[:lh]) + h2 = mergesort(A[lh:]) + return merge(h1,h2) + +args = [int(x) for x in sys.argv[1:]] +print(mergesort(args)) diff --git a/notes.md b/notes.md index 12b98ac..65667a9 100644 --- a/notes.md +++ b/notes.md @@ -94,3 +94,8 @@ Proof sort of by induction. An algorithm is correct if given an input the output satisfies the conditions stated. The algorithm must terminate. + +### The loop invariant + +Invariant condition able to make a loop equivalent to a straight path in an +execution graph.