From bdd0e256b8c3eca42274feb27f7ec63a403d6d80 Mon Sep 17 00:00:00 2001
From: ph <ph@rook.cz>
Date: Sat, 11 Jun 2016 02:20:51 +0200
Subject: [PATCH] Cosmetics
---
interactive_sort.py | 88 ++++++++++++++++++++++++++-------------------
1 file changed, 51 insertions(+), 37 deletions(-)
diff --git a/interactive_sort.py b/interactive_sort.py
index e8f63a5..b5e1ee0 100755
--- a/interactive_sort.py
+++ b/interactive_sort.py
@@ -11,21 +11,13 @@ import json
import curses
import locale
-input_file = "testjmena.txt"
-output_file = "result.txt"
-comparison_file = "comparisons.pickle"
-comparisons = {}
-count_full = 0
-count = 0
-win = None
-
-def mergeSort(toSort):
- if len(toSort) <= 1:
- return toSort
+def merge_sort(to_sort):
+ if len(to_sort) <= 1:
+ return to_sort
- mIndex = len(toSort) / 2
- left = mergeSort(toSort[:mIndex])
- right = mergeSort(toSort[mIndex:])
+ mIndex = len(to_sort) / 2
+ left = merge_sort(to_sort[:mIndex])
+ right = merge_sort(to_sort[mIndex:])
result = []
while len(left) > 0 and len(right) > 0:
@@ -35,65 +27,88 @@ def mergeSort(toSort):
result.append(left.pop(0))
if len(left) > 0:
- result.extend(mergeSort(left))
+ result.extend(merge_sort(left))
else:
- result.extend(mergeSort(right))
+ result.extend(merge_sort(right))
return result
+
+def get_key():
+ key = 0
+ while not key in [97, 98]:
+ key = win.getch()
+
+
def compare(a, b):
global comparisons
global count
global count_full
+ # Human is the slowest part and we want to be reasonably sure
+ # to have all choices saved on disk, so let's read and write
+ # everything every time - what the heck
try:
fctx = open(comparison_file, "r")
except IOError:
- pass
+ pass # First time
else:
with fctx as f:
comparisons = pickle.load(f)
count_full += 1
- if a==b:
+ if a==b: # Keep multiples close
return True
+ # Human is the slowest part and mergesort may compare the same
+ # items multiple times, so let's use cached choices
if (a, b) in comparisons:
return comparisons[(a, b)]
if (b, a) in comparisons:
- return comparisons[(b, a)]
+ return not comparisons[(b, a)]
- # Comparison
+ # Comparison - ask human
win.addstr(("%s(a) nebo %s(b)?\n" % (a, b)).encode("utf-8"))
- key = 0
- while not key in [97, 98]:
- key = win.getch()
- res = key==97
+ res = get_key()==97
+ # Cache progress
comparisons[(a, b)] = res
comparisons[(b, a)] = not res
+ # And save for sure
with open(comparison_file, "w") as f:
pickle.dump(comparisons, f)
count += 1
return res
+
def __main__():
- locale.setlocale(locale.LC_ALL, "cs_CZ.UTF-8")
- sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
- global win
- win = curses.initscr()
- win.scrollok(True)
- win.idlok(True)
- curses.noecho()
- curses.cbreak()
- l = [''.join(random.choice(string.ascii_lowercase) for y in range(1)) for x in range(76)]
with io.open(input_file, "r", encoding="utf-8") as f:
l = [line for line in f.read().split("\n") if not line.startswith("\\")]
win.addstr((u", ".join(l)).encode("utf-8"))
win.addstr("\n\n")
- r = mergeSort(l)
+ r = merge_sort(l)
with io.open(output_file, "w", encoding="utf-8") as f:
for n in reversed(r):
f.write(n)
f.write(u"\n")
+
+comparisons = {}
+count_full = 0
+count = 0
+win = None
+
+if len(sys.argv)!=4:
+ print "Use: interactive_sort.py source.txt result.txt comparisoncache.pickle"
+
+input_file = sys.argv[1]
+output_file = sys.argv[2]
+comparison_file = sys.argv[3]
+
+locale.setlocale(locale.LC_ALL, "cs_CZ.UTF-8")
+sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
+win = curses.initscr()
+win.scrollok(True)
+win.idlok(True)
+curses.noecho()
+curses.cbreak()
try:
__main__()
except KeyboardInterrupt:
@@ -101,6 +116,5 @@ except KeyboardInterrupt:
curses.nocbreak()
curses.echo()
curses.endwin()
-print comparisons
-print count_full
-print count
+print "Total comparisons: ", count_full
+print "Human comparisons: ", count
--
GitLab