def __init__(self, parent=None): QWidget.__init__(self, parent) self.table = Table(11, self) self.table.show() self.resize(self.table.width(), self.table.height() + WND_SPACING*3 + BUTT_H*2) self.ht = HashTable(11, lambda x: x, lambda x, n: x % n, logger=self.logger) self.example_g = QPushButton(self) self.example_g.setText(u'예제') self.example_g.move(WND_SPACING, self.table.height() + WND_SPACING*2 + BUTT_H); self.example_g.resize(BUTT_W, BUTT_H) self.example_g.clicked.connect(self.on_start) self.input_g = QLineEdit(self) self.input_g.move(WND_SPACING, self.table.height() + WND_SPACING); self.input_g.resize(BUTT_W, BUTT_H) self.add_g = QPushButton(self) self.add_g.setText(u'추가') self.add_g.move(WND_SPACING*2 + BUTT_W, self.table.height() + WND_SPACING); self.add_g.resize(BUTT_W, BUTT_H) self.add_g.clicked.connect(self.on_add) self.delete_g = QPushButton(self) self.delete_g.setText(u'삭제') self.delete_g.move(WND_SPACING*3 + BUTT_W*2, self.table.height() + WND_SPACING); self.delete_g.resize(BUTT_W, BUTT_H) self.delete_g.clicked.connect(self.on_delete) self.input_g.setFocus()
def __init__(self, parent=None): QWidget.__init__(self, parent) self.table = Table(11, self) self.table.show() self.resize(self.table.width(), self.table.height() + WND_SPACING * 3 + BUTT_H * 2) self.ht = HashTable(11, lambda x: x, lambda x, n: x % n, logger=self.logger) self.example_g = QPushButton(self) self.example_g.setText(u'예제') self.example_g.move(WND_SPACING, self.table.height() + WND_SPACING * 2 + BUTT_H) self.example_g.resize(BUTT_W, BUTT_H) self.example_g.clicked.connect(self.on_start) self.input_g = QLineEdit(self) self.input_g.move(WND_SPACING, self.table.height() + WND_SPACING) self.input_g.resize(BUTT_W, BUTT_H) self.add_g = QPushButton(self) self.add_g.setText(u'추가') self.add_g.move(WND_SPACING * 2 + BUTT_W, self.table.height() + WND_SPACING) self.add_g.resize(BUTT_W, BUTT_H) self.add_g.clicked.connect(self.on_add) self.delete_g = QPushButton(self) self.delete_g.setText(u'삭제') self.delete_g.move(WND_SPACING * 3 + BUTT_W * 2, self.table.height() + WND_SPACING) self.delete_g.resize(BUTT_W, BUTT_H) self.delete_g.clicked.connect(self.on_delete) self.input_g.setFocus()
class MainWindow(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.table = Table(11, self) self.table.show() self.resize(self.table.width(), self.table.height() + WND_SPACING * 3 + BUTT_H * 2) self.ht = HashTable(11, lambda x: x, lambda x, n: x % n, logger=self.logger) self.example_g = QPushButton(self) self.example_g.setText(u'예제') self.example_g.move(WND_SPACING, self.table.height() + WND_SPACING * 2 + BUTT_H) self.example_g.resize(BUTT_W, BUTT_H) self.example_g.clicked.connect(self.on_start) self.input_g = QLineEdit(self) self.input_g.move(WND_SPACING, self.table.height() + WND_SPACING) self.input_g.resize(BUTT_W, BUTT_H) self.add_g = QPushButton(self) self.add_g.setText(u'추가') self.add_g.move(WND_SPACING * 2 + BUTT_W, self.table.height() + WND_SPACING) self.add_g.resize(BUTT_W, BUTT_H) self.add_g.clicked.connect(self.on_add) self.delete_g = QPushButton(self) self.delete_g.setText(u'삭제') self.delete_g.move(WND_SPACING * 3 + BUTT_W * 2, self.table.height() + WND_SPACING) self.delete_g.resize(BUTT_W, BUTT_H) self.delete_g.clicked.connect(self.on_delete) self.input_g.setFocus() def logger(self, *args): if args[0] == 'a': self.table.add_card(args[1], str(args[2])) elif args[0] == 'm': self.table.move_card(args[1], args[2]) elif args[0] == 'd': self.table.remove_card(args[1]) elif args[0] == 'l': self.table.link_card(args[1], args[2]) def on_start(self, ev): items = [27, 18, 29, 28, 39, 13, 16, 42, 17] for x in items: print '* add %d' % x self.ht.add(x) self.ht.check_validity() self.ht.print_table() for x in items: print '* remove %d' % x self.ht.remove(x) self.ht.check_validity() self.ht.print_table() def on_add(self, ev): try: key = int(self.input_g.text()) if key <= 0: QMessageBox.critical(self, None, 'Only positive numbers are allowed.') return except ValueError: QMessageBox.critical(self, None, 'Invalid key.') return item = key try: self.ht.add(item) except ValueError: QMessageBox.critical(self, None, 'Duplicate found.') return self.ht.check_validity() self.ht.print_table() def on_delete(self, ev): key = int(self.input_g.text()) item = key try: self.ht.remove(item) except KeyError: QMessageBox.critical(self, None, 'Key does not exist.') return self.ht.check_validity() self.ht.print_table()
def main(): ht = HashTable(1070000, lambda x: x['name'], lambda x, n: sum(ord(i)**3 for i in x) % n) for line in open(NAMES_FPATH): key = line.rstrip() try: ht.add({'name': key, 'x': 0.0, 'y': 0.0, 'addr': ''}) except ValueError: print key print ht.search(key) print '* Hash table created' while True: line = raw_input() words = line.split() if words[0] == 'r': key = words[1] try: item = ht.search(key) except KeyError: print >> sys.stderr, '* not found' else: print '* found: %s' % item elif words[0] == 'f': key = words[1] for item in ht.items: if item and ht.keyer(item) == key: print '* found: %s' % item break else: print >> sys.stderr, '* not found' elif words[0] == 'd': key = words[1] try: ht.remove(key) except KeyError: print >> sys.stderr, '* not found' else: print '* removed' ht.check_validity() elif words[0] == 'p': probe_sum = 0 probe_cnt = 0 for line in open(NAMES_FPATH): key = line.rstrip() probe_sum += ht.get_probe_cnt(key) probe_cnt += 1 print '%.2f' % (float(probe_sum) / probe_cnt) elif words[0] == 'e': break else: print >> sys.stderr, '* invalid command'
def main(): ht = HashTable(11, lambda x: x['id'], lambda x, n: x % n) ht.add({'id': 11, 'name': 'kim'}) ht.add({'id': 22, 'name': 'lee'}) ht.add({'id': 12, 'name': 'park'}) ht.add({'id': 33, 'name': 'jung'}) ht.add({'id': 23, 'name': 'nam'}) ht.add({'id': 15, 'name': 'ra'}) ht.add({'id': 36, 'name': 'son'}) ht.add({'id': 17, 'name': 'ban'}) ht.add({'id': 27, 'name': 'shin'}) ht.add({'id': 19, 'name': 'choi'}) ht.check_validity() while True: line = raw_input() words = line.split() if words[0] == 's': key = int(words[1]) try: item = ht.search(key) except KeyError: print >> sys.stderr, '* not found' else: print '* found: %s' % item elif words[0] == 'd': key = int(words[1]) try: ht.remove(key) except KeyError: print >> sys.stderr, '* not found' else: print '* removed' ht.check_validity() elif words[0] == 'p': ht.print_table() elif words[0] == 'a': item = {'id': int(words[1]), 'name': 'dummy'} try: ht.add(item) except OverflowError: print >> sys.stderr, '* overflowed' except ValueError: print >> sys.stderr, '* duplicate found' else: print '* added: %s' % item ht.check_validity() elif words[0] == 'q': break else: print >> sys.stderr, '* invalid command'
class MainWindow(QWidget): def __init__(self, parent=None): QWidget.__init__(self, parent) self.table = Table(11, self) self.table.show() self.resize(self.table.width(), self.table.height() + WND_SPACING*3 + BUTT_H*2) self.ht = HashTable(11, lambda x: x, lambda x, n: x % n, logger=self.logger) self.example_g = QPushButton(self) self.example_g.setText(u'예제') self.example_g.move(WND_SPACING, self.table.height() + WND_SPACING*2 + BUTT_H); self.example_g.resize(BUTT_W, BUTT_H) self.example_g.clicked.connect(self.on_start) self.input_g = QLineEdit(self) self.input_g.move(WND_SPACING, self.table.height() + WND_SPACING); self.input_g.resize(BUTT_W, BUTT_H) self.add_g = QPushButton(self) self.add_g.setText(u'추가') self.add_g.move(WND_SPACING*2 + BUTT_W, self.table.height() + WND_SPACING); self.add_g.resize(BUTT_W, BUTT_H) self.add_g.clicked.connect(self.on_add) self.delete_g = QPushButton(self) self.delete_g.setText(u'삭제') self.delete_g.move(WND_SPACING*3 + BUTT_W*2, self.table.height() + WND_SPACING); self.delete_g.resize(BUTT_W, BUTT_H) self.delete_g.clicked.connect(self.on_delete) self.input_g.setFocus() def logger(self, *args): if args[0] == 'a': self.table.add_card(args[1], str(args[2])) elif args[0] == 'm': self.table.move_card(args[1], args[2]) elif args[0] == 'd': self.table.remove_card(args[1]) elif args[0] == 'l': self.table.link_card(args[1], args[2]) def on_start(self, ev): items = [27, 18, 29, 28, 39, 13, 16, 42, 17] for x in items: print '* add %d' % x self.ht.add(x) self.ht.check_validity() self.ht.print_table() for x in items: print '* remove %d' % x self.ht.remove(x) self.ht.check_validity() self.ht.print_table() def on_add(self, ev): try: key = int(self.input_g.text()) if key <= 0: QMessageBox.critical(self, None, 'Only positive numbers are allowed.') return except ValueError: QMessageBox.critical(self, None, 'Invalid key.') return item = key try: self.ht.add(item) except ValueError: QMessageBox.critical(self, None, 'Duplicate found.') return self.ht.check_validity() self.ht.print_table() def on_delete(self, ev): key = int(self.input_g.text()) item = key try: self.ht.remove(item) except KeyError: QMessageBox.critical(self, None, 'Key does not exist.') return self.ht.check_validity() self.ht.print_table()