-
Notifications
You must be signed in to change notification settings - Fork 3
/
4digit-base36-timestamp.html
1496 lines (1408 loc) · 101 KB
/
4digit-base36-timestamp.html
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
<!DOCTYPE html><html><head><meta charset="utf-8" /><title>Encoding four base 36 digits as a timestamp - The Terminal Programmer</title><meta content="2015-04-25T22:01:36-07:00" name="DCTERMS.created" /><meta content="2015-05-10T17:43:38-07:00" name="DCTERMS.modified" /><meta content="Suraj N. Kurapati" name="author" /><meta content="solution, algorithm, compression" name="keywords" /><meta content="width=device-width, initial-scale=1" name="viewport" /><meta content="Readably https://github.com/sunaku/readably" name="generator" /><link href="style.css" rel="stylesheet" type="text/css" /><link href="index.atom" rel="alternate" title="feed" type="application/atom+xml" /><script src="js/jquery.slim.min.js"></script></head><body><article data-entry-id="4digit-base36-timestamp" id="body"><header><div class="navigation"><a class="rootlink" href="index.html#4digit-base36-timestamp" title="The Terminal Programmer"><span>The Terminal Programmer</span></a></div><h1 class="title">Encoding four base 36 digits as a timestamp</h1><div class="author">Suraj N. Kurapati</div><time class="date" datetime="2015-04-25T22:01:36-07:00">25 April 2015</time><br /><time class="date" datetime="2015-05-10T17:43:38-07:00"><a href="#updates" title="1 update">10 May 2015</a></time></header><hr /><div class="description"></div><div class="content"><ol class="table-of-contents"><li><a id="__problem__" href="#problem" class="downlink">Problem</a><ol></ol></li><li><a id="__approach__" href="#approach" class="downlink">Approach</a><ol></ol></li><li><a id="__solution__" href="#solution" class="downlink">Solution</a><ol></ol></li><li><a id="__evaluation__" href="#evaluation" class="downlink">Evaluation</a><ol></ol></li><li><a id="__comparison__" href="#comparison" class="downlink">Comparison</a><ol></ol></li><li><a id="__behind-the-scenes__" href="#behind-the-scenes" class="downlink">Behind the scenes</a><ol><li><a id="__comparison-rb__" href="#comparison-rb" class="downlink">comparison.rb</a><ol></ol></li></ol></li></ol>
<div id="problem" class="section"></div><h1 class="heading">Problem<a href="#problem" class="permalink" title="Permalink"></a><a href="#__problem__" class="uplink" title="Contents"></a></h1>
<p>Convert a string containing 4 uppercase alphanumeric characters, whose
values range from <em>A</em> through <em>Z</em> and <em>0</em> through <em>9</em>, into a “HH:MM:SS”
(24 hours, 60 minutes, 60 seconds) timestamp whose value ranges from
“00:00:00” through “23:59:59”.</p>
<p>The string has 1679616 possible values (36 × 36 × 36 ×
36) whereas the timestamp has 86400 possible values (24 × 60 ×
60). Thus the challenge is to convey more information using less.</p>
<p>How would <em>you</em> solve this problem? Pause for a moment to think about it.
In fact, go now and implement your solution <em>before</em> reading about my
particular approach and solution below, as this is quite a fun puzzle! :)</p>
<div id="approach" class="section"></div><h1 class="heading">Approach<a href="#approach" class="permalink" title="Permalink"></a><a href="#__approach__" class="uplink" title="Contents"></a></h1>
<p>Each character in the input string can be represented as a base 36 digit:</p>
<ul>
<li>Characters <em>0</em> through <em>9</em> become integers 0 through 9 respectively.</li>
<li>Characters <em>A</em> through <em>Z</em> become integers 10 through 35 respectively.</li>
</ul>
<p>Let’s examine the base 36 and binary representations of each character:</p>
<table><thead>
<tr>
<th>Character</th>
<th>Base 36</th>
<th>Bit 6</th>
<th>Bit 5</th>
<th>Bit 4</th>
<th>Bit 3</th>
<th>Bit 2</th>
<th>Bit 1</th>
</tr>
</thead><tbody>
<tr>
<td><em>0</em></td>
<td>0</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>0</td>
</tr>
<tr>
<td><em>1</em></td>
<td>1</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
</tr>
<tr>
<td><em>2</em></td>
<td>2</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>3</em></td>
<td>3</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>4</em></td>
<td>4</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>5</em></td>
<td>5</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>6</em></td>
<td>6</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>7</em></td>
<td>7</td>
<td>.</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>8</em></td>
<td>8</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>9</em></td>
<td>9</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>A</em></td>
<td>10</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>B</em></td>
<td>11</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>C</em></td>
<td>12</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>D</em></td>
<td>13</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>E</em></td>
<td>14</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>F</em></td>
<td>15</td>
<td>.</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>G</em></td>
<td>16</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>H</em></td>
<td>17</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>I</em></td>
<td>18</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>J</em></td>
<td>19</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>K</em></td>
<td>20</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>L</em></td>
<td>21</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>M</em></td>
<td>22</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>N</em></td>
<td>23</td>
<td>.</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>O</em></td>
<td>24</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>P</em></td>
<td>25</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>Q</em></td>
<td>26</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>R</em></td>
<td>27</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>S</em></td>
<td>28</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>T</em></td>
<td>29</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>U</em></td>
<td>30</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>V</em></td>
<td>31</td>
<td>.</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td><em>W</em></td>
<td>32</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td><em>X</em></td>
<td>33</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td><em>Y</em></td>
<td>34</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td><em>Z</em></td>
<td>35</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
</tbody></table>
<p>Notice that bit 6 is present in only 4 characters, <em>W</em> through <em>Z</em>, and
absent in all others. Thus, if we randomly picked a character from the
set of 36 possible characters, the probability of finding bit 6 in our
pick is 4 ÷ 36, or 0.1111111111111111, which approximately denotes a
11% chance of success.</p>
<p>In contrast, notice that bit 1 is present in <em>every</em> character. Thus, if
we randomly picked a character from the set of 36 possible characters, the
probability of finding bit 1 in our pick is 36 ÷ 36, or 1, which
denotes a 100% chance of success.</p>
<p>These facts are recorded in the table below.</p>
<table><thead>
<tr>
<th>Bit</th>
<th>Number of characters present in</th>
<th>Probability of presence</th>
</tr>
</thead><tbody>
<tr>
<td>6</td>
<td>4</td>
<td>0.1111111111111111</td>
</tr>
<tr>
<td>5</td>
<td>20</td>
<td>0.5555555555555556</td>
</tr>
<tr>
<td>4</td>
<td>28</td>
<td>0.7777777777777778</td>
</tr>
<tr>
<td>3</td>
<td>32</td>
<td>0.8888888888888888</td>
</tr>
<tr>
<td>2</td>
<td>34</td>
<td>0.9444444444444444</td>
</tr>
<tr>
<td>1</td>
<td>36</td>
<td>1</td>
</tr>
</tbody></table>
<p>The timestamp, with its 86400 possible values, <a href="https://en.wikipedia.org/wiki/Pigeonhole_principle">cannot</a> represent all
1679616 values possible in the string <em>unless</em> we reuse some of them. That
is, instead of mapping each unique string value to exactly one unique
timestamp value, we map <em>multiple</em> unique string values to the <em>same</em>
unique timestamp value, thereby reusing it.</p>
<p>This reuse happens automatically when we discard information. For example,
if we discard the fourth character from two different strings “ABCD” and
“ABCX”, then they would both become “ABC”. Next, if the string “ABC” were
mapped to the timestamp “01:02:03”, then both strings “ABCD” and “ABCX”
would map to the same “01:02:03” timestamp, thereby achieving reuse.</p>
<p>We can discard information <em>strategically</em> based on the probabilities
listed in the table above. In particular, we can discard bits with lower
presence probabilities with less risk of information loss because
discarding nonexistent bits from a value does not change it. For example,
bit 6 is present 11% of the time, so we only risk information loss 11% of
the time by discarding it. In contrast, bit 1 is present 100% of the time,
so we risk information loss 100% of the time by discarding it.</p>
<p>In binary, the string occupies 24 bits (4 characters × 6 bits per
character) and the timestamp occupies at least 16 bits (2<sup>16</sup> < 86400
possible values < 2<sup>17).</sup> Therefore, we need to discard at least 8 bits
(24 string bits - 16 timestamp bits) of information from the string to fit
it within the timestamp’s 16 bits. This translates to 2 bits (8 bits
discarded ÷ 4 characters) discarded per character.</p>
<p>Since lower bits are more likely to be present than higher ones, as the
table above shows, we should keep as many lower bits as possible and
discard as few higher bits as necessary. Thus, we shall discard the upper
2 bits (bit 6 and 5) from each character in the string to fit the string
within the timestamp.</p>
<div id="solution" class="section"></div><h1 class="heading">Solution<a href="#solution" class="permalink" title="Permalink"></a><a href="#__solution__" class="uplink" title="Contents"></a></h1>
<p>In the <a href="https://www.ruby-lang.org/en/">Ruby</a> programming language:</p>
<div class="highlight"><pre class="highlight ruby"><code>
<span class="c1"># Converts the given string into a "HH:MM:SS" timestamp.</span>
<span class="k">def</span> <span class="nf">solution</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
<span class="n">digits</span> <span class="o">=</span> <span class="n">digits36</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
<span class="n">num_seconds</span> <span class="o">=</span> <span class="n">compress</span><span class="p">(</span><span class="o">*</span><span class="n">digits</span><span class="p">)</span>
<span class="no">Time</span><span class="p">.</span><span class="nf">at</span><span class="p">(</span><span class="n">num_seconds</span><span class="p">).</span><span class="nf">utc</span><span class="p">.</span><span class="nf">strftime</span><span class="p">(</span><span class="s1">'%H:%M:%S'</span><span class="p">)</span>
<span class="k">end</span>
<span class="c1"># Converts each character in the given string into a base 36 digit.</span>
<span class="k">def</span> <span class="nf">digits36</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
<span class="n">string</span><span class="p">.</span><span class="nf">each_char</span><span class="p">.</span><span class="nf">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">char</span><span class="o">|</span> <span class="n">digit36</span><span class="p">(</span><span class="n">char</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>
<span class="c1"># Converts the given character into a base 36 digit:</span>
<span class="c1"># decimal digits (0..9) + uppercase letters (10..35)</span>
<span class="k">def</span> <span class="nf">digit36</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="k">case</span> <span class="n">char</span>
<span class="k">when</span> <span class="s1">'0'</span><span class="o">..</span><span class="s1">'9'</span> <span class="k">then</span> <span class="n">char</span><span class="p">.</span><span class="nf">ord</span> <span class="o">-</span> <span class="s1">'0'</span><span class="p">.</span><span class="nf">ord</span>
<span class="k">when</span> <span class="s1">'A'</span><span class="o">..</span><span class="s1">'Z'</span> <span class="k">then</span> <span class="n">char</span><span class="p">.</span><span class="nf">ord</span> <span class="o">-</span> <span class="s1">'A'</span><span class="p">.</span><span class="nf">ord</span> <span class="o">+</span> <span class="mi">10</span>
<span class="k">else</span> <span class="k">raise</span> <span class="no">ArgumentError</span><span class="p">,</span> <span class="s2">"not a base36 digit: </span><span class="si">#{</span><span class="n">char</span><span class="p">.</span><span class="nf">inspect</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="c1"># Combines the given base 36 digits together into a 16-bit integer.</span>
<span class="no">SECONDS_PER_DAY</span> <span class="o">=</span> <span class="mi">24</span> <span class="o">*</span> <span class="mi">60</span> <span class="o">*</span> <span class="mi">60</span> <span class="c1"># hours * minutes/hour * seconds/minute</span>
<span class="no">SECONDS_PER_DAY_BITS</span> <span class="o">=</span> <span class="no">Math</span><span class="p">.</span><span class="nf">log2</span><span class="p">(</span><span class="no">SECONDS_PER_DAY</span><span class="p">).</span><span class="nf">ceil</span> <span class="c1"># 2 ** exponent</span>
<span class="k">def</span> <span class="nf">compress</span><span class="p">(</span><span class="o">*</span><span class="n">chars</span><span class="p">)</span>
<span class="k">return</span> <span class="mi">0</span> <span class="k">if</span> <span class="n">chars</span><span class="p">.</span><span class="nf">empty?</span>
<span class="n">bits</span> <span class="o">=</span> <span class="no">SECONDS_PER_DAY_BITS</span> <span class="o">/</span> <span class="n">chars</span><span class="p">.</span><span class="nf">length</span>
<span class="n">mask</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o"><<</span> <span class="n">bits</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
<span class="n">chars</span><span class="p">.</span><span class="nf">inject</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">acc</span><span class="p">,</span> <span class="n">char</span><span class="o">|</span>
<span class="c1"># grab lower bits from each character and place them side by side</span>
<span class="p">(</span><span class="n">acc</span> <span class="o"><<</span> <span class="n">bits</span><span class="p">)</span> <span class="o">|</span> <span class="p">(</span><span class="n">char</span> <span class="o">&</span> <span class="n">mask</span><span class="p">)</span>
<span class="k">end</span> <span class="o">%</span> <span class="no">SECONDS_PER_DAY</span>
<span class="k">end</span>
</code></pre></div><p>Save the above code to a <code>solution.rb</code> file to play with it:</p>
<div class="highlight"><pre class="highlight ruby"><code>
<span class="err">$</span> <span class="n">irb</span> <span class="o">--</span><span class="n">simple</span><span class="o">-</span><span class="n">prompt</span> <span class="o">-</span><span class="n">r</span> <span class="p">.</span><span class="nf">/</span><span class="n">solution</span><span class="p">.</span><span class="nf">rb</span>
<span class="c1">## ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]</span>
<span class="o">>></span> <span class="n">solution</span> <span class="s2">"ABCD"</span>
<span class="s2">"12:13:01"</span>
<span class="o">>></span> <span class="n">digits36</span> <span class="s2">"ABCD"</span>
<span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">]</span>
<span class="o">>></span> <span class="n">compress</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span>
<span class="mi">43981</span>
<span class="o">>></span> <span class="no">Time</span><span class="p">.</span><span class="nf">at</span><span class="p">(</span><span class="mi">43981</span><span class="p">).</span><span class="nf">utc</span>
<span class="mi">1970</span><span class="o">-</span><span class="mo">01</span><span class="o">-</span><span class="mo">01</span> <span class="mi">12</span><span class="p">:</span><span class="mi">13</span><span class="p">:</span><span class="mo">01</span> <span class="no">UTC</span>
</code></pre></div><p>The solution above is generic to any number of characters, so try more:</p>
<div class="highlight"><pre class="highlight ruby"><code>
<span class="err">$</span> <span class="n">irb</span> <span class="o">--</span><span class="n">simple</span><span class="o">-</span><span class="n">prompt</span> <span class="o">-</span><span class="n">r</span> <span class="p">.</span><span class="nf">/</span><span class="n">solution</span><span class="p">.</span><span class="nf">rb</span>
<span class="c1">## ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]</span>
<span class="o">>></span> <span class="n">solution</span> <span class="s2">"ABCDEFG"</span>
<span class="s2">"03:09:32"</span>
<span class="o">>></span> <span class="n">digits36</span> <span class="s2">"ABCDEFG"</span>
<span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">16</span><span class="p">]</span>
<span class="o">>></span> <span class="n">compress</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">16</span>
<span class="mi">11372</span>
<span class="o">>></span> <span class="no">Time</span><span class="p">.</span><span class="nf">at</span><span class="p">(</span><span class="mi">11372</span><span class="p">).</span><span class="nf">utc</span>
<span class="mi">1970</span><span class="o">-</span><span class="mo">01</span><span class="o">-</span><span class="mo">01</span> <span class="mo">03</span><span class="p">:</span><span class="mi">09</span><span class="p">:</span><span class="mi">32</span> <span class="no">UTC</span>
</code></pre></div>
<div id="evaluation" class="section"></div><h1 class="heading">Evaluation<a href="#evaluation" class="permalink" title="Permalink"></a><a href="#__evaluation__" class="uplink" title="Contents"></a></h1>
<p>To evaluate how well the algorithm performs, we feed it different sets of
input strings and analyze its output: the more input strings it maps to
unique timestamps, the smaller the reuse probability and the better it is.</p>
<table><thead>
<tr>
<th>Algorithm (f : string → timestamp) characteristics</th>
<th>Best case</th>
<th>Worst case</th>
<th>No case</th>
</tr>
</thead><tbody>
<tr>
<td>Number of strings (domain)</td>
<td>65536</td>
<td>160000</td>
<td>1679616</td>
</tr>
<tr>
<td>Number of timestamps (codomain)</td>
<td>65536</td>
<td>65536</td>
<td>65536</td>
</tr>
<tr>
<td>Number of unique timestamps</td>
<td>65536</td>
<td>20736</td>
<td>0</td>
</tr>
<tr>
<td>Number of timestamps reused</td>
<td>0</td>
<td>44800</td>
<td>65536</td>
</tr>
<tr>
<td>Probability of timestamp reuse</td>
<td>0</td>
<td>0.68359375</td>
<td>1</td>
</tr>
<tr>
<td>Number of strings mapped to reused timestamps</td>
<td>0</td>
<td>139264</td>
<td>1679616</td>
</tr>
<tr>
<td>Probability of string mapped to reused timestamp</td>
<td>0</td>
<td>0.8704</td>
<td>1</td>
</tr>
</tbody></table>
<p>In the best case, where all input characters fed into the algorithm lack
the upper bits it discards, information is <em>never</em> lost. Therefore, the
probability of timestamp reuse is zero.</p>
<p>In the worst case, where all input characters fed into the algorithm
contain the upper bits it discards, some information is <em>always</em> lost.
Therefore, the probability of timestamp reuse is greater than zero.</p>
<p>In the “no” case, where some input characters fed into the algorithm
contain the upper bits it discards, information is <em>sometimes</em> lost.
However, since this input set contains <em>all</em> 1679616 possible input
strings, timestamps will <em>always</em> be reused: the timestamp, with its 86400
possible values, <a href="https://en.wikipedia.org/wiki/Pigeonhole_principle">cannot</a> represent all 1679616 values possible in the
string <em>unless</em> we reuse some of them.</p>
<p>This observation applies to any such algorithm.</p>
<div id="comparison" class="section"></div><h1 class="heading">Comparison<a href="#comparison" class="permalink" title="Permalink"></a><a href="#__comparison__" class="uplink" title="Contents"></a></h1>
<p>My solution to this problem is based on probabilities. However, there are
many other solutions, some of which are known as <a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx">hash functions</a>.
Let’s compare them, along with my solution, to see how well they perform.</p>
<p>To compare these algorithms fairly, we need to feed them the <em>same</em> set of
strings. Unlike the singular evaluation of my solution above, here we
don’t have the luxury of partitioning the input space into best and worst
cases up front, because such cases <em>vary</em> wildly across these algorithms.</p>
<p>Here, I have chosen to feed all <a href="http://mathworld.wolfram.com/k-Subset.html">4-subsets</a> of the 36 possible uppercase
alphanumeric characters, which produces 58905 unique strings containing
4 unique uppercase alphanumeric characters each, into these algorithms.
Although this choice poorly approximates reality, wherein strings <em>may</em>
contain repeated characters, some such sacrifice is necessary to produce
a non-exhaustive set of inputs with which to compare these algorithms.</p>
<p>The results are recorded in the table below, sorted by reuse probabilities
in ascending order: from the best result first to the worst result last.</p>
<p>The key result to notice here is that <a href="#solution">my solution</a> had the
best performance among all of these algorithms. I was delighted to see
this because I expected it fare far worse than <a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx">real algorithms</a>. :-)</p>
<p>One surprising result is that the ∅ solution, which simply discards
the fourth character and doesn’t bother with any of this algorithmic
nonsense, performs better than half of these algorithms. Although its
performance isn’t praiseworthy, it nevertheless hints that perhaps,
sometimes, ignoring a problem can be just as effective as solving it.</p>
<p>Augh! What am I saying? Those are not the words of a true engineer!
Problems must <em>always</em> be solved and solved well, so long as it’s fun. ;-)
Anyway, that’s all folks. I hope you enjoyed this algorithmic treatise.
<em>But wait!</em> Don’t leave just yet because there are some fun <a href="#behind-the-scenes">behind the
scenes</a> goodies waiting for you below! :-)</p>
<table><thead>
<tr>
<th>Algorithm (f : string → timestamp)</th>
<th>Number of strings (domain)</th>
<th>Number of timestamps (codomain)</th>
<th>Number of unique timestamps</th>
<th>Number of timestamps reused</th>
<th>Probability of timestamp reuse</th>
<th>Number of strings mapped to reused timestamps</th>
<th>Probability of string mapped to reused timestamp</th>
</tr>
</thead><tbody>
<tr>
<td><a href="#solution">My solution</a></td>
<td>58905</td>
<td>39531</td>
<td>34690</td>
<td>4841</td>
<td>0.12246085350737396</td>
<td>24215</td>
<td>0.411085646379764</td>
</tr>
<tr>
<td>Preserve bits 4,3,2,1</td>
<td>58905</td>
<td>39531</td>
<td>34690</td>
<td>4841</td>
<td>0.12246085350737396</td>
<td>24215</td>
<td>0.411085646379764</td>
</tr>
<tr>
<td><a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx">Rotating hash</a></td>
<td>58905</td>
<td>42936</td>
<td>34649</td>
<td>8287</td>
<td>0.193008198248556</td>
<td>24256</td>
<td>0.4117816823699177</td>
</tr>
<tr>
<td><a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx">Bernstein XOR hash</a></td>
<td>58905</td>
<td>46718</td>
<td>35673</td>
<td>11045</td>
<td>0.23641851106639838</td>
<td>23232</td>
<td>0.39439775910364144</td>
</tr>
<tr>
<td><a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx">Shift-Add-XOR hash</a></td>
<td>58905</td>
<td>44623</td>
<td>32440</td>
<td>12183</td>
<td>0.2730206395804854</td>
<td>26465</td>
<td>0.4492827434003905</td>
</tr>
<tr>
<td>Preserve bits 4,3,2,1 then add bits 6,5</td>
<td>58905</td>
<td>40651</td>
<td>29341</td>
<td>11310</td>
<td>0.2782219379597058</td>
<td>29564</td>
<td>0.5018928783634666</td>
</tr>
<tr>
<td><a href="http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx">Bernstein hash</a></td>
<td>58905</td>
<td>43281</td>
<td>28707</td>
<td>14574</td>
<td>0.33672974284327994</td>
<td>30198</td>
<td>0.5126559714795009</td>
</tr>
<tr>
<td><a href="http://www.cse.yorku.ca/%7Eoz/hash.html">djb2 hash</a></td>
<td>58905</td>
<td>43281</td>
<td>28707</td>
<td>14574</td>
<td>0.33672974284327994</td>
<td>30198</td>
<td>0.5126559714795009</td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/Pearson_hashing">Pearson hash</a></td>
<td>58905</td>
<td>38838</td>
<td>23805</td>
<td>15033</td>
<td>0.3870693650548432</td>
<td>35100</td>
<td>0.5958747135217723</td>
</tr>
<tr>
<td><a href="http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1a">FNV-1a hash</a></td>
<td>58905</td>
<td>36225</td>
<td>21085</td>
<td>15140</td>
<td>0.4179434092477571</td>
<td>37820</td>
<td>0.6420507596978186</td>
</tr>
<tr>
<td><a href="http://www.isthe.com/chongo/tech/comp/fnv/#FNV-1">FNV-1 hash</a></td>
<td>58905</td>
<td>35338</td>
<td>20229</td>
<td>15109</td>
<td>0.42755673778934855</td>
<td>38676</td>
<td>0.6565826330532213</td>
</tr>
<tr>
<td>Preserve bits 4,3,2,1 then XOR bits 6,5</td>
<td>58905</td>
<td>29003</td>
<td>15230</td>
<td>13773</td>
<td>0.47488190876805847</td>
<td>43675</td>
<td>0.7414480943892708</td>
</tr>
<tr>
<td>Preserve bits 5,3,2,1</td>
<td>58905</td>
<td>21243</td>
<td>6706</td>
<td>14537</td>
<td>0.6843195405545356</td>
<td>52199</td>
<td>0.8861556743909685</td>
</tr>
<tr>
<td><a href="http://cpansearch.perl.org/src/MOOLI/Algorithm-Nhash-0.002/lib/Algorithm/Nhash.pm">Exim nhash</a></td>
<td>58905</td>
<td>7401</td>
<td>1280</td>
<td>6121</td>
<td>0.8270503985947845</td>
<td>57625</td>
<td>0.9782700959171547</td>
</tr>
<tr>
<td>Merge fourth character into others</td>
<td>58905</td>
<td>9467</td>
<td>1477</td>
<td>7990</td>
<td>0.8439843667476498</td>
<td>57428</td>
<td>0.9749257278669043</td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/Fletcher%27s_checksum">Fletcher16 checksum</a></td>
<td>58905</td>
<td>4935</td>
<td>621</td>
<td>4314</td>
<td>0.8741641337386018</td>
<td>58284</td>
<td>0.9894576012223071</td>
</tr>
<tr>
<td>Preserve bits 5,4,2,1</td>
<td>58905</td>
<td>12931</td>
<td>1610</td>
<td>11321</td>
<td>0.8754930013146702</td>
<td>57295</td>
<td>0.9726678550207962</td>
</tr>
<tr>
<td><a href="http://www.cse.yorku.ca/%7Eoz/hash.html">sdbm hash</a></td>
<td>58905</td>
<td>3343</td>
<td>331</td>
<td>3012</td>
<td>0.9009871373018247</td>
<td>58574</td>
<td>0.9943807826160768</td>
</tr>
<tr>
<td>Discard fourth character (∅ solution)</td>
<td>58905</td>
<td>6545</td>
<td>561</td>
<td>5984</td>
<td>0.9142857142857143</td>
<td>58344</td>
<td>0.9904761904761905</td>
</tr>
<tr>
<td>Preserve bits 6,3,2,1</td>
<td>58905</td>
<td>6561</td>
<td>331</td>
<td>6230</td>
<td>0.9495503734186862</td>
<td>58574</td>
<td>0.9943807826160768</td>
</tr>
<tr>
<td>Preserve bits 5,4,3,1</td>
<td>58905</td>
<td>7191</td>
<td>327</td>
<td>6864</td>
<td>0.9545264914476429</td>
<td>58578</td>
<td>0.9944486885663356</td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/BSD_checksum">BSD checksum</a></td>
<td>58905</td>
<td>464</td>
<td>21</td>
<td>443</td>
<td>0.9547413793103449</td>
<td>58884</td>
<td>0.9996434937611408</td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/Longitudinal_redundancy_check">LRC checksum</a></td>
<td>58905</td>
<td>129</td>
<td>4</td>
<td>125</td>
<td>0.9689922480620154</td>
<td>58901</td>
<td>0.9999320940497411</td>
</tr>
<tr>
<td><a href="http://www.cse.yorku.ca/%7Eoz/hash.html">lose lose hash</a></td>
<td>58905</td>
<td>129</td>
<td>4</td>
<td>125</td>
<td>0.9689922480620154</td>
<td>58901</td>
<td>0.9999320940497411</td>
</tr>
<tr>
<td>Preserve bits 6,4,2,1</td>
<td>58905</td>
<td>6145</td>
<td>171</td>
<td>5974</td>
<td>0.9721724979658258</td>
<td>58734</td>
<td>0.9970970206264324</td>
</tr>
<tr>
<td>Preserve bits 6,5,2,1</td>
<td>58905</td>
<td>2625</td>
<td>71</td>
<td>2554</td>
<td>0.9729523809523809</td>
<td>58834</td>
<td>0.9987946693829047</td>
</tr>
<tr>
<td>Preserve bits 5,4,3,2</td>
<td>58905</td>
<td>5655</td>
<td>150</td>
<td>5505</td>
<td>0.9734748010610079</td>
<td>58755</td>
<td>0.9974535268652915</td>
</tr>
<tr>
<td>Preserve bits 6,5,3,1</td>
<td>58905</td>
<td>1953</td>
<td>39</td>
<td>1914</td>
<td>0.9800307219662059</td>
<td>58866</td>
<td>0.9993379169849758</td>
</tr>
<tr>
<td>Preserve bits 6,5,4,1</td>
<td>58905</td>
<td>1073</td>
<td>21</td>
<td>1052</td>
<td>0.9804287045666356</td>
<td>58884</td>
<td>0.9996434937611408</td>
</tr>
<tr>
<td>Preserve bits 6,5,4,3</td>
<td>58905</td>
<td>495</td>
<td>9</td>
<td>486</td>
<td>0.9818181818181818</td>
<td>58896</td>
<td>0.9998472116119175</td>
</tr>
<tr>
<td>Preserve bits 6,4,3,1</td>
<td>58905</td>
<td>4193</td>
<td>75</td>
<td>4118</td>
<td>0.9821130455521107</td>
<td>58830</td>
<td>0.9987267634326458</td>
</tr>
<tr>
<td><a href="https://en.wikipedia.org/wiki/Jenkins_hash_function#one-at-a-time">Jenkins one-at-a-time hash</a></td>
<td>58905</td>
<td>9582</td>
<td>127</td>
<td>9455</td>
<td>0.9867459820496765</td>
<td>58778</td>
<td>0.9978439860792802</td>
</tr>
<tr>
<td>Preserve bits 6,5,4,2</td>
<td>58905</td>
<td>1005</td>
<td>13</td>
<td>992</td>
<td>0.9870646766169154</td>
<td>58892</td>
<td>0.9997793056616586</td>
</tr>
<tr>
<td>Preserve bits 6,5,3,2</td>
<td>58905</td>
<td>1749</td>
<td>21</td>
<td>1728</td>
<td>0.9879931389365352</td>
<td>58884</td>
<td>0.9996434937611408</td>
</tr>
<tr>
<td>Preserve bits 6,4,3,2</td>