This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item | |
attr_accessor :name, :weight, :price | |
end | |
class Knapsack | |
attr_accessor :items, :capacity, :table | |
def initialize(capacity) | |
@items = [] | |
@capacity = capacity |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Destination | |
attr_accessor :name, :time_cost, :weight | |
def initialize(name,time_cost,weight) | |
@name = name | |
@time_cost = time_cost | |
@weight = weight | |
end | |
end | |
class Schedule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
def binary_search(arr, given_element) | |
length = arr.size | |
t = 1 # search times, used to locate every step's relative_position | |
relative_position = 0.5 # compared element's relative position, initialized as 0.5 (1/2) | |
current_index = (1/2.to_f * length) | |
e = arr[current_index] # the element compared with the given_element, first time it'll be the "middle" one |
NewerOlder