PDA

View Full Version : Challenge - Array Class


mpol777
05-20-2003, 12:15 AM
Description
Any custom method added to the Array Class.

Rules
Standard Rules (http://www.actionscript.org/forums/showthread.php3?s=&threadid=28070)

Format

/*
Method: Method Name

Description:
A short description of what your method does.

Arguments:
argument - (type) description of the argument

Return:
What the method returns
*/

Array.prototype.MyMethod = function() {

}

// EXAMPLE


If you don't have a .swf or .fla, paste an example of it being used. The example doesn't count towards the line limit.

The person with the best method gets a cookie. *








*To recieve prize winner must be between the ages of 93 to 106 years old. Please allow 12 years for delivery.

Billy T
05-20-2003, 12:52 AM
OK I don't know how useful this is or if its been done before (or even if you can do it with a built in method) but here goes

/*
Method: remove

Description:
Extends the splice method a bit

Arguments:
'item' can be either a number (which will be the index of the element you want to remove)
or a string (which will be the value of the element you want to remove)

Return:
The array with 'item' removed.
*/
Array.prototype.remove = function(item) {
if (typeof item == "number") {
this.splice(item,1);
} else {
for(i=0;i<this.length;i++){
if(this[i]==item){
this.splice(i,1);
}
}
}
return this;
};
// EXAMPLE
theArray = new Array("Bob", "Fred", "Jeff", "Monica");
trace(theArray);
theArray.remove("Fred");
trace(theArray);
theArray.remove(1);
trace(theArray);

just extends the splice method a bit. Seems to me that removing 1 item from an array is pretty common and that's what this prototype does

cheers

farafiro
05-20-2003, 07:18 AM
ok, here is mine
basicly this to return a random value of an array so u can use it to pick the Array elements randomly
posArray = new Array("0", "275", "550", "825", "1100", "1375");
function random_num(which_array) {
n = random(posArray.length);
return which_array[n];
}
function go() {
newPos = random_num(posArray);
trace(newPos);
}
setInterval(go, 3000);Example (http://www.geocities.com/a_fayek/flash/christmas.swf), sorry, this was meant to be off line, so u just wait for it about 10-20 sec//Example's script
stop();
posArray = new Array("0", "275", "550", "825", "1100", "1375");
function random_num(which_array) {
n = random(posArray.length);
return which_array[n];
}
function go() {
newPos = random_num(posArray);
_root.nextFrame();
if (_currentframe>5) {
gotoAndStop(2);
}
trace(newPos);
}
_root.onEnterFrame = function() {
currPos = nav._x;
if (newPos<currPos) {
friction = currPos-newPos;
nav._x = currPos-(friction*.2);
nav2._x = currPos-(friction*.7);
} else if (newPos>currPos) {
friction = newPos-currPos;
nav._x = currPos+(friction*.2);
nav2._x = currPos+(friction*.7);
}
};
setInterval(go, 3000);

buggedcom
05-20-2003, 12:39 PM
findPos

done a million times b4 but here you go


/*
METHOD : find

DESCRIPTION : finds the positions in an array where the idex is the same as the value

ARGUMENTS : value = a value

RETURNS : an array containing the index nums of the found values
*/
Array.prototype.findPos = function(value) {
var tAr = [];
for (var i = 0; i<this.length; i++) {
if (this[i] == value) {
tAr[tAr.length] = i;
}
}
return (tAr.length>0) ? tAr : false;
};
// EXAMPLE
listRA = ["first", "second", "third", "first"];
var ar = listRA.findPos("first");
if (!ar) {
trace("not found");
} else {
trace("value 'first' found in positions : "+ar);
}

Billy T
05-20-2003, 02:35 PM
I think we need one from the challenge starter...

farafiro
05-20-2003, 02:53 PM
Originally posted by Billy T
I think we need one from the challenge starter... heheheheeeeeeee
yep, I think we do

mpol777
05-20-2003, 05:50 PM
OK here ya go.

I'm not exactly happy with it. It seems like there would be a good way without duplicating the array.


/*
Method: insertAt

Description:
Inserts data into a particualr position into an array

Arguments:
data - can be pretty much anything. string, number, boolean, object, etc.
index - (number) the index to insert the data at

Return:
What the method returns
*/

Array.prototype.insertAt = function(data, index) {
oldArray = new Array();
oldArray = oldArray.concat(this);
count = 0;
for (i=0; i<oldArray.length; i++) {
if (i == index) {
this[count] = data;
count++;
}
this[count] = oldArray[i];
count++;
}
return this;
}

// EXAMPLE

array1 = new Array("remington","winchester","glock","colt");
trace(array1);
// returns "remington,winchester,glock,colt"

array1.insertAt("browning",2);
trace(array1);
// returns "remington,winchester,browning,glock,colt"

littleRichard
05-20-2003, 06:35 PM
hate to say this, but you can do that much easier with the built in splice() method.

array1 = new Array("remington","winchester","glock","colt");
trace(array1);

array1.splice(2, 0, "browning");
trace(array1);

mpol777
05-20-2003, 07:37 PM
Originally posted by littleRichard
hate to say this,

Don't hate the playa, hate the game. :D

That is much better. thanks.

Esquared
05-20-2003, 08:06 PM
/*
Method: insertInOrder

Description:
inserts an object into a SORTED array in log(n) time
(fastest possible: for instance, need < 10 comparisons for array of length 1024)

Arguments:
object - (anything) the object to be inserted
order - (boolean) increasing if true, decreasing if false

Return:
the array containing the inserted object
*/

Array.prototype.insertInOrder = function(object, order)
{
L = 0;
R = this.length;
M;

while(L < R)
{
M = Math.floor(L+(R-L)/2);

if(object < this[M])
{
if(order) R = M;
else L = M+1;
}
else if(object > this[M])
{
if(order) L = M+1;
else R = M;
}
else
break;
}
this.splice(L, 0, object);

return this;
}

// EXAMPLE
trace("BEFORE INSERTION:");
//
increasing = new Array(1,2,3,4,5,6,8,9);
trace(" -increasing: " + increasing); // returns "1,2,3,4,5,6,8,9"
//
decreasing = new Array(9,8,6,5,4,3,2,1);
trace(" -decreasing: " + decreasing); // returns "9,8,6,5,4,3,2,1"
//
trace("AFTER INSERTION:");
//
increasing.insertInOrder(7, true);
trace(" -increasing: " + increasing); // returns "1,2,3,4,5,6,7,8,9"
//
decreasing.insertInOrder(7, false);
trace(" -decreasing: " + decreasing); // returns "9,8,7,6,5,4,3,2,1"

Billy T
05-21-2003, 01:03 AM
funky

Esquared
05-21-2003, 02:40 AM
...in a good way, I'd hope ;)

By the way, I like the "sticky."

Billy T
05-21-2003, 02:13 PM
funky is always good...except for smells but your proto smells pretty fresh (that's fresh in a good way of course)

bstudly
05-21-2003, 07:19 PM
/*
METHOD : num_occur

DESCRIPTION : finds the number of occurences of a value in the array

ARGUMENTS : val = the value you want to look for

RETURNS : an integer representing the number of times it was found
*/


Array.prototype.num_occur=function(val){
var occ, ind, array_length;
for(occ=ind=0, array_length=this.length; ind<array_length; ind++) if(this[ind]==val) occ++;
return occ;
};



//example
a=[4,5,3,4,4,66,7,4,4,44];
trace(a.num_occur(4)); // will trace 5

Jesse
05-23-2003, 12:49 PM
I'm not feeling particularly creative but here's a small entry... a findMin would be good too but wouldn't fit in the word limit:
/*
Method: Find Max

Description:
Find the maximum value in an array; in the case of numbers
this is (obviously) the highest number, while in an array of
strings it is the LONGEST string.

Arguments:
isString - (bool) Is this an array of strings?

Return:
An object with three attributes: value, theIndex, theMaxLength
value: the highest value / longest string
theIndex: the index of the highest value
theMaxLength: length of string (applicable only to string searches)
*/

Array.prototype.findMax = function(isString) {
var theMaxValue = null;
var highestIndex = null;
var theMaxLength = null;
for (val in this) {
if (isString) {
if (this[val].length > this.theMaxLength) {
this.theMaxValue = this[val]
this.theMaxLength = this[val].length
this.highestIndex = val
}
} else {
if (this[val] > this.theMaxValue) {
this.theMaxValue = this[val]
this.highestIndex = val
}
}
}
return ({value: this.theMaxValue, theIndex: this.highestIndex, theLength: this.theMaxLength})
}

// EXAMPLE
numbers = new Array(23,5,2,345,321,7742,124,52,1)
trace(numbers.findMax().value)
trace(numbers.findMax().theIndex)
strings = new Array("Macromedia","Flash","ActionScript")
trace(strings.findMax(true).value)
trace(strings.findMax(true).theIndex)
trace(strings.findMax(true).theLength)

pixelwit
05-23-2003, 02:21 PM
Shortened Jesse's code up a bit so maybe he can fit the findMin function in there. ;)Array.prototype.findMax = function(isString) {
var theMaxValue, highestIndex, theMaxLength;
for (val in this) {
if (isString) {
if (this[val].length>theMaxLength) {
theMaxValue = this[val];
theMaxLength = this[val].length;
highestIndex = val;
}
} else if (this[val]>theMaxValue) {
theMaxValue = this[val];
highestIndex = val;
}
}
return ({value:theMaxValue, theIndex:highestIndex, theLength:theMaxLength});
};-PiXELWiT
http://www.pixelwit.com

tg
05-23-2003, 05:05 PM
don't forget to look in the library here at as.org...

some of these may already posted there, and there is no use in repeating the code....

farafiro
05-25-2003, 06:34 AM
Originally posted by tg
some of these may already posted there, and there is no use in repeating the code.... why, maybe it's better
faster
opimised
...
..
.
maybe :)

senocular
06-16-2003, 12:39 AM
Just for Kicks Challenges eh?

Well I'll throw one in ;) Id do something crazy fun but 25 lines doesnt allow much for that. Ill do something more practical:
/************************************************** *****\
* Method:
* Array.convertTo2D(rows, cols [, retain]);
*
*
* Description:
* Creates a 2 dimensional array out of a 'normal'
* single dimensional array. This new array gets a
* new property .is2D (set to true) specifying that
* the array is 2 dimensional and has a new .toString
* method written for it to show the array in 2D.
*
* Arguments:
* rows - (number) number of rows for the new 2D
* array.
* cols - (number) number of columns for the new
* 2D array
* retain - (boolean, optional) dictates whether
* or not the current array is converted to a 2D
* array (true) or a new 2D array is created from
* the current array (false). Default: true.
*
* Returns:
* if the array is retained, the array is returned. If
* not retained, the new array is returned. If the
* call lacks the correct number of arguments or the
* array was already converted to a 2D array by this
* method.
\************************************************* *****/

Array.prototype.convertTo2D = function(rows, cols, retain){
if (this.is2D || arguments.length<2) return false;
var a = this.slice(), me = (retain === false) ? [] : this;
for (me.length=0; me.length<rows;) me[me.length] = (a.length) ? a.splice(0,cols) : [];
me.toString = function(){
for (var r,out="",c=0; c<this.length; ++c){
for (r=0; r<this[c].length; ++r) out += (r) ? ","+this[c][r] : "["+this[c][r];
out += "]\n";
}
return out.slice(0,-1);
}
me.is2D = true;
return me;
};

// EXAMPLE:
a = [1,2,3,4,5,6];
trace(a);
trace("=========");
trace(a.convertTo2D(2,3, false));
trace("=========");
trace(a.convertTo2D(3,2, false));
trace("=========");
trace(a.convertTo2D(1,6, false));
trace("=========");
a.convertTo2D(6,1);
trace(a);
trace("=========");
trace(a.convertTo2D(2,3));

/* OUTPUT:
1,2,3,4,5,6
=========
[1,2,3]
[4,5,6]
=========
[1,2]
[3,4]
[5,6]
=========
[1,2,3,4,5,6]
=========
[1]
[2]
[3]
[4]
[5]
[6]
=========
false
********/

farafiro
06-16-2003, 06:18 AM
wow seno
this is something ..... cool

like Penner's Vector's to 2d and 3d
but very good indeed

magicwand
06-16-2003, 05:13 PM
i wish i can understand what that is :confused: :confused: :confused:

farafiro
06-17-2003, 06:44 AM
hey magic
did u hear about 2 dimentional Arrays??

magicwand
06-17-2003, 01:59 PM
hear??? yes.
did i ever use them??? No.
do i understand them codes?? no.
perhaps later i will look into it.
thanks.