Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

php - Encode/Decode ID Reversal issue

  • SCENARIO:

A) You have a charset of 100 in which the first characters are A, B, C and the last characters are -, _.

B) The encode function returns a string of length 10.

C) The encode converts a number into the correlating number in the charset

Example: A == 0 || B == 1 || C == 2 || - == 98 || _ == 99

Amount of possibilities: 100 ^ 10 = 1e+20 || 100,000 Quadrillion || 100,000,000,000 Billion.

  • PROBLEM: How would you figure out whether 999 is iii, _i or i_?
  • Note: The solution to the problem sketched above should work for every possible situation
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

looks like homework...

lets have a look at our problem:

999 can not be represented as a single char in our charset we can encode it in 3 different ways

9 9 9 => I I I

99 9 => _I

9 99 => I_

now... a charset alone does not make an encoding ... at this point you should probably read up about what a "code" is ... http://en.wikipedia.org/wiki/Code

please notice that this has absolutely nothing to do with encryption ...

so ... we need a ruleset for encoding/decoding our code

since we are supposed to make that ruleset, it is our free choice how we handle things, as long as we keep in mind what other key rules we have to follow...

the code shall be 10 characters long ... at max from what i see, or else III wouldn't possibly be a valid example of our code ... AAAAAAAAIII would be ... so lets assume that we may drop leading zeros, or As in this case, and further assume that III and AAAAAAAIII are identical

now we have the given fact that our code has 100^10 possible codewords, which can only be achived if every combination of our charset with a length of 10 is a valid codeword

so all three ... III and I_ and _I ... have to be valid codewords ...

does that mean that all three have the value of 999?

short: no

long:

as mentioned earlier, there is a ruleset needed to give the code a meaning...

since there is no encoding ruleset given, we seem to be free to create one...

lets have a look at the ruleset to encode our regular base 10 numbers ...

we have a charset from 0 to 9 -> 10 digits

the position of a digit in a number contains information...

123 for example can be written as 1*10^10 + 2*10^1 + 3*10^0

if we transfer this to our new encoding ... let's call it base 100 ... it would look like this:

123 -> 1*100^1 + 23*100^0

=> 1=B ... 23=X => 123 -> BX

999 -> 9*100^1 + 99*100^0 -> I_

but who says we have to declare the left most digit in our code to be the most siginificant digit?

what if we would interpret it otherwise?

isn't 99*100^0 + 9*100^1 = 999 too?

yes ... therefore we could write it as _I too ...

which one is the correct one now? ... that ONLY depends on the ruleset of our code ... if it says the leftmost digit ist the most significant one, the answer is I_ ... if the rightmost digit ist the most significant one, the answer is _I

as long as the ruleset for the encoding is not specified, the answer to this question cannot be solved ... you can only try to make an educated guess, and use the same convention as in our "normal" base 10 encoding ... leftmost digit = most significant digit -> I_

but please keep in mind ... this is a guess ... if i'd get such a question in a test, i'd explain why there is no answer unless the encoding rules have been specified.

tldr:

with the provided information, it's a free choice if it is i_ or _i


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...