Jump to content

Mixed Base Conversion (coding challenge)


Recommended Posts

.... 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[] = [10] ;                 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

Func _MixedRadix($aInputDigits, $aInputBases, $aOutputBases)

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

    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

    Return $aDigits_Out

EndFunc   ;==>_MixedRadix

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

Here my attempt (works for all examples) :

#include <Array.au3>

;Const $DIG = [1,0,0], $IN = [10], $OUT = [2]
;Const $DIG = [1,0,0], $IN = [2], $OUT = [10]
;Const $DIG = [1,9,0,3,1,5], $IN = [2,10], $OUT = [10]
;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 = [10]
Const $DIG = [0,2,10], $IN = [2,4,8,16], $OUT = [42]
;Const $DIG = [], $IN = [123,456], $OUT = [13]
;Const $DIG = [0, 0], $IN = [123,456], $OUT = [13]

Local $Result[0]
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)

 

Link to comment
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 ...

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

Optimized code :

#include <Array.au3>

Const $DIG = [1,0,0], $IN = [10], $OUT = [2]
;Const $DIG = [1,0,0], $IN = [2], $OUT = [10]
;Const $DIG = [1,9,0,3,1,5], $IN = [2,10], $OUT = [10]
;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 = [10]
;Const $DIG = [0,2,10], $IN = [2,4,8,16], $OUT = [42]
;Const $DIG = [], $IN = [123,456], $OUT = [13]
;Const $DIG = [0, 0], $IN = [123,456], $OUT = [13]

Local $Result[0], $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)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...