recursion - Permutation of jagged array -
i'm trying create permutation of multidimensional array in classic asp (vbscript) , i'm stuck. i've tried several functions of own , tried copying several php versions over, end either goes buffer overflow / infinite recursion or results more combination permutation, if understand differences correctly.
lets it's shirt. shirt can have colors, sizes, , styles. (the actual system allows number of "groups" of options (think color, size, etc) , number of options within each group (each particular size, each particular color,etc).
for example:
small med lg xl red blue green white pocket no-pocket
note number of elements in either dimension of array unknown beforehand; also, not second dimensions have same number of elements.
i need iterate through each possible unique option contains option each row. in particular example, there 32 options (because need ignore results have empty value given option, since asp doesn't handle jagged array way expect. so: small red pocket small red no-pocket small blue pocket small blue no-pocket etc.
once have part done, i'll need integrate ids database, i'm sure can part on own. it's recursive function that's killing me.
anyone able point me in starting place or me out? appreciated!
to avoid problems of terminology: wrote small program:
dim aaitems : aaitems = array( _ array( "small", "med", "lg", "xl" ) _ , array( "red", "blue", "green", "white" ) _ , array( "pocket", "no-pocket" ) _ ) dim oododemo : set oododemo = new cododemo.init( aaitems ) oododemo.run 33
and that's output:
0: small red pocket 1: small red no-pocket 2: small blue pocket 3: small blue no-pocket 4: small green pocket 5: small green no-pocket 6: small white pocket 7: small white no-pocket 8: med red pocket 9: med red no-pocket 10: med blue pocket 11: med blue no-pocket 12: med green pocket 13: med green no-pocket 14: med white pocket 15: med white no-pocket 16: lg red pocket 17: lg red no-pocket 18: lg blue pocket 19: lg blue no-pocket 20: lg green pocket 21: lg green no-pocket 22: lg white pocket 23: lg white no-pocket 24: xl red pocket 25: xl red no-pocket 26: xl blue pocket 27: xl blue no-pocket 28: xl green pocket 29: xl green no-pocket 30: xl white pocket 31: xl white no-pocket 32: small red pocket
if looks seed solution of problem, , post code cododemo class.
code cododemo:
'' cododemo - q&d combinations generator (odometer approach) ' ' based on ideas from: ' !! http://www.quickperm.org/index.php ' !! http://www.ghettocode.net/perl/buzzword_generator ' !! http://www.dreamincode.net/forums/topic/107837-vb6-combinatorics-lottery-problem/ ' !! http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n class cododemo private m_nplaces ' # of places/slots/digits/indices private m_nplacesub ' ubound (for vbscript only) private m_alasts ' last index each place => carry on private m_adigits ' digits/indices spin around private m_aaitems ' init: aoa containing elements spin private m_awords ' 1 result: array of combined private m_npos ' current increment position '' init( aaitems ) - use aoa of 'words' in positions init '' odometer public function init( aaitems ) set init = me m_aaitems = aaitems m_nplacesub = ubound( m_aaitems ) m_nplaces = m_nplacesub + 1 redim m_alasts( m_nplacesub ) redim m_adigits( m_nplacesub ) redim m_awords( m_nplacesub ) dim nrow nrow = 0 m_nplacesub dim ncol ncol = 0 ubound( m_aaitems( nrow ) ) m_aaitems( nrow )( ncol ) = m_aaitems( nrow )( ncol ) next m_alasts( nrow ) = ncol - 1 next reset end function ' init '' reset() - start afresh: indices/digit set 0 (=> first word), next '' increment @ utmost right public sub reset() m_npos = 0 m_nplacesub m_adigits( m_npos ) = 0 next m_npos = m_nplacesub end sub ' reset '' tick() - increment current position , deal carry public sub tick() m_adigits( m_npos ) = m_adigits( m_npos ) + 1 if m_adigits( m_npos ) > m_alasts( m_npos ) ' carry left m_npos = m_npos - 1 0 step -1 m_adigits( m_npos ) = m_adigits( m_npos ) + 1 if m_adigits( m_npos ) <= m_alasts( m_npos ) ' carry done exit end if next m_npos = m_npos + 1 m_nplacesub ' 0 right m_adigits( m_npos ) = 0 next m_npos = m_nplacesub ' next increment @ utmost right end if end sub ' tick '' map() - build result array getting 'words' '' indices in current 'digits' private sub map() dim nidx nidx = 0 m_nplacesub m_awords( nidx ) = m_aaitems( nidx )( m_adigits( nidx ) ) next end sub ' map '' run( nmax ) - reset odometer, tick/increment nmax times , '' display mapped/translated result public sub run( nmax ) reset dim opad : set opad = new cpad.initww( len( cstr( nmax ) ) + 1, "l" ) dim ncnt ncnt = 0 nmax - 1 map wscript.echo opad.pad( ncnt ) & ":", join( m_awords ) tick next end sub ' run end class ' cododemo
some hints/remarks: think of odometer genererates combinations 6 (7?) places/digits in numerical order. imagine odometer lets specify sequence/ordered set of 'digits'/words/items each place/slot. specification done aaitems.
this code cpad, used in .run():
''= cpad - q&d padding class cpad private m_nw private m_sw private m_ss private m_nw1 public function initww( nw, sw ) m_nw = nw m_nw1 = m_nw + 1 m_sw = ucase( sw ) m_ss = space( nw ) set initww = me end function public function initwwc( nw, sw, sc ) set initwwc = initww( nw, sw ) m_ss = string( nw, sc ) end function public function pad( vx ) dim sx : sx = cstr( vx ) dim nl : nl = len( sx ) if nl > m_nw err.raise 4711, "cpad::pad()", "too long: " & nl & " > " & m_nw end if select case m_sw case "l" pad = right( m_ss & sx, m_nw ) case "r" pad = left( sx & m_ss, m_nw ) case "c" pad = mid( m_ss & sx & m_ss, m_nw1 - ((m_nw1 - nl) \ 2), m_nw ) case else err.raise 4711, "cpad::pad() unknown m_sw: '" & m_sw & "'" end select end function end class ' cpad
sorry missing documentation. i'll try answer question.
Comments
Post a Comment