Skip to content
Snippets Groups Projects
Commit bdd0e256 authored by ph's avatar ph
Browse files

Cosmetics

parent ab7779df
Branches master
No related tags found
No related merge requests found
...@@ -11,21 +11,13 @@ import json ...@@ -11,21 +11,13 @@ import json
import curses import curses
import locale import locale
input_file = "testjmena.txt" def merge_sort(to_sort):
output_file = "result.txt" if len(to_sort) <= 1:
comparison_file = "comparisons.pickle" return to_sort
comparisons = {}
count_full = 0
count = 0
win = None
def mergeSort(toSort):
if len(toSort) <= 1:
return toSort
mIndex = len(toSort) / 2 mIndex = len(to_sort) / 2
left = mergeSort(toSort[:mIndex]) left = merge_sort(to_sort[:mIndex])
right = mergeSort(toSort[mIndex:]) right = merge_sort(to_sort[mIndex:])
result = [] result = []
while len(left) > 0 and len(right) > 0: while len(left) > 0 and len(right) > 0:
...@@ -35,65 +27,88 @@ def mergeSort(toSort): ...@@ -35,65 +27,88 @@ def mergeSort(toSort):
result.append(left.pop(0)) result.append(left.pop(0))
if len(left) > 0: if len(left) > 0:
result.extend(mergeSort(left)) result.extend(merge_sort(left))
else: else:
result.extend(mergeSort(right)) result.extend(merge_sort(right))
return result return result
def get_key():
key = 0
while not key in [97, 98]:
key = win.getch()
def compare(a, b): def compare(a, b):
global comparisons global comparisons
global count global count
global count_full 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: try:
fctx = open(comparison_file, "r") fctx = open(comparison_file, "r")
except IOError: except IOError:
pass pass # First time
else: else:
with fctx as f: with fctx as f:
comparisons = pickle.load(f) comparisons = pickle.load(f)
count_full += 1 count_full += 1
if a==b: if a==b: # Keep multiples close
return True 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: if (a, b) in comparisons:
return comparisons[(a, b)] return comparisons[(a, b)]
if (b, a) in comparisons: 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")) win.addstr(("%s(a) nebo %s(b)?\n" % (a, b)).encode("utf-8"))
key = 0 res = get_key()==97
while not key in [97, 98]:
key = win.getch()
res = key==97
# Cache progress
comparisons[(a, b)] = res comparisons[(a, b)] = res
comparisons[(b, a)] = not res comparisons[(b, a)] = not res
# And save for sure
with open(comparison_file, "w") as f: with open(comparison_file, "w") as f:
pickle.dump(comparisons, f) pickle.dump(comparisons, f)
count += 1 count += 1
return res return res
def __main__(): 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: with io.open(input_file, "r", encoding="utf-8") as f:
l = [line for line in f.read().split("\n") if not line.startswith("\\")] l = [line for line in f.read().split("\n") if not line.startswith("\\")]
win.addstr((u", ".join(l)).encode("utf-8")) win.addstr((u", ".join(l)).encode("utf-8"))
win.addstr("\n\n") win.addstr("\n\n")
r = mergeSort(l) r = merge_sort(l)
with io.open(output_file, "w", encoding="utf-8") as f: with io.open(output_file, "w", encoding="utf-8") as f:
for n in reversed(r): for n in reversed(r):
f.write(n) f.write(n)
f.write(u"\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: try:
__main__() __main__()
except KeyboardInterrupt: except KeyboardInterrupt:
...@@ -101,6 +116,5 @@ except KeyboardInterrupt: ...@@ -101,6 +116,5 @@ except KeyboardInterrupt:
curses.nocbreak() curses.nocbreak()
curses.echo() curses.echo()
curses.endwin() curses.endwin()
print comparisons print "Total comparisons: ", count_full
print count_full print "Human comparisons: ", count
print count
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment