Skip to content
Snippets Groups Projects
Commit e218f84e authored by Markus Rothgänger's avatar Markus Rothgänger
Browse files

sorting stuff

parent 4d209c04
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import cv2 as cv import cv2 as cv
import numpy as np import numpy as np
LEFT_ARROW_KEY_CODE = 65361
RIGHT_ARROW_KEY_CODE = 65363
DOWN_ARROW_KEY_CODE = 65364
WINDOW_NAME = "cmp"
global cmp_count
cmp_count = 0
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
class _Getch: def create_comp_image(leftContent: str, rightContent: str):
"""Gets a single character from standard input. Does not echo to the img = np.zeros((64, 128, 3), np.uint8)
screen. From http://code.activestate.com/recipes/134892/"""
def __init__(self):
self.impl = _GetchUnix()
def __call__(self):
return self.impl()
class _GetchUnix: cv.putText(img, leftContent, (30, 35), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), thickness=2)
def __init__(self): cv.putText(img, rightContent, (90, 35), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), thickness=2)
import tty, sys, termios # import termios now or else you'll get the Unix version on the Mac
def __call__(self): return img
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def user_input_cmp(a, b):
global cmp_count
cmp_count += 1
img = create_comp_image(str(a), str(b))
def getKey(): cv.imshow(WINDOW_NAME, img)
inkey = _Getch() cv.setWindowTitle(WINDOW_NAME, "which image is more complex?")
return_key = None
while True: while True:
k = inkey() key = cv.waitKeyEx(0)
if k != "":
return k
def user_input_cmp(a, b):
print(a, b)
k = getKey() if key == LEFT_ARROW_KEY_CODE:
print(k) return_key = 1
break
if key == RIGHT_ARROW_KEY_CODE:
return_key = -1
break
# if key == DOWN_ARROW_KEY_CODE:
# return_key = 0
# break
return 1 # a lt b cv.destroyWindow(WINDOW_NAME)
return -1 # a gt b
if return_key is None:
sys.exit()
# TODO: use two ipywidget buttons?!Q return return_key
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
cmp_count = 0
random_list = np.arange(10) random_list = np.arange(10)
np.random.shuffle(random_list)
def cmp_to_key(cmp_fn): def cmp_to_key(cmp_fn):
'Convert a cmp= function into a key= function' 'Convert a cmp= function into a key= function'
class K: class K:
def __init__(self, obj, *args): def __init__(self, obj, *args):
self.obj = obj self.obj = obj
def __lt__(self, other): def __lt__(self, other):
return cmp_fn(self.obj, other.obj) < 0 return cmp_fn(self.obj, other.obj) < 0
def __gt__(self, other): def __gt__(self, other):
return cmp_fn(self.obj, other.obj) > 0 return cmp_fn(self.obj, other.obj) > 0
def __eq__(self, other): def __eq__(self, other):
return cmp_fn(self.obj, other.obj) == 0 return cmp_fn(self.obj, other.obj) == 0
def __le__(self, other): def __le__(self, other):
return cmp_fn(self.obj, other.obj) <= 0 return cmp_fn(self.obj, other.obj) <= 0
def __ge__(self, other): def __ge__(self, other):
return cmp_fn(self.obj, other.obj) >= 0 return cmp_fn(self.obj, other.obj) >= 0
def __ne__(self, other): def __ne__(self, other):
return cmp_fn(self.obj, other.obj) != 0 return cmp_fn(self.obj, other.obj) != 0
return K return K
sorted(list(random_list), key=cmp_to_key(user_input_cmp)) print(random_list)
print(sorted(list(random_list), key=cmp_to_key(user_input_cmp)))
print(cmp_count)
``` ```
%% Output %% Output
1 0 Canceled future for execute_request message before replies were done
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.
%% Cell type:code id: tags:
``` python
10 * np.log(10)
```
%% Output
--------------------------------------------------------------------------- 23.02585092994046
error Traceback (most recent call last)
/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb Cell 3' in <cell line: 22>()
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000002?line=18'>19</a> return cmp_fn(self.obj, other.obj) != 0
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000002?line=19'>20</a> return K
---> <a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000002?line=21'>22</a> sorted(list(random_list), key=cmp_to_key(user_input_cmp))
/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb Cell 3' in cmp_to_key.<locals>.K.__lt__(self, other)
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000002?line=7'>8</a> def __lt__(self, other):
----> <a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000002?line=8'>9</a> return cmp_fn(self.obj, other.obj) < 0
/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb Cell 2' in user_input_cmp(a, b)
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=38'>39</a> def user_input_cmp(a, b):
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=39'>40</a> print(a, b)
---> <a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=41'>42</a> k = getKey()
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=42'>43</a> print(k)
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=44'>45</a> return 1 # a lt b
/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb Cell 2' in getKey()
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=29'>30</a> inkey = _Getch()
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=31'>32</a> while True:
---> <a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=32'>33</a> k = inkey()
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=34'>35</a> if k != "":
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=35'>36</a> return k
/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb Cell 2' in _Getch.__call__(self)
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=7'>8</a> def __call__(self):
----> <a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=8'>9</a> return self.impl()
/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb Cell 2' in _GetchUnix.__call__(self)
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=16'>17</a> import sys, tty, termios
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=18'>19</a> fd = sys.stdin.fileno()
---> <a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=19'>20</a> old_settings = termios.tcgetattr(fd)
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=20'>21</a> try:
<a href='vscode-notebook-cell:/home/markus/uni/minerl-indexing/shape_complexity/sorting_apllication_test.ipynb#ch0000001?line=21'>22</a> tty.setraw(sys.stdin.fileno())
error: (25, 'Inappropriate ioctl for device')
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment