Best search algorithm to find condition within a range
davea at davea.name
Tue Apr 7 16:26:48 CEST 2015
On 04/07/2015 10:10 AM, jonas.thornvall at gmail.com wrote:
> Den tisdag 7 april 2015 kl. 15:30:36 UTC+2 skrev Dave Angel:
>> If that code were in Python, I could be more motivated to critique it.
>> The whole algorithm could be much simpler. But perhaps there is some
>> How would you do it if you were converting the base by hand? I
>> certainly wouldn't be doing any trial and error. For each pass, I'd
>> calculate quotient and remainder, where remainder is the digit, and
>> quotient is the next value you work on.
> I am doing it just like i would do it by hand finding the biggest digit first. To do that i need to know nearest base^exp that is less than the actual number. Add up the digit (multiply) it to the nearest smaller multiple. Subtract that number (base^exp*multiple).
> Divide / Scale down the exponent with base. And record the digit.
> And start looking for next digit doing same manipulation until remainder = 0.
> And that is what i am doing.
Then I don't know why you do the call to reverse() in the top-level code.
If I were doing it, I'd have no trial and error in the code at all.
Generate the digits right to left, then reverse them before returning.
For example, if you want to convert 378 to base 10 (it's binary
internally), you'd divide by 10 to get 37, remainder 8. Save the 8, and
loop again. Divide 37 by 10 and get 3, remainder 7. Save the 7. Divide
again by 10 and get 0, remainder 3. Save the 3
Now you have '8', '7', '3' So you reverse the list, and get
'3', '7', '8'
More information about the Python-list