Skip to content

Instantly share code, notes, and snippets.

@peco8
Created August 11, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peco8/5517ae2c705ee63a25bb032e8a9334ec to your computer and use it in GitHub Desktop.
Save peco8/5517ae2c705ee63a25bb032e8a9334ec to your computer and use it in GitHub Desktop.
benchmark
class ProgrammerProvicer
PROGRAMMERS = [
{
name: 'tanaka',
skills: ['Java','Oracle', 'HTML']
},
{
name: 'toshiki',
skills: ['C','Ruby', 'php']
},
{
name: 'aka',
skills: ['Python','oop', 'sushi']
}
]
def get_programmer_skills(name)
PROGRAMMERS.select{ |v| v[:name] == name}.first
end
end
require 'benchmark'
def linear_search(arr_size, value)
arr = (1..arr_size).to_a
arr.each do |number|
if number == value
return "Found"
end
end
end
def binary_search(arr_size, value)
arr = (1..arr_size).to_a
left = 0
right = arr.last
mid = 0
while left <= right do
mid = (left + right) / 2
if arr[mid] == value
return "Found!"
elsif arr[mid] < value
left = mid + 1
else
right = mid - 1
end
end
end
##########################
# Find something in array
def find_something(arr_size, value)
ret = nil
array = (1..arr_size).to_a
array.each do |item|
if item == value
return "FOUND!"
end
end
ret
end
# Find something in array with Enumaerable#Find
def find_something_with_find(arr_size, value)
array = (1..arr_size).to_a
array.find{|item| return "FOUND" if item == value}
end
###########################
def each_join(attr_size)
str = ""
array = (1..attr_size).to_a
array.each do |item|
str += "#{array.index(item)}='#{item}'"
end
str
end
def map_join(attr_size)
array = (1..attr_size).to_a
str = array.map do |item|
"#{array.index(item)}='#{item}'"
end
str
end
###########################
def reject_zero
%w(1 0 2 3 5 6).map(&:to_i).select(&:nonzero?)
end
def reject_zero_short
%w(1 0 2 3 5 6).map(&:to_i).reject(&:zero?)
end
###########################
def get_env(options = {})
env = options[:env]
env.fetch(:languages)
end
def get_env_better(options = {})
env = options.fetch(:env){ :env_not_set }
env.fetch(:languages)
end
##########################
class Pencil
def write_charactors(color, depth)
"#{color}色、濃さ#{depth}で文字を書いた"
end
def write_pictures(color, depth)
"#{color}色、濃さ#{depth}で絵をかいた"
end
end
class PencilBundle
def write_charactors(pencil_style)
"#{pencil_style.color}色、濃さ#{pencil_style.depth}で文字を書いた"
end
def write_pictures(pencil_style)
"#{pencil_style.color}色、濃さ#{pencil_style.depth}で文字を書いた"
end
end
PencilStyle = Struct.new(:color, :depth)
pencil_style1 = PencilStyle.new("赤", "B")
pencil_style2 = PencilStyle.new("緑", "HB")
pencil = PencilBundle.new
[pencil_style1, pencil_style2].each do |pencil_style|
# puts pencil.write_charactors(pencil_style)
# puts pencil.write_pictures(pencil_style)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment