Filter: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 156: | Line 156: | ||
val ary = [1,2,3,4,5,6]; |
val ary = [1,2,3,4,5,6]; |
||
List.filter (fn x => x mod 2 = 0) ary |
List.filter (fn x => x mod 2 = 0) ary |
||
==[[TCL]]== |
|||
[[Category:TCL]] |
|||
TCL doesn't really have a concept of a "number" per se - strictly speaking its only data type is the string (but a string can be interpreted as a number, of course). The generic way of getting certain elements from an array looks roughly like this: |
|||
foreach key [array names arr] {if { <condition> } then {puts $tst($key)}} |
Revision as of 04:45, 31 January 2007
Filter
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Select certain elements from an Array into a new Array in a generic way. E.g., select all even numbers from an Array
AppleScript
set array to {1, 2, 3, 4, 5} set evens to {} repeat with i in array -- very important -- list index starts at 1 not 0 if (i mod 2 = 0) then set evens to evens & i end repeat
C
int arr[5] = {1,2,3,4,5}; int *result; int memoryReqd = 0,length = sizeof(arr)/sizeof(arr[0]), i,j; for (i=0; i<length; i++) { if(0 == arr[i]%2) { memoryReqd++; } } result = (int*)malloc(memoryReqd*sizeof(int)); for(i=0,j=0; i<length; i++) { if(0 == arr[i]%2) { result[j++]=arr[i]; } }
Test our resultant array:
for(i=0; i<memoryReqd; i++) { printf("%d\n",result[i]); }
C++
Using STL and Boost.Lambda with Microsoft Visual C++ 2005
using namespace std; using namespace boost::lambda; vector<int> ary(10); int i = 0; for_each(ary.begin(), ary.end(), _1 = ++var(i)); // init array vector<int> evens; remove_copy_if(ary.begin(), ary.end(), back_inserter(evens), _1 % 2); // filter copy
C#
// .NET 1.x solution ArrayList array = new ArrayList( new int[] { 1, 2, 3, 4, 5 } ); ArrayList evens = new ArrayList(); foreach( int i in array ) { if( (i%2) == 0 ) evens.Add( i ); } foreach( int i in evens ) System.Console.WriteLine( i.ToString() );
// .NET 2.0 solution List<int> array = new List<int>( new int[] { 1, 2, 3, 4, 5 } ); List<int> evens = array.FindAll( delegate( int i ) { return (i%2)==0; } ); foreach( int i in evens ) System.Console.WriteLine( i.ToString() );
Alternate
int[] iArray = { 1, 2, 3, 4, 5 }; int iArray_Length = iArray.Length; for( int i = 0; i < iArray_Length; i++ ){ System.Console.WriteLine( iArray[i].ToString() ); }
Haskell
ary = [1..10] evens = [ x | x <- ary, even x ]
IDL
The where() function can select elements on any logical expression. For example
result = array[where(NOT array AND 1)]
Java
int[] array = new int[] {1, 2, 3, 4, 5 }; List<Integer> evensList = new ArrayList<Integer>(); for (int i: array) { if (i % 2 == 0) evensList.add(i); } int[] evens = evensList.toArray(new int[0]);
JavaScript
var arr = [1,2,3,4,5]; var evens = []; for (var i=0, ilen=arr.length; i<ilen; i++) if (arr[i] % 2 == 0) evens.push(arr[i]);
Perl
my @list = (1, 2, 3, 4, 5, 6); my @even = grep { 0 == $_%2; } @list;
PHP
//using a standard loop $arr = range(1,5); $evens = array(); foreach ($arr as $val) if ($val % 2 == 0) array_push($evens,$val); print_r($evens);
//using a filter function function is_even($var) { return(!($var & 1)); } $arr = range(1,5); $evens = array_filter($arr, "is_even"); print_r($evens);
Python
values = range(10) evens = [x for x in values if not(x & 1)] ievens = (x for x in values if not(x & 1)) # lazy
Ruby
ary = [1,2,3,4,5,6] #or ary = (1..6).to_a even_ary = ary.select{|el|el%2==0} # => [2, 4, 6]
Standard ML
val ary = [1,2,3,4,5,6]; List.filter (fn x => x mod 2 = 0) ary
TCL
TCL doesn't really have a concept of a "number" per se - strictly speaking its only data type is the string (but a string can be interpreted as a number, of course). The generic way of getting certain elements from an array looks roughly like this:
foreach key [array names arr] {if { <condition> } then {puts $tst($key)}}