Re: [問卦] 這段code要怎麼打?已回收
八卦黑客松 Run 起來!
#!/usr/bin/env ruby
# 輸入
input = [1, 4, 6]
# 計算特徵
def calc_pattern(input)
# 把 input 每一個都拿來當指數乘,算總合。
input.inject(0) { |sum, exp| sum + 2**(exp - 1) }
end
# 旋轉演算法
def circle_shift(input, max = 6)
target = input
output = []
max.times do
# 把尾巴加到頭。
target = target / 2 + (target % 2) * 2**(max - 1)
# 收集這些計算後的整數。
output << target
end
output
end
# 餘數分整數
def separate(input)
output = []
cursor = 1
target = input
until target < 1
output << cursor if target.odd?
cursor += 1
target /= 2
end
output
end
# 執行程式並收集未排序的列表。
unsorted = []
circle_shift(calc_pattern(input)).each do |set|
unsorted << separate(set)
end
# 不要計較排序演算法了!直接從後面排回前面。
unsorted
.sort_by { |array| array[2] }
.sort_by { |array| array[1] }
.sort_by { |array| array[0] }.each do |set|
p set
end
# 執行結果:
#=> [1, 2, 5]
#=> [1, 3, 4]
#=> [1, 4, 6]
#=> [2, 3, 6]
#=> [2, 4, 5]
#=> [3, 5, 6]
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.250.246.160
※ 文章網址: https://www.ptt.cc/bbs/Gossiping/M.1523899847.A.D07.html
→
04/17 01:31,
7年前
, 1F
04/17 01:31, 1F
推
04/17 01:31,
7年前
, 2F
04/17 01:31, 2F
推
04/17 01:32,
7年前
, 3F
04/17 01:32, 3F
→
04/17 01:32,
7年前
, 4F
04/17 01:32, 4F
→
04/17 01:34,
7年前
, 5F
04/17 01:34, 5F
推
04/17 01:34,
7年前
, 6F
04/17 01:34, 6F
推
04/17 01:35,
7年前
, 7F
04/17 01:35, 7F
→
04/17 01:44,
7年前
, 8F
04/17 01:44, 8F
→
04/17 01:48,
7年前
, 9F
04/17 01:48, 9F
推
04/17 02:00,
7年前
, 10F
04/17 02:00, 10F
→
04/17 03:15,
7年前
, 11F
04/17 03:15, 11F
推
04/17 03:28,
7年前
, 12F
04/17 03:28, 12F
※ 編輯: zero00072 (118.163.133.244), 04/17/2018 11:59:48
※ 編輯: zero00072 (118.163.133.244), 04/18/2018 10:11:26
※ 編輯: zero00072 (118.163.133.244), 06/15/2018 13:16:24
討論串 (同標題文章)
完整討論串 (本文為第 16 之 18 篇):