-2
Q
活動時間表
A
回答
1
我們的問題可能會被認爲是一個有點簡單化, 所以我們的答案是簡單,太(在Ruby中):
#! /usr/bin/ruby
DAY_START = 0
DAY_END = 24*60
# now, busy periods as array of size-2 arrays:
BUSY_PERIODS = [[9*60, 9*60+30], [18*60+30, 20*60]]
ADDITIONAL_EVENT_LENGTH = 1*60 + 25 # that is, 1 hour and 25 minutes
# in this simple program,
# time will be expressed as a number of minutes of the day
# for more advanced use, try to use "Time" class
# now let define a function to compute the list of free periods
# from a given list of busy periods:
def compute_free_periods(list_of_events)
# at the begining of the calculation, our whole day is assumed free:
free_periods = Array[ [ DAY_START, DAY_END] ]
# now, one by one, let us take away the busy periods:
list_of_events.each_with_object free_periods do | busy_period, free_periods |
# we use 'each_with_object' version of 'each' enumerator
# list of free_periods is passed in as an external object
# so that we can gradually take each busy period away from it
# let us first note the end time for the last free period
# ([-1] refers to the last element of the list of free periods,
# subsequent [1] to the second element of the size-2 array)
last_free_period_end = free_periods[-1][1]
# and now, let us split this last free period into two by the
# current busy period (busy periods assumed non-overlapping and
# sorted in ascending order)
# first, end time of the last free period is set to the beginning
# of the busy period that we consider in this iteration:
free_periods[-1][1] = busy_period[0]
# then, additional free period is appended to the list of
# free periods usind << operator, starting when the busy period ends:
free_periods << [ busy_period[1], last_free_period_end ]
end
end
# now, let us use the function we just defined:
free_periods = compute_free_periods(BUSY_PERIODS)
# Now, for each free period we will count how many times we can stuff
# in the desired additional event:
free_period_capacities = free_periods.map { |free_period|
# we are using map function for this, which will return
# an array of the same length as free_periods, having
# performed prescribed modifications on each element:
# first, we calculate how many minutes a free period has
period_length = free_period[1] - free_period[0]
# then, we will use whole-number division to get the number
# of additional events that can fit in:
period_length/ADDITIONAL_EVENT_LENGTH
# (in Ruby, whole number division is the default behavior of/operator)
}
# and finally, we sum up the free period capacities for our new event:
total_events_possible = free_period_capacities.reduce(:+)
# (summing is done elegantly by using reduce function with + operator)
# the last remaining thing we can do to consider program finished is to
puts total_events_possible # print the result on the screen
如果你拿出的意見和縮短,程序變得相當短:
#! /usr/bin/ruby
DAY_START, DAY_END = 0, 24*60
BUSY_PERIODS = [[9*60, 9*60+30], [18*60+30, 20*60]]
ADDITIONAL_EVENT_LENGTH = 1*60 + 25
def compute_free_periods(list_of_events)
free_periods = Array[ [ DAY_START, DAY_END] ]
list_of_events.each_with_object free_periods do | busy_period, free_periods |
last_free_period_end = free_periods[-1][1]
free_periods[-1][1] = busy_period[0]
free_periods << [ busy_period[1], last_free_period_end ] end
end
free_periods = compute_free_periods(BUSY_PERIODS)
free_period_capacities = free_periods.map { |free_period|
period_length = free_period[1] - free_period[0]
period_length/ADDITIONAL_EVENT_LENGTH }
puts (total_events_possible = free_period_capacities.reduce(:+))
你看,沒有太多的算法。只是簡單的編碼。拿起 例如。 Ole Foul Zed的教科書Learn Ruby The Hard Way和開始 從練習0讀到最後。或者,您可以使用其他語言, ,例如Python。 Ole Zed教授人才。如果您喜歡 體驗,不要忘記購買它作爲電子書。
相關問題
- 1. 活動時間表DB
- 2. Acumatica活動時間
- 3. R活動時間
- 4. 如何測試活動活動時間
- 5. 活動MQ超時時間
- 6. Redmine花時間和活動
- 7. 如何減少活動啓動時間
- 8. 安裝時間表以啓動用戶活動
- 9. 如何獲取Android上前臺活動的活動時間
- 10. 需要在活動時間之前顯示活動標題(fullcalendar)
- 11. 活動開始時間過長
- 12. 空閒時間不活動檢測
- 13. Android - 2活動在同一時間
- 14. 增加活動加載時間
- 15. 返回第一個活動的時間
- 16. Android長時間運行活動/服務
- 17. Android長時間更新活動
- 18. Php中的時間計算活動
- 19. 應用程序和活動時間
- 20. 從facebook應用上次活動時間?
- 21. Android活動需要時間加載
- 22. 評估Android活動渲染時間
- 23. 活動日誌 - 持續時間
- 24. Ratingsystem一種考慮時間和活動
- 25. 從FB中刪除時間線活動
- 26. Android和回主活動時間結束
- 27. 在特定時間發起活動
- 28. 改善活動開始時間(AsyncTask?)
- 29. 活動加載時間計算
- 30. 在活動之間保持locationListener活動
你試過了什麼? –
我試過簡單的解決方案,但現在我搜索特殊算法。 – dizpers
實際上沒有任何算法需要考慮,因爲已經很幼稚的算法在O(n):))中完成任務)。但是,等一下,可能會有一件事情 - 犧牲性能的內存,將計數維持爲每個空閒時段的數組,並且當您的每日時間表更改時,只更新那些已更改的空閒時段。然而,除非你的一天有像DNA那樣的百萬部分,否則沒有太多的收穫:))) –