Skip to content

Instantly share code, notes, and snippets.

@frangucc
Last active December 11, 2015 03:39
Show Gist options
  • Save frangucc/4539550 to your computer and use it in GitHub Desktop.
Save frangucc/4539550 to your computer and use it in GitHub Desktop.
Answer
API is implemented at :
http://ariise.com/api/matrix/min_weigth.json-
- mandatory parameter: matrix.
Matrix is sent to server as string, like: "[[3,1,4],[1,5,9],[2,6,5]]",
"[[3,1,4],[1,5,9],[2,6,5,4,7]]", etc.
Return:
- status: 200: valid call, 205: invalid matrix
- min_path: min weight path, as tring, for example : "[1,1,4]"
- total_paths: total min paths
Example:
curl -X POST 'http://ariise.com/api/matrix/min_weigth.json' -d
'matrix=[[3,1,4],[5,5,9],[2,6,2]]'
Response: {"status":200,"min_path":"[1,5,2]","total_path":4}
curl -X POST 'http://ariise.com/api/matrix/min_weigth.json' -d
'matrix=[[3,1,4],[5,9],[2,6,1]]'
{"status":200,"min_path":"[1,5,1]","total_path":1}
curl -X POST 'http://ariise.com/api/matrix/min_weigth.json' -d
'matrix=[[3,1,4],[5,9, 6],[2,6,1]]'
Response: {"status":200,"min_path":"[1,5,1]","total_path":1}
curl -X POST 'http://ariise.com/api/matrix/min_weigth.json' -d
'matrix=[[3,1,4],[5,9, 6],[a,b]]'
Response: {"status":205,"message":"invalid matrix"}, because the last
row does not content any numeric element.
@frangucc
Copy link
Author

def min_weigth_path
matrix = contruct_matrix
unless validate_matrix(matrix)
render json: {status: 205, message: "invalid matrix"}
return
end
min_path = []
total_path = 1
matrix.each do |row|
min_ele = row.min
t_lengh = 0
row.each do |ele|
t_lengh += 1 if ele == min_ele
end
total_path = total_path*t_length
min_path << min_ele
end
path = "["
path << min_path.join(",")
path << "]"

render json: {status: 200, min_path: path, total_path: total_path}
end

@frangucc
Copy link
Author

Lol, Jon.

Easy, in what language. Hahaha.

@datapimp
Copy link

class MatrixWeightFinder
  attr_accessor :matrix

  def initialize(matrix=[])
    @matrix = matrix
  end

  def find_weight
    matrix.inject([]) do |memo, row|
      memo << row.min
    end
  end
end


sample_matrix = [[3, 1, 4], [5, 9, 6], [2, 6, 1]]

MatrixWeightFinder.new(sample_matrix).find_weight # => [1,5,1]

@datapimp
Copy link

var sample = [[3, 1, 4], [5, 9, 6], [2, 6, 1]];

//using underscore.js
_( sample ).reduce(function(memo,row){
  memo.push( _( row ).min() );
  return memo
}, []);  //=> [1,5,1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment