Logo

Programming-Idioms

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.
Implementation
Erlang

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Erlang implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
func search(m [][]int, x int) (bool, int, int) {
	for i := range m {
		for j, v := range m[i] {
			if v == x {
				return true, i, j
			}
		}
	}
	return false, 0, 0
}
static class Position{
	int i;
	int j;
}

Position search(int[][] m, int x){
	for(int i=0;i<m.length;i++)
		for(int j=0;j<m[i].length;j++)
			if(m[i][j] == x){
				Position pos= new Position();
				pos.i = i;
				pos.j = j;
				return pos;
			}
	return null;
}
function search(m, x) {
    for (var i = 0; i < m.length; i++) {
        for (var j = 0; j < m[i].length; j++) {
            if (m[i][j] == x) {
                return [i, j];
            }
        }
    }
    return false;
}
sub search {
   my ($x, $m) = @_;
   while ( ($k1,$v1) = each @$m ) {
      while ( ($k2, $v2) = each @$v1 ) {
           return $k1, $k2 if $v2 == $x;
      }
   }
}
fn search<T: Eq>(m: &Vec<Vec<T>>, x: &T) -> Option<(usize, usize)> {
    for (i, row) in m.iter().enumerate() {
        for (j, column) in row.iter().enumerate() {
            if *column == *x {
                return Some((i, j));
            }
        }
    }

    None
}
#include <string.h>
#include <stdlib.h>
void search(void ***m,void *x,size_t memb_size,int len_x,int len_y,int *i,int *j)
{
	typedef void *m_type[len_x][len_y];
	m_type *m_ref=(m_type*)m;

	for(*i=0;*i<len_x;*i+=1)
	{
		for(*j=0;*j<len_y;*j+=1)
		{
			if(!memcmp((*m_ref)[*i][*j],x,memb_size))
			{
				return;
			}
		}
	}
	*i=*j=-1;
}
import std.typecons;
auto search(int[][] m, int x)
{
	foreach (i, row; m)
	{
		foreach (j, cell; row)
		{
			if (cell == x)
				return tuple(i, j);
		}
	}
}
def search(m, x):
    for idx, item in enumerate(m):
        if x in item:
            return idx, item.index(x)
class Position {
  int i, j;
  Position(this.i, this.j);
}
Position search(List<List> m, x) {
  for (var i = 0; i < m.length; i++) {
    var line = m[i];
    for (var j = 0; j < line.length; j++) {
      if (line[j] == x) {
        return new Position(i, j);
      }
    }
  }
  return null;
}
procedure search(m:T2dMatrix; x:TElement; out i,j:integer);
begin
   for i := 0 to high(m) do
	for j := 0 to high(m[i]) do
            if m[i,j] = x then
               exit;
   i := -1;
   j := -1;
end;
def search(m, x)
  m.each_with_index do |row, i|
    row.each_with_index do |value, j|
      return i, j if value == x
    end
  end
  nil
end
search x m = head [ (i, j) | (i, r) <- zip [0..] m,
                             (j, c) <- zip [0..] r, c == x]


type Matrix is array (Positive range <>, Positive range <>) of Integer;

function Search (M : Matrix; X : Integer; I, J : out Integer) return Boolean is
begin
   for K in M'Range (1) loop
      for L in M'Range (2) loop
         if M (K, L) = X then
            I := K;
            J := L;
            return True;
         end if;    
      end loop;
   end loop;

   return False;
end Search;
(int, int) Search(int[,] m, int x)
{
    for (var i = 0; i <= m.GetUpperBound(0); i++)
        for (var j = 0; j <= m.GetUpperBound(1); j++)
            if (m[i, j] == x)
                return (i, j);

    return (-1, -1);
}
function search($x, array $m): ?array
{
    for ($j = 0; $j < count($m); $j++) {
        if (($i = array_search($x, $m[$j])) !== false) {
            return [$i, $j];
        }
    }

    return null;
}
function search(m, x)
   for i,v1 in ipairs(m) do
      for j,v2 in ipairs(v1) do
         if v2 == x then return i,j end
      end
   end
end
  (defn find-in-2d-matrix [m x]
    (for [i (range (count m))
          j (range (count (first m)))
          :when (= x (-> m (nth i) (nth j)))]
      [i j]))
def search[T](m: Iterable[Iterable[T]], x: T): Option[(Int, Int)] = {
  for ((row, i) <- m.view.zipWithIndex; (column, j) <- row.view.zipWithIndex if column == x)
    return Some((i, j))
  None
}
(defun mysearch (x m)
  (loop for i below (array-dimension m 0)
        do (loop for j below (array-dimension m 1)
                 when (eql x (aref m i j))
                 do (return-from my-search
                                 (values i j)))))
#include <utility>
template<typename T, size_t len_x, size_t len_y>
std::pair<size_t, size_t> search (const T (&m)[len_x][len_y], const T &x) {
    for(size_t pos_x = 0; pos_x < len_x; ++pos_x) {
        for(size_t pos_y = 0; pos_y < len_y; ++pos_y) {
            if(m[pos_x][pos_y] == x) {
                return std::pair<size_t, size_t>(pos_x, pos_y);
            }
        }
    }

    // return an invalid value if not found
    return std::pair<size_t, size_t>(len_x, len_y);
}
module Arr = Bigarray.Array2

let search array value =
	let x_max = Arr.dim1 array in
	let y_max = Arr.dim2 array in
	let rec loop x y = 
		(* End of array *)
		if x = x_max then
			raise Not_found
		(* End of row, go to next *)
		else if y = y_max then
			loop (x+1) 0
		else 
			(* If found, return it *)
			if array.{x,y} = value then
				(x,y)
			(* Otherwise go to next col *)
			else
				loop x (y+1)
	in
	loop 0 0





		
bool _search(const Matrix& _m, const float _x, size_t* const out_i, size_t* const out_j) {
  for (size_t j = 0; j < _m.rows; j++) {
    for (size_t i = 0; i < _m.cols; i++) {
      if (_m.at(i, j) == _x) {
         *out_i = i;
         *out_j = j;
         return true;
      }
    }
  }
  return false;
}
function search(m,x)
  integer, dimension(:,:), intent(in) :: m
  integer, intent(in) :: x
  integer, dimension(2) :: search
  search = findloc(x,m)
end function search
fun search(m: Array<Array<Int>>, x: Int): Pair<Int, Int>? {
    m.forEachIndexed { i, row ->
        row.forEachIndexed { j, value ->
            if (value == x) {
                return Pair(i, j)
            }
        }
    }
    return null
}
@import Foundation;
NSIndexPath *search(NSArray *m,id x) {
  NSUInteger j,i;
  for (i=0;i<m.count;i++) {
    if ((j=[m[i] indexOfObject:x])!=NSNotFound)
      return [NSIndexPath indexPathWithIndexes:&i length:2];
  }
  return nil;
}
def search(m, x) do
  Enum.reduce_while(m, {0, 0}, fn list, {index, _inner_index} ->
    {found?, inner_index} =
      Enum.reduce_while(list, {false, 0}, fn item, {_found?, acc} ->
        if x == item, do: {:halt, {true, acc}}, else: {:cont, {false, acc + 1}}
      end)

    if found?, do: {:halt, {index, inner_index}}, else: {:cont, {index + 1, inner_index}}
  end)
end