Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
interactive_sort
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pavel Kácha
interactive_sort
Commits
bdd0e256
Commit
bdd0e256
authored
Jun 11, 2016
by
ph
Browse files
Options
Downloads
Patches
Plain Diff
Cosmetics
parent
ab7779df
Branches
master
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
interactive_sort.py
+51
-37
51 additions, 37 deletions
interactive_sort.py
with
51 additions
and
37 deletions
interactive_sort.py
+
51
−
37
View file @
bdd0e256
...
@@ -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
(
to
S
ort
)
/
2
mIndex
=
len
(
to
_s
ort
)
/
2
left
=
merge
S
ort
(
to
S
ort
[:
mIndex
])
left
=
merge
_s
ort
(
to
_s
ort
[:
mIndex
])
right
=
merge
S
ort
(
to
S
ort
[
mIndex
:])
right
=
merge
_s
ort
(
to
_s
ort
[
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
(
merge
S
ort
(
left
))
result
.
extend
(
merge
_s
ort
(
left
))
else
:
else
:
result
.
extend
(
merge
S
ort
(
right
))
result
.
extend
(
merge
_s
ort
(
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
=
merge
S
ort
(
l
)
r
=
merge
_s
ort
(
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment