A
A
Alexander Knyazev2017-03-15 14:01:23
JavaScript
Alexander Knyazev, 2017-03-15 14:01:23

How to sort through all possible combinations of three characters in order?

The bottom line is:
There is a hash code of three characters, the initial value is 000, for example.
You need to write a function that, knowing only the previous hash, will create the next one, something like: the
previous one is 000 - then the next 001,
the next 002 and so on until 00z,
then 011, 012, 013 and so on up to 0zz
How can such a function be written, maybe there are already similar implementations?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
Lynn "Coffee Man", 2017-03-15
@alexandrknyazev13071995

> incHash = (h) => ('00' + (parseInt(h, 36) + 1).toString(36)).slice(-3)
[Function]
> incHash('000')
'001'
> incHash('00z')
'010'
> incHash('12z')
'130'
> incHash('1sz')
'1t0'

A
Alexander Pushkarev, 2017-03-15
@AXP-dev

function pad(num, size) {
    var s = (num+1)+"";
    while (s.length < size) s = "0" + s;
    return s;
}

pad(004, 3);
pad(005, 3)

S
Sergey, 2017-03-15
@begemot_sun

erlang:

Code = [ 1,2,3 ].
[{A,B,C} || A <- Code, B <- Code, C <- Code ].

PS Well, in general, of course, I did not read it to the end.
But such a function can also be created. It is enough to work in the ternary system :)
PSS
12> F = fun(A) -> integer_to_binary(binary_to_integer(A, 3) + 1, 3) end.
#Fun
13> F(<<"000">>).
<<"1">>
14> F(<<"001">>).
<<"2">>
15> F(<<"002">>).
<<"10">>
16> F(<<"010">>).
<<"11">>
17> F(<<"210">>).
<<"211">>

PSSS .... S: I finally read it, but I think the meaning is clear :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question