Skip to content

Instantly share code, notes, and snippets.

View xullnn's full-sized avatar
🎯
Focusing

xullnn

🎯
Focusing
View GitHub Profile
@xullnn
xullnn / knapsack.rb
Last active April 27, 2018 13:16
the knapsack problem
class Item
attr_accessor :name, :weight, :price
end
class Knapsack
attr_accessor :items, :capacity, :table
def initialize(capacity)
@items = []
@capacity = capacity
@xullnn
xullnn / travel_destination.rb
Last active April 27, 2018 13:16
Find the best destination combinations
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
@xullnn
xullnn / binary_s.rb
Last active June 30, 2017 09:44
Binary search _ caven
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