Sign in to follow this  
Followers 0
EvenSteven

2d array and ArraySwap

7 posts in this topic

global $array[4][4] = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]

I have a 2d array and using ArraySwap() I wanted to swap through every distinct permutation. Every iteration there's one less possible flip so summation of 0 to 15 = 120 different possibilities. It seems easier to me for a 1d array. So I was thinking maybe it could be possible to call $array[1,0] = 5 as $array[4] for example? Aside from that all I can think of would be convoluted multi variable counting that makes my head hurt just thinking about it.

Share this post


Link to post
Share on other sites



First of all, $array[1,0] is bad syntax. Should be $array[1][0].

What does "swap through" mean?

Given your initialized 2D array example, how would it look after it had been "swapped through"?

:unsure:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I wrote a function which does what I want but with a 1d array. Should help to illustrate what I mean. All it does is swap 2 values to get all the different combinations. I think I would break my brain trying to do this with a 2d array. Well it would give several sessions of brain stimulation lol.

#include <array.au3>
global $1d[16] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

_analyze1d()

func _analyze1d()
        Local $i = 0
        Local $a = 1
        Local $b = 15
        Local $c = 1
    
        While $b > 0
                _ArraySwap($1d[$i], $1d[$i+ $a])
                _arraydisplay($1d)
                _ArraySwap($1d[$i+ $a], $1d[$i])
                $a += 1
                if $a > $b      Then
                        $i += 1
                        $c += 1
                        $b -= 1
                        $a = 1
                EndIf
        WEnd
EndFunc

2d is messing me up because it goes from [0][3] to [1][0] then [1][3] to [2][0]. Though I guess at this point I could just add some if loops and such and manage. It would have been easier if I could use [4] instead of [1][0] for example but whatever. Just writing this down I see a few solutions. but they are going to be pretty convoluted lol.

Edited by EvenSteven

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

As far as I know the permutation of 16 is 16! = 20922789888000 different possible rearrangements of the elements!

Example:

Permutation of ABC

ABC

ACB

BAC

BCA

CAB

CBA

-> 3! = 6

Are you sure that you want to permute 16 elements?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

@EvenSteven,

That you arrange values in a 1D, 2D or N-dimentional array is just cosmetic (for your eyes or convenience only). The issue is that permutations get out of reach very fast when the number of elements increases (whatever their arrangement in line, rectangle, square, cube, hypercube or worse), just as UEZ showed you.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I shouldn't have used the word permutation. My bad, that's not what I meant. I switched over to a 1d array anyways and calculated rows and columns elsewhere. Way less complicated than operating on a 2d array. Thanks guys. I wanted to analyze one move at a time and not the complete set actually. So for ABC there would be 3 possibilities. [A to B][A to C][b to C]. But it's all good now. Cheers

Edited by EvenSteven

Share this post


Link to post
Share on other sites

Sounds like what you're dealing with is k-combinations of a n-set, which is described here for instance.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
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
Sign in to follow this  
Followers 0