Skip to content

Instantly share code, notes, and snippets.

View piecehealth's full-sized avatar

Zhang Kang piecehealth

View GitHub Profile
  • don't generate stylesheet and js
# confg/application.rb
    config.generators.stylesheets = false
    config.generators.javascripts = false
  • precompile assets
# config/initializers/assets.rb
Rails.application.config.assets.precompile << Proc.new do |path|
@piecehealth
piecehealth / qs.rb
Created June 4, 2014 10:23
Quick Sort
# 快速排序 递归
def quick_sort arr
return arr if arr.size <= 1
mid = arr.shift
l, r = arr.partition {|i| i <= mid}
quick_sort(l) + [mid] + quick_sort(r)
end
# 快速排序 非递归,可视化
def visualise arr
a= [[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
[16,17,18,19,20],
[21,22,23,24,25]]
class Walker
attr :steps
@piecehealth
piecehealth / 24.rb
Created March 4, 2014 07:55
解24点
def calculator number, *factors
if factors.size == 1
if number.to_f.round(10) == factors[0].to_f.round(10)
return factors[0].to_s
else
return nil # nil stand for no solution
end
else
factors.each_with_index do |factor, idx|
[[:+, :-], [:-, :+], [:*, :/], [:/, :*]].each do |operator|
@piecehealth
piecehealth / to_chinese.rb
Created November 28, 2013 07:45
数字变中文大写。
# encoding: utf-8
def number_to_chinese number
mappings = {
"1" => '壹',
"2" => '贰',
"3" => '叁',
"4" => '肆',
"5" => '伍',
"6" => '陆',
"7" => '柒',
ldap = Net::LDAP.new(encryption: :simple_tls) ldap.host = "ldap.hp.com"
ldap.port = "636"
ldap.auth "uid=#{email},ou=People,o=hp.com", password if ldap.bind
success
else
fail
end
def how_many_water arr
how_many = 0
arr.each_with_index do |height, idx|
next if idx == 0 or idx == arr.size - 1
l_bound, r_bound = arr[0...idx].max, arr[(idx + 1)..(arr.size - 1)].max
if l_bound > height && r_bound > height
bound = l_bound < r_bound ? l_bound : r_bound
how_many += bound - height
end
end
@piecehealth
piecehealth / to_letter.rb
Created September 16, 2013 06:19
26进制。可用于计算Excel的行列坐标
class Fixnum
def to_letter
if self <= 26
return (self + 64).chr
else
if self % 26 == 0
return ((self / 26 - 1).to_letter + 'Z')
else
return ((self / 26).to_letter + (self % 26).to_letter)
end
@piecehealth
piecehealth / gist:6577147
Created September 16, 2013 06:12
腾讯面试题: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数 要求下排每个数都是先前上排那十个数在下排出现的次数。 上排的十个数如下: 【0,1,2,3,4,5,6,7,8,9】 举一个例子, 数值: 0,1,2,3,4,5,6,7,8,9 分配: 6,2,1,0,0,0,1,0,0,0 0在下排出现了6次,1在下排出现了2次, 2在下排出现了1次,3在下排出现了0次.... 以此类推..
def get_all_possible_arrs n, total
return [[0] * n] if total == 0
return [[total]] if n == 1
possible_arrs = []
0.upto(total) do |i|
get_all_possible_arrs(n - 1, total - i).each {|arr| possible_arrs << arr.unshift(i)}
end
return possible_arrs
end
@piecehealth
piecehealth / partition.rb
Last active December 22, 2015 23:49
Ruby数组 分组算法
#zdl = [[1, 3, 5, 10000000, 9], [2, 4, 6, 8, 7]].flatten
#zdl = [1,3,5,7,9,100] + [2,4,6,8,11]
zdl = [1, 2, 3, 4, 5, 100, 6, 7, 8, 9, 11]
#zdl = []
#30.times {zdl << rand(100)}
def partition arr
if arr.size.even?
a, b = arr[0, arr.size / 2], arr[arr.size / 2, arr.size / 2]
else