# Mixed Base Conversion (coding challenge)

## Recommended Posts

at this link https://codegolf.stackexchange.com/questions/37905/mixed-base-conversion there is an interesting "challenge",
how would you solve it with AutoIt?

Edited by Chimp Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

.... just an initial skeleton with no converter yet ...

```Example()

Func Example()

Local \$aDigit_D[] = [52, 0, 0, 0, 0] ;     D - a number passed as a list of digits

Local \$aBases_I[] = [100, 7, 24, 60, 60] ; I - an input base(s) array, an element for each input digit,
;                                              if there are fewer elements than digits just wrap around

Local \$aBases_O[] =  ;                 O - an output base(s) array, an element for each output digit,
;                                              if there are fewer elements than digits just wrap around

Local \$aResult = _MixedRadix(\$aDigit_D, \$aBases_I, \$aBases_O) ; an array of output digits
EndFunc   ;==>Example

Local \$Ib = UBound(\$aInputBases)
Local \$Ob = UBound(\$aOutputBases)

For \$dgit = UBound(\$aInputDigits) - 1 To 0 Step -1
\$IndexIb = \$Ib - Mod(\$dgit, \$Ib) -1 ; this pointer points to the next input base element (it auto wraps when needed)
\$IndexOb = \$Ob - Mod(\$dgit, \$Ob) -1 ; this pointer points to the next output base element (it auto wraps when needed)

; to be continued .....

Next Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

Here my attempt (works for all examples) :

```#include <Array.au3>

;Const \$DIG = [1,0,0], \$IN = , \$OUT = 
;Const \$DIG = [1,0,0], \$IN = , \$OUT = 
;Const \$DIG = [1,9,0,3,1,5], \$IN = [2,10], \$OUT = 
;Const \$DIG = [1,9,0,3,1,5], \$IN = [2,10], \$OUT = [4,3,2]
;Const \$DIG = [52,0,0,0,0], \$IN = [100,7,24,60,60], \$OUT = 
Const \$DIG = [0,2,10], \$IN = [2,4,8,16], \$OUT = 
;Const \$DIG = [], \$IN = [123,456], \$OUT = 
;Const \$DIG = [0, 0], \$IN = [123,456], \$OUT = 

Local \$Result
Local \$res = 0, \$tmp

For \$i = 1 To UBound(\$DIG)
\$tmp = \$DIG[UBound(\$DIG) - \$i]
For \$j = 1 to \$i - 1
\$tmp *= \$IN[UBound(\$IN)-Mod(\$j-1, UBound(\$IN)) - 1]
Next
\$res += \$tmp
Next

ConsoleWrite(\$res & @CRLF)

\$i = 0
While \$res > 0
ReDim \$Result[\$i+1]
\$tmp = \$OUT[UBound(\$OUT)-Mod(\$i, UBound(\$OUT)) - 1]
\$Result[\$i] = Mod(\$res, \$tmp)
\$res = Floor(\$res/\$tmp)
\$i += 1
WEnd
_ArrayReverse(\$Result)
_ArrayDisplay(\$Result)```

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites

Nice, thanks @Nine

... here's another possible interesting use case suitable for this function:
https://codegolf.stackexchange.com/questions/79609/index-of-a-multidimensional-array
that's way I was interested in that function, ... see you later ... Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

Optimized code :

```#include <Array.au3>

Const \$DIG = [1,0,0], \$IN = , \$OUT = 
;Const \$DIG = [1,0,0], \$IN = , \$OUT = 
;Const \$DIG = [1,9,0,3,1,5], \$IN = [2,10], \$OUT = 
;Const \$DIG = [1,9,0,3,1,5], \$IN = [2,10], \$OUT = [4,3,2]
;Const \$DIG = [52,0,0,0,0], \$IN = [100,7,24,60,60], \$OUT = 
;Const \$DIG = [0,2,10], \$IN = [2,4,8,16], \$OUT = 
;Const \$DIG = [], \$IN = [123,456], \$OUT = 
;Const \$DIG = [0, 0], \$IN = [123,456], \$OUT = 

Local \$Result, \$res = 0, \$tmp = 1

For \$i = 1 To UBound(\$DIG)
\$res += \$DIG[UBound(\$DIG) - \$i] * \$tmp
\$tmp *= \$IN[UBound(\$IN)-Mod(\$i-1, UBound(\$IN)) - 1]
Next

ConsoleWrite(\$res & @CRLF)

\$i = 0
While \$res > 0
ReDim \$Result[\$i+1]
\$tmp = \$OUT[UBound(\$OUT)-Mod(\$i, UBound(\$OUT)) - 1]
\$Result[\$i] = Mod(\$res, \$tmp)
\$res = Floor(\$res/\$tmp)
\$i += 1
WEnd
_ArrayReverse(\$Result)
_ArrayDisplay(\$Result)```

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

## Create an account

Register a new account

• ### Similar Content

• #### Challenge: Solving Josephus problem 1 2

By guinness,

• 31 replies
• 2,789 views
• #### Trim text in a file without storing the contents of anywhere 1 2

By TheDcoder,

• 22 replies
• 3,506 views
• #### Hey Everyone (please VOTE in Challenge)

By TheSaint,

• 3 replies
• 1,988 views
• #### Lottery - Challenge 1 2 3 4

By guinness,

• 74 replies
• 10,906 views
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...