2

I want to write a function which removes elements from an array of integers starting from the lowest values without changing the positions of the elements. Programming language is ActionScript3.

e.g. (these are individual trace statements)

    var aNumArr:Array = [0,7,2,5,9,0]
    trace(RemoveMinValues(aNumArr, 1, false)) //output: 7,2,5,9,0
    //trace(RemoveMinValues(aNumArr, -1, true)) //output: 0,7,2,5,9
    //trace(RemoveMinValues(aNumArr, 2)) //output: 7,2,5,9

I have managed to remove the lowest values in an array using sort(Array.NUMERIC) and sort(Array.DESCENDING).

But I can't seem to figure out how to move the elements back to their original positions.

As this is an assignment, I cannot copy the entire function code. And I wish you not to tell me the exact answer, but rather give me insight on how to go about doing it.

Hope I was clear enough. Please let me know if you need additional info.

Thanks in advance.


EDIT: I realised I missed out a few things on the function. I've also changed the aNumArr and desired output values to make it clearer what I want.

And here was what I did previously:

    function RemoveMinValues(aNumArr:Array, iMinsToRemove:int):void
    {
         if(iMinsToRemove >= 0)
         {
              aNumArr.sort(Array.NUMERIC);
              for(var i:int = 0; i < iMinsToRemove; ++i)
              {
                   aNumArr.shift();
              }
         }
         else
         {
              aNumArr.sort(Array.DESCENDING);
              for(var i:int = 0; i > iMinsToRemove; --i)
              {
                   aNumArr.pop();
              }
         }
    }

Basically aNumArr:Arrayis the array of integers specified. AndiNumbersOfMinsToRemove:int is the number of min values to remove. The Assignment requires me to return nothing.

I know Array.NUMERIC and Array.DESCENDING would change the position of the elements, but I can't seem to figure out the logic on how to keep their positions. Please try to keep as simple a possible. I'm still an ameture.

Davina Leong
  • 737
  • 2
  • 11
  • 28

3 Answers3

1

Instead of removing the value, set it to null. Better still, set it to Math.NEGATIVE_INFINITY so that the sorting order remains unchanged.

That way, the array indices will remain the same because you are modifying the value, not removing it

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
0

First, note that Math.min() and Math.max() can take any number of arguments. Also, it's important to understand the apply() method available to Function objects. It allows you to pass arguments to the function using an Array. Let's take advantage of both:

var aNumArr:Array = [0,7,2,5,9];
var maxValue:Number = Math.max.apply(null, aNumArr);
var minValue:Number = Math.min.apply(null, aNumArr);

Here's the best part: the "loop" is actually run using native code (inside Flash Player), so it's faster than searching for the minimum or maximum value using a pure ActionScript loop.

Swati Singh
  • 1,863
  • 3
  • 19
  • 40
0

[[Edit]]

Added the code to a spoiler block for others.


If I understand your question correctly, you wish to investigate Array.filter. If the question you are asking requires the final result of each RemoveMinValues pass to return an array that maintains the relative, not absolute, position, then the filter method would work best.

Here is the difference given the following array, assuming the second argument to RemoveMinValues is the min value, defaulted to 0.

var arr:Array = [-1,3,2,1,0];

Absolute position:

RemoveMinValues(arr)    // [null,3,2,1,null]
RemoveMinValues(arr, 1) // [null,3,2,null,null]

Relative position:

RemoveMinValues(arr)    // [3,2,1]
RemoveMinValues(arr, 1) // [3,2]

The following method implementation for RemoveMinValues would provide the "relative" results. Please spend some time reading the Array.filter documentation. Each call to RemoveMinValues, with this specific implementation, returns a new array without modifying the original, as defined in the documentation.

Code:

target.filter(function(item:*, ... args):Boolean { return item > minValue });

This method will be slower on larger data sets. Switching to a Vector object would address performance issues.

Best of luck!

stat
  • 411
  • 2
  • 6