Created
November 4, 2014 04:55
-
-
Save osyoyu/bddd554daf49a7a90c74 to your computer and use it in GitHub Desktop.
らくだメソッド
This file contains 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
# coding: UTF-8 | |
# 山を確認するためだけのメソッド | |
def show_array(array) | |
array.each {|e| | |
puts "#{e}: #{"***" * e}" | |
} | |
end | |
def camelize(target, bump_count) | |
# 元の配列を bump_count 個のコブに分割する | |
bumps = Array.new | |
bump_count.times do |i| | |
bumps[i] = Array.new | |
end | |
target.each.with_index {|e, i| | |
bumps[i % bump_count].push(e) | |
} | |
# それぞれのコブの中で小さい方から順番に左右交互に積んでいく | |
bumps = bumps.map do |bump| | |
tmp = Array.new | |
tmp = bump.select.with_index {|e, i| i % 2 == 0} | |
bump.reject!.with_index {|e, i| i % 2 == 0} | |
bump += tmp.reverse | |
end | |
# コブを合成して返す | |
return bumps.inject(Array.new) {|result, e| result + e} | |
end | |
target = [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7] | |
show_array(target) | |
puts "-------------------------------" | |
show_array(camelize(target, 7)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment