-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 3.26 make-table generalized with ordered keys.rkt
1619 lines (1476 loc) · 49.5 KB
/
Exercise 3.26 make-table generalized with ordered keys.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket
; Exercise 3.26. To search a table as implemented above, one needs to scan through the list of records.
; This is basically the unordered list representation of section 2.3.3. For large tables, it may be more
; efficient to structure the table in a different manner. Describe a table implementation where the
; (key, value) records are organized using a binary tree, assuming that keys can be ordered in some
; way (e.g., numerically or alphabetically). (Compare exercise 2.66 of chapter 2.)
; S O L U T I O N
(require rnrs/mutable-pairs-6)
(require compatibility/mlist)
; This took more time than I expected probably because of the number of interruptions I had to handle while working
; on this problem. My approach was to replace the list data structure from the previous exercise with a binary tree. This
; was not straightforward because in the previous (Exercise 3.25) implementation there is no clean separation between the
; procedure "make-table" and the list data structure that it uses. Operations such as 'get-first-key-value-pair' and
; 'get-remaining-key-value-pairs' do not make sense for a binary-tree. So I had to redo all of this. So it was a good opportunity
; to introduce a clean separation between the table object and the binary-tree structure that it uses to store its key-value
; pairs. This is exactly what I did. So the generic table level operations like lookup, insert!, find-key-value-pair etc. make no
; assumptions about the underlying data structure. Instead they use the message passing strategy to invoke operations on the
; binary-tree objects which exist in the generic table.
; For key ordering I have used string comparisons. However, the 'same', 'less-than' and 'greater-than' operations
; are passed in as parameters to the make-table procedure so make-table is agnostic to how the keys are ordered.
; I liked the idea of procedures with local state so I continued to use this technique to implement the binary-tree. I found
; the encapsulation that this technique gives, quite useful because it allowed me to think of the table and its contents as
; objects with operations within them.
; I re-wrote the monolithic print procedure from the previous exercise and distributed the print work across the three
; objects: generic table, binary tree and key-value-pair. This made it a little clunky to handle the indentations while
; printing different levels in the generic table but overall, I think this is a huge improvement over the way I wrote
; the solution for Exercise 3.25.
; I am a stickler for separation of concerns and clean interfaces. In this problem, the generic table contains binary trees
; which in turn contain key-value pairs. The key-value pair objects contain generic tables themselves in order to allow
; an arbitrary number of keys for any value. This circular relationship made the print procedure a little unclean as you can
; see where I call the procedure "print-generic-table"
; As you can see in the tests below, my generic table implementation works pretty well. It allows values to be stored under
; an arbitrary number of keys, different values may be stored under different numbers of keys and since I use a binary tree
; to store the values, both the lookups and inserts have a time complexity of (Theta(log n)) where n is the number of
; elements at a given level in the multi-level table.
; Supported table structure is something like:
; TABLE
; -----
; K1: val1
; K2: val2
; K3: val3
; K31: val31
; K32: (no value)
; K321: val321
; K322: val322
; K33: val33
; K4: val4
;
; So the key combinations to retrieve data would be:
;
; K1 retrieves val1
; K2 retrieves val2
; K3 retrieves val3
; K3, K31 retrieves val31
; K3, K32, K321 retrieves val321
; K3, K32, K322 retrieves val322
; K3, K33 retrieves val33
; K4 retrieves val4
; Note: I want to allow for the possibility for not just primitives like string literals and numbers
; but also objects to be inserted and looked up from the table. Since every non-primitive object is
; implemented using pairs, this makes it necessary for us to be able to distinguish between a sub-table
; and a value which is an object. So I am introducing tags so that the table procedures can correctly
; identify the data they handle as they traverse through the table. So a 'key' will not be just a
; string literal but a pair in which the 'car' is a tag with the value 'key and the 'cdr' is the actual key
; Similarly, a 'value' will not just be a string literal but a pair in which the 'car' is a tag with the
; value 'value and the 'cdr' is the actual value which could be a primitive or a complex object
(define (make-table less-than-key? same-key? greater-than-key?)
; Key-Value Pair operations
(define (make-key-value-pair key subtable value)
; A key-value pair is the basic building block of the table. It is a pair in which
; the 'car' is the key and the 'cdr' is another pair in which the 'car' is a pointer to
; a generic table and the 'cdr' is the value. This structure allows us to create an
; arbitrary number of levels in the table. If it is just a simple key-value pair with no
; sub-keys, then the contained generic table will be null. If it is a key with sub-keys
; but no value for this key, then the contained generic table will be non-null and the
; value will be null. And of course, both the contained generic table and the value
; can be non-null to allow for data like:
; K3, K31: val31
; K3, K32, K321: val321
(mcons (make-key key) (mcons subtable (make-value value)))
)
(define (get-key key-value-pair)
(if (mpair? key-value-pair)
(mcdr (mcar key-value-pair))
null
)
)
(define (get-value key-value-pair)
(if (mpair? key-value-pair)
(if (mpair? (mcdr key-value-pair))
(if (mpair? (mcdr (mcdr key-value-pair)))
(mcdr (mcdr (mcdr key-value-pair)))
null
)
null
)
null
)
)
(define (get-subtable key-value-pair)
(if (mpair? key-value-pair)
(if (mpair? (mcdr key-value-pair))
(mcar (mcdr key-value-pair))
null
)
null
)
)
(define (set-value! key-value-pair x)
(set-mcdr! (mcdr key-value-pair) x)
)
(define (set-subtable! key-value-pair subtable)
(set-mcar! (mcdr key-value-pair) subtable)
)
(define (print-key-and-value key-value-pair)
(display (get-key key-value-pair))
(display ": ")
(if (not (null? (get-value key-value-pair)))
(display (get-value key-value-pair))
(void)
)
(newline)
)
(define (make-key key) (mcons 'key key))
(define (make-value value) (mcons 'value value))
; Binary tree implementation
; Each node will be a list of three items: the entry at the node, the left subtree,
; and the right subtree. A left or a right subtree of the empty list will indicate that
; there is no subtree connected there.
(define (make-kvp-binary-tree less-than-key? same-key? greater-than-key?)
(let ((local-tree (mlist null null null)))
(define (empty-tree?)
(if (and (null? (entry)) (null? (left-branch)) (null? (right-branch)))
true
false
)
)
(define (entry) (mcar local-tree))
(define (left-branch)
(if (mpair? (mcdr local-tree))
(mcar (mcdr local-tree))
null
)
)
(define (right-branch)
(if (mpair? (mcdr local-tree))
(if (mpair? (mcdr (mcdr local-tree)))
(mcar (mcdr (mcdr local-tree)))
null
)
null
)
)
; returns the key-value pair in which the key is k. If there is no such key-value-pair
; it returns null
(define (lookup k)
(cond
((null? local-tree) null)
((same-key? k (get-key (entry))) (entry))
((less-than-key? k (get-key (entry)))
(if (not (null? (left-branch)))
(((left-branch) 'lookup) k)
null
)
)
((greater-than-key? k (get-key (entry)))
(if (not (null? (right-branch)))
(((right-branch) 'lookup) k)
null
)
)
)
)
(define (set-tree! entry left right)
(set-entry! entry)
(set-left-branch! left)
(set-right-branch! right)
)
(define (set-entry! entry)
(set-mcar! local-tree entry)
)
(define (set-left-branch! tree)
(set-mcar! (mcdr local-tree) tree)
)
(define (set-right-branch! tree)
(set-mcar! (mcdr (mcdr local-tree)) tree)
)
; Inserting an item into this tree requires (log n) steps. To insert an item x, we compare
; x with the node entry to determine whether x should be added to the right or to the left
; branch, and having inserted x into the appropriate branch we piece this newly constructed
; branch together with the original entry and the other branch. If we are asked to insert x
; into an empty tree, we generate a tree that has x as the entry and empty right and left
; branches. If the tree already contains an element that is 'equal to' the new element,
; then we replace the old element with the new one.
(define (insert-key-value-pair! kvp)
(if (empty-tree?)
; if the tree is empty, then insert the kvp at this node
(set-entry! kvp)
; tree is not empty so look for the right place to insert the kvp
(if (same-key? (get-key kvp) (get-key (entry)))
(set-entry! kvp)
(begin
(if (less-than-key? (get-key kvp) (get-key (entry)))
(begin
(if (null? (left-branch))
(set-left-branch! (make-kvp-binary-tree less-than-key? same-key? greater-than-key?))
(void)
)
(((left-branch) 'insert-key-value-pair!) kvp)
)
(begin
(if (null? (right-branch))
(set-right-branch! (make-kvp-binary-tree less-than-key? same-key? greater-than-key?))
(void)
)
(((right-branch) 'insert-key-value-pair!) kvp)
)
)
)
)
)
)
(define (print-tree indentation-level)
(define (indent indentation-level)
(if (= indentation-level 0)
(void)
(begin
(display " ")
(indent (- indentation-level 1))
)
)
)
; In-order traversal: We want to print in left-to-right order
; print the left branch first, then the entry in this node and then the right branch
(if (not (null? (left-branch)))
(((left-branch) 'print) indentation-level)
(void)
)
(indent indentation-level)
(print-key-and-value (entry))
; print the subtable
(if (not (null? (get-subtable (entry))))
(print-generic-table (get-subtable (entry)) (+ indentation-level 1))
(void)
)
(if (not (null? (right-branch)))
(((right-branch) 'print) indentation-level)
(void)
)
)
(define (dispatch m)
(cond
((eq? m 'entry) entry)
((eq? m 'left-branch) left-branch)
((eq? m 'right-branch) right-branch)
((eq? m 'lookup) lookup)
((eq? m 'insert-key-value-pair!) insert-key-value-pair!)
((eq? m 'print) print-tree)
(else (error "Unknown operation -- KVP-BINARY-TREE" m))
)
)
dispatch
)
)
(define (make-generic-table)
(mlist '*generic-table*)
)
(define (print-generic-table table indentation-level)
(if (mpair? table)
(((mcdr table) 'print) indentation-level)
(void)
)
)
(let ((local-table (make-generic-table)))
(define (get-records table)
(if (mpair? table)
(mcdr table)
null
)
)
(define (insert-at-top-level! table key-value-pair)
(if (null? (get-records table))
(set-mcdr! table (make-kvp-binary-tree less-than-key? same-key? greater-than-key?))
(void)
)
(((get-records table) 'insert-key-value-pair!) key-value-pair)
)
(define (find-key-value-pair key-to-find records)
; Note that 'records' here is a binary tree
; This procedure searches for the key at this level in the table and if it is found,
; returns the key-value pair object. If not found, it returns false
(if (not (null? records))
((records 'lookup) key-to-find)
null
)
)
(define (lookup list-of-keys)
(define (lookup-internal key-list table)
(cond
((null? key-list) false)
((not (pair? key-list)) false)
(else
(let ((key-value-pair (find-key-value-pair (car key-list) (get-records table))))
(if key-value-pair
; Found key
; If this is the last key in the list of keys, then we have found the
; data we are looking for in the table.
(if (null? (cdr key-list))
; we have reached the last key, so return the value if there is one
(if (not (null? (get-value key-value-pair)))
(get-value key-value-pair)
false
)
; we have to look for more keys
(lookup-internal (cdr key-list) (get-subtable key-value-pair))
)
; Could not find key, so stop looking and return false
false
)
)
)
)
)
(lookup-internal list-of-keys local-table)
)
(define (insert! key-list value)
; Logic: Look for the first key from the key-list in the first level of the table
; If found, then look for the 2nd key in the sub-table pointed to by the first key
; and continue this process. If all the keys are found, then the insert operation will
; replace the existing value with the new value.
; If at any level the key is not found, then insert that key at that level in the table and
; continue inserting keys below it and finally the value
(define (insert-internal! list-of-keys value table)
(let ((key-value-pair (find-key-value-pair (car list-of-keys) (get-records table))))
(if (not (null? key-value-pair))
; found the key
; if this is the last key in the list of keys, then we have found the
; correct location in the table. So replace the old value with the new value
(if (null? (cdr list-of-keys))
; we are at the last key, so replace the old value with the new value
(set-value! key-value-pair (make-value value))
; there are more keys so continue
(begin
; If there is no subtable inside this key-value pair, create it
(if (null? (get-subtable key-value-pair))
(set-subtable! key-value-pair (make-generic-table))
(void)
)
(insert-internal! (cdr list-of-keys) value (get-subtable key-value-pair))
)
)
; did not find the key so we need to insert key(s) and the value TODO
(insert-new-keys-and-value list-of-keys value table)
)
)
)
; This procedure is for inserting a new set of hierarchical keys in the supplied
; set of records
(define (insert-new-keys-and-value list-of-keys value table)
(cond
((not (pair? list-of-keys)) (error "Not a list" list-of-keys))
; if this is the last key then we insert the value also and stop
((null? (cdr list-of-keys))
(let ((new-key-value-pair (make-key-value-pair (car list-of-keys) null value)))
(insert-at-top-level! table new-key-value-pair)
)
)
(else
; the tricky part
(let ((new-key-value-pair (make-key-value-pair (car list-of-keys) null null)))
(let ((subtable (make-generic-table)))
(insert-new-keys-and-value (cdr list-of-keys) value subtable)
(set-subtable! new-key-value-pair subtable)
)
(insert-at-top-level! table new-key-value-pair)
)
)
)
)
(if (pair? key-list)
(begin
(insert-internal! key-list value local-table)
'ok
)
(error "Supplied parameter is not a pair! -- INSERT!" key-list)
)
)
(define (print-table)
(displayln "GENERIC-TABLE")
(displayln "-------------")
(((get-records local-table) 'print) 0)
)
(define (dispatch m)
(cond
((eq? m 'lookup-proc) lookup)
((eq? m 'insert-proc!) insert!)
((eq? m 'print) print-table)
(else (error "Unknown operation -- TABLE" m))
)
)
dispatch
)
)
; Test Driver
(define (run-test return-type proc . args)
(define (print-item-list items first-time?)
(cond
((not (pair? items)) (void))
(else
(if (not first-time?)
(display ", ")
(void)
)
(print (car items))
(print-item-list (cdr items) false)
)
)
)
(display "Applying ")
(display proc)
(if (not (null? args))
(begin
(display " on: ")
(print-item-list args true)
)
(void)
)
(newline)
(let ((result (apply proc args)))
(if (not (eq? return-type 'none))
(display "Result: ")
(void)
)
(cond
((procedure? result) ((result 'print)))
; ((eq? return-type 'deque) (print-deque result))
((eq? return-type 'none) (void))
(else
(print result)
(newline)
)
)
)
(newline)
)
(define (execution-time proc . args)
(define start-time (current-milliseconds))
; (display start-time)
; (display " ")
(apply proc args)
(define end-time (current-milliseconds))
; (display end-time)
(display "Execution time of ")
(display proc)
(display ": ")
(- end-time start-time)
)
; Tests
(define (key-equality-comparator key1 key2)
(equal? (to-string key1) (to-string key2))
)
(define (key-less-than-comparator key1 key2)
(string<? (to-string key1) (to-string key2))
)
(define (key-greater-than-comparator key1 key2)
(string>? (to-string key1) (to-string key2))
)
(define (to-string x)
(cond
((null? x) "")
((symbol? x) (symbol->string x))
(else
x
)
)
)
(define T1 (make-table key-less-than-comparator key-equality-comparator key-greater-than-comparator))
(run-test 'unknown (T1 'insert-proc!) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India" "Deccan" "Karnataka" "Bengaluru" "J P Nagar" "Brigade Millennium" "Jacaranda" "Floor 7" "702" "Study Room" "Writing Desk" "Drawer" "Pencil") 'Lead)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Kazhakhistan") 'KZ)
(run-test 'unknown (T1 'lookup-proc) (list "Kazhakhistan"))
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Afghanistan") 'AF)
(run-test 'unknown (T1 'lookup-proc) (list "Kazhakhistan"))
(run-test 'unknown (T1 'lookup-proc) (list "Afghanistan"))
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Turkmenistan") 'TM)
(run-test 'unknown (T1 'lookup-proc) (list "Turkmenistan"))
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Kyrgiztan") 'KY)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "Karnataka") 'KA)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India") 'IN)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu") 'TU)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India" "Deccan") 'Plateau)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Pakistan") 'PK)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Pakistan" "Sindh") 'SD)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Pakistan" "Sindh") 'SN)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu") 'TN)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai") 'CHEN)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Pakistan" "Sindh" "Rawalpindi") 'RAWA)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai" "BesantNagar" "ElliotsBeach") 'ELLIOTS)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Pakistan") 'PoK)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Uzbekistan") 'UB)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "Tajikistan") 'TJ)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai" "BesantNagar" "Kalakshetra") 'Dance)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai" "Mogapper") 'Jikky)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai" "Saidapet") 'Bus-stand)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai" "Adayar") 'A2B)
(run-test 'none (T1 'print))
(run-test 'unknown (T1 'insert-proc!) (list "India" "TamilNadu" "Chennai" "Adayar" "Sangeeta" "Breakfast" "Plate") 'Idli)
(run-test 'none (T1 'print))
(display "Testing the lookup proc")
(newline)
(run-test 'unknown (T1 'lookup-proc) (list "Tajikistan"))
(run-test 'unknown (T1 'lookup-proc) (list "Uzbekistan"))
(run-test 'unknown (T1 'lookup-proc) (list "Burma"))
(run-test 'unknown (T1 'lookup-proc) (list "Pakistan"))
(run-test 'unknown (T1 'lookup-proc) (list "Sindh"))
(run-test 'unknown (T1 'lookup-proc) (list "Pakistan" "Sindh"))
(run-test 'unknown (T1 'lookup-proc) (list "Pakistan" "Sindh" "Rawalpindi"))
(run-test 'unknown (T1 'lookup-proc) (list "Pakistan" "Sindh" "Rawalpindi"))
(run-test 'unknown (T1 'lookup-proc) (list "India"))
(run-test 'unknown (T1 'lookup-proc) (list "TamilNadu"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu" "Chennai"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu" "Chennai" "BesantNagar"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu" "Chennai" "BesantNagar" "ElliotsBeach"))
(run-test 'unknown (T1 'lookup-proc) (list "Karnataka"))
(run-test 'unknown (T1 'lookup-proc) (list "Karnataka" "India"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "Karnataka"))
(run-test 'unknown (T1 'lookup-proc) (list "Kyrgiztan"))
(run-test 'unknown (T1 'lookup-proc) (list "Turkmenistan"))
(run-test 'unknown (T1 'lookup-proc) (list "Kazhakhstan"))
(run-test 'unknown (T1 'lookup-proc) (list "Kazakhstan"))
(run-test 'unknown (T1 'lookup-proc) (list "Kazhakhistan"))
(run-test 'unknown (T1 'lookup-proc) (list "Afghanistan"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu" "Chennai" "Adayar" "Sangeeta" "Breakfast" "Plate"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu" "Chennai" "Adayar"))
(run-test 'unknown (T1 'lookup-proc) (list "India" "TamilNadu" "Chennai" "Adayar" "Sangeeta" "Plate"))
(run-test 'unknown (T1 'lookup-proc) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India" "Deccan" "Karnataka" "Bengaluru" "J P Nagar" "Brigade Millennium" "Jacaranda" "Floor 7" "702" "Study Room" "Writing Desk" "Drawer" "Pencil"))
(run-test 'unknown (T1 'lookup-proc) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India" "Deccan"))
(run-test 'unknown (T1 'lookup-proc) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India"))
(run-test 'unknown (T1 'insert-proc!) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India") 'Dharmarajya)
(run-test 'unknown (T1 'lookup-proc) (list "Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India"))
; Test Results
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 512 MB.
Applying #<procedure:insert!> on: '("Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India" "Deccan" "Karnataka" "Bengaluru" "J P Nagar" "Brigade Millennium" "Jacaranda" "Floor 7" "702" "Study Room" "Writing Desk" "Drawer" "Pencil"), 'Lead
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
Applying #<procedure:insert!> on: '("Kazhakhistan"), 'KZ
Result: 'ok
Applying #<procedure:lookup> on: '("Kazhakhistan")
Result: 'KZ
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
Kazhakhistan: KZ
Applying #<procedure:insert!> on: '("Afghanistan"), 'AF
Result: 'ok
Applying #<procedure:lookup> on: '("Kazhakhistan")
Result: 'KZ
Applying #<procedure:lookup> on: '("Afghanistan")
Result: 'AF
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
Kazhakhistan: KZ
Applying #<procedure:insert!> on: '("Turkmenistan"), 'TM
Result: 'ok
Applying #<procedure:lookup> on: '("Turkmenistan")
Result: 'TM
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
Kazhakhistan: KZ
Turkmenistan: TM
Applying #<procedure:insert!> on: '("Kyrgiztan"), 'KY
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
Kazhakhistan: KZ
Kyrgiztan: KY
Turkmenistan: TM
Applying #<procedure:insert!> on: '("India" "Karnataka"), 'KA
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
India:
Karnataka: KA
Kazhakhistan: KZ
Kyrgiztan: KY
Turkmenistan: TM
Applying #<procedure:insert!> on: '("India"), 'IN
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
India: IN
Karnataka: KA
Kazhakhistan: KZ
Kyrgiztan: KY
Turkmenistan: TM
Applying #<procedure:insert!> on: '("India" "TamilNadu"), 'TU
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan:
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
India: IN
Karnataka: KA
TamilNadu: TU
Kazhakhistan: KZ
Kyrgiztan: KY
Turkmenistan: TM
Applying #<procedure:insert!> on: '("Andromeda" "Milky Way" "Outer Spiral" "Sun Solar System" "Earth" "Asia" "South Asia" "India" "Deccan"), 'Plateau
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan: Plateau
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
India: IN
Karnataka: KA
TamilNadu: TU
Kazhakhistan: KZ
Kyrgiztan: KY
Turkmenistan: TM
Applying #<procedure:insert!> on: '("Pakistan"), 'PK
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan: Plateau
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
India: IN
Karnataka: KA
TamilNadu: TU
Kazhakhistan: KZ
Kyrgiztan: KY
Pakistan: PK
Turkmenistan: TM
Applying #<procedure:insert!> on: '("Pakistan" "Sindh"), 'SD
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia:
South Asia:
India:
Deccan: Plateau
Karnataka:
Bengaluru:
J P Nagar:
Brigade Millennium:
Jacaranda:
Floor 7:
702:
Study Room:
Writing Desk:
Drawer:
Pencil: Lead
India: IN
Karnataka: KA
TamilNadu: TU
Kazhakhistan: KZ
Kyrgiztan: KY
Pakistan: PK
Sindh: SD
Turkmenistan: TM
Applying #<procedure:insert!> on: '("Pakistan" "Sindh"), 'SN
Result: 'ok
Applying #<procedure:print-table>
GENERIC-TABLE
-------------
Afghanistan: AF
Andromeda:
Milky Way:
Outer Spiral:
Sun Solar System:
Earth:
Asia: