English 中文(简体)
良好的算法将时间序列改为较短的字母数字。
原标题:Good algorithm to convert timestamp to a shorter alphanumeric representation
  • 时间:2011-03-23 13:06:55
  •  标签:
  • algorithm

采用包含年数、月、日、小时、分钟、二字并改成七位数或更少(但一致)字母数字的时段算法是好的。 字母数字代表对上下级的字体表示不一。

最佳回答

让我们做一些数学

You can use 7 alphanumeric digits. Each alphanumeric digit take a value from 36 possible different values (26 letters, 10 decimal digits) So we have 36^7 different values, that is 78364164096.

Now we compute the number of different values needed to represent a given timestamp in one year. To simplify things a bit we will allow some values that will never happen (ex: 31th november).

因此,我们已经

month: 12  -> coded from 0 to 11
day: 31  -> coded from 0 to 30
hour: 24
minute: 60
second: 60 

which gives use 32140800 different possibilites

We now divide 78364164096 / 32140800 which is ~2438, thus we will give an enumeration of timestamps from 00:00 jan 1 0000 to 23:59 dec 31 2437

当时编码正在编码。

X = second + minute*60 + hour*60*60 + 
    day*60*60*24 + month*60*60*24*31 + 
    year*60*60*24*31*12

而脱硫正在

second = X mod 60
minute = (X div 60) mod 60
hour = (X div 60*60) mod 24
day = (X div 60*60*24) mod 31
month = (X div 60*60*24*31) mod 12
year = X div 60*60*24*31*12

举一个例子:

Suppose you want to encode the date december 20, 1998, 05:33:12 So you would have

second: 12
minute: 33
hour: 5
day: 19   -> note that we encode days in the range 0..31
month: 11  -> note that we conde months in the range 0..11
year: 1998

因此,我们赞扬:

X = 12 + 33*60 + 5*60*60 + 
        19*60*60*24 + 11*60*60*24*31 + 
        1998*60*60*24*31*12

即X = 12 + 1980 + 18000 + 1641600 + 29462400 + 64217318400 = 64248442392

现在,我们就把它编码。

second = 64248442392 mod 60  = 12
minute = (64248442392 div 60) mod 60 = 33
hour = (64248442392 div 60*60) mod 24 = 5
day = (64248442392 div 60*60*24) mod 31 = 19
month = (64248442392 div 60*60*24*31) mod 12 = 11
year = 64248442392 div 60*60*24*31*12 = 1998
问题回答

认为数字系统高于正数。 例如,hexa数字系统是16基数。 让我们在新的数字制度中说,我们有0-9位数、低位字母(a-z)和高位字母(A-Z)。 这使我们有62位可能的数位数,因此我们可以使用基数-62数字系统。 这里,有些Javascript代码用于转换——

function createConverter() {
    var charRange = (start, end) => Array.from(Array(end-start), 
                                         (v, i) => String.fromCharCode(i + start))

    var digits = charRange(48, 48+10)             // 0 to 9
                   .concat(charRange(97, 97+26))  // a to b
                   .concat(charRange(65, 65+26))  // A to B

    var base = digits.length

    return function(decimal) {
            var result = ""
            while (decimal >= base) {
                result = digits[decimal % base] + result
                decimal = parseInt(decimal / base)
            }
            result = digits[decimal] + result
            return result
        }
}

var convert = createConverter()
convert(parseInt(Date.now() / 1000)) // returns a 6-digit alphanumeric string 

<代码>charRange只是一种实用的arrow功能,从ASCII代码中产生一系列的特性。

<代码>数字代表我们计数系统的所有有效数字。 您可以在本清单中增加更有效的数字(如特别的斜体)。

<代码>为现有数位数(正数)

这个基数-62数字系统编码的输出数将超过1 700年,保持6位数。

EDIT: 实际日期.now()





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...