#!/usr/local/bin/python3 # Author: Dr. Robert Heckendorn, Computer Science Department, University of Idaho, 2013 import sys # ensemble performance # functionality of a symbol table: # 1. make symbol table # 2. length of symbol table # 2. list of keys # 2. insert a new item by key # 3. retrieve value based on key if not there return default # 4. remove a specific item by key # 5. sort the symbol table by the keys # 6. join one symbol table to another # 7. support duplicate keys with different information or only one copy? # # associative memory: LISP, Icon, Perl, Python # def makesymt() : return {} def length(st) : return len(st) def keys(st) : return st.keys() def insert(st, key, value) : st[key] = value def retrieve(st, key, default) : return st.get(key, default) def remove(st, key) : if key in st : del(st[key]) def comparebykey(item) : return item[0] def comparebyvalue(item) : return item[1] def sortbykeys(st) : ans = list(st.items()) ans.sort(key=comparebykey) return ans def sortbyvalues(st) : ans = list(st.items()) ans.sort(key=comparebyvalue) return ans def join(st1, st2) : st1.update(st2) def printsymbols(st) : syms = list(st.keys()) syms.sort() for s in syms : print(s, st[s])