How can I find the cells adjacent to a cell in a matrix in OCaml? -


for example, given list [3;5;6;2;10;4;9;1;3] maps out matrix this:

3 5 6 2 10 4 9 1 3 

i have coded function "find_size" find size of matrix given list (for example, give me int 3)

as function "find_position" tell me in matrix 10 (so here, give me tuple (1, 1) because in second row , second column)

i think best way approach iterate on list using fold function applies given function each element , keeps track of results. therefore, goal create function tell me if element in matrix adjacent 10 in ocaml (if is, tell fold function add element answer list).

the final answer [5;2;4;1] because elements adjacent 10.

you can translate size , position values list of adjacent positions tuples. can translate size , tuple single list index. sorted list of indices, can fold on list , extract elements @ given indices.

this lot easier if used array of numbers, it's worth.

without seeing code you've tried, , hearing how did or didn't work you, it's difficult more this.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -