Conference Track Management
快速访问
准备Node.js环境(已经装好Node.js环境略过这一步)
简易教程,仅供参考
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash$ nvm install 6
帮助信息 https://github.com/creationix/nvm
快速运行
$ git clone https://github.com/chiedey/ConferenceTrackManagement.git$ cd ConferenceTrackManagement$ node index.js data/data1.txt# 除了GitHub,项目同时发布到了NPM上面,你也可以 $ npm i conference-track-management$ cd node_modules/conference-track-management$ node index.js data/data1.txt
将会看到类似的屏幕输出
Track 1:09:00AM Ruby Errors from Mismatched Gem Versions 45min09:45AM Lua
测试
全局安装mocha
$ npm i mocha -g
进入项目目录,安装测试所需要的依赖包
$ cd ConferenceTrackManagement$ npm i
测试
$ mocha test/*
将会看到类似的屏幕输出
Testing
其他
可以指定多个输入文件,仓库自带了几个数据文件,可以看到不一样的屏幕输出效果
$ node index.js data/data1.txt data/data2.txt data/data3.txt data/data3plus.txt# 或者 $ node index.js data/*
将会看到类似的屏幕输出
Track 1:09:00AM Ruby Errors from Mismatched Gem Versions 45min09:45AM Lua
可以看到上面的输出内容比较多,不是很友好,当然这个可以得到有效的控制。接下来的部分将会涉及到如何解决这个问题。 配置文件相对比较灵活,是可以定制的,我们可以根据实际情况,指定配置文件,先来看怎么指定配置文件(如果未指定,将读取默认配置文件)
$ node index.js --cfg config/config.weekend.js data/data3.txt data/data3plus.txt# 或者把--cfg简写成-c $ node index.js -c config/config.weekend.js data/data3.txt data/data3plus.txt# 可以随意调换参数的位置,放到后面 $ node index.js data/data3.txt data/data3plus.txt -c config/config.weekend.js# 甚至是中间任意位置,都是没问题的 $ node index.js data/data3.txt -c config/config.weekend.js data/data3plus.txt
执行下面这条命令
$ node index.js -c config/config.weekend.js data/data3.txt
输出
Day 1:07:00AM Do the homework of Geography 30min07:30AM Do the homework of English 60min08:30AM Breakfast09:00AM Do the homework of maths 60min10:00AM Paly games 120min12:00PM Lunch02:00PM Chitchat on line 45min02:45PM Online shopping 50min03:35PM Do the laundry 45min04:20PM Make a call to Mom 60min05:20PM Make a call to Mason 40min06:00PM Dinner07:00PM Fit 40min07:40PM Watch news 20min08:00PM Watch movies 120min10:00PM Watering the flowers 5min10:05PM Feed a dog 5min10:10PM Burn bath 15min10:25PM Do housework 30min10:55PM Have a rest 20min11:15PM Make a call to Dad 10min11:25PM Sleep
恰好能把data3.txt里面的活动安排到一天以内完成。而如果我们活动不止data3.txt这些,我们可以把data3plus.txt也放进去,测试一下结果
$ node index.js -c config/config.weekend.js data/data3.txt data/data3plus.txt
输出
Day 1:07:00AM Do the homework of Geography 30min07:30AM Do the homework of English 60min08:30AM Breakfast09:00AM Do the homework of maths 60min10:00AM Paly games 120min12:00PM Lunch02:00PM Chitchat on line 45min02:45PM Online shopping 50min03:35PM Do the laundry 45min04:20PM Make a call to Mom 60min05:20PM Make a call to Mason 40min06:00PM Dinner07:00PM aaaaa aaaaa aaaaa aaaaa 120min09:00PM Watch movies 120min11:00PM Have a rest 20min11:20PM Make a call to Dad 10min11:30PM Sleep Day 2:11:15AM Watch news 20min11:35AM Watering the flowers 5min11:40AM Feed a dog 5min11:45AM Burn bath 15min12:00PM Do housework 30min12:30PM Brunch02:00PM ccccc ccccc ccccc ccccc 80min03:20PM Fit 40min04:00PM Waiting
可以看到活动都被安排到了周末这两天,细心的朋友可能会发现,活动其实还有剩余的没安排上,其实这是通过配置文件做了天数配置,因为周末只有两天,所以配置文件里面配置了相应的参数来控制行程的天数,使合理化。具体可以去项目根目录下的config文件夹下面查看config.weekend.js文件,里面已经备注好相应的注释。
总结
测试驱动开发是很高效的一种开发模式,在功能完全开发出来之前,我们可以先把测试写出来,尽可能的完整,那么在测试结果的驱动下,项目的开发就会很高效很有针对性,同时对代码的健壮性也是有很大帮助的,前提当然是你愿意花时间先去写出详细的测试代码。大有裨益。
思路
该问题本质上带有算法考题的特点,给定输入,经过运算之后输出符合要求的结果,同时这个题目带有实际应用价值,项目根目录下的data目录下面data2.txt文件就是实际生活中的真实数据,所以问题还是挺有意思的。把各个活动安排到日程表里,可以归纳为是经典的01背包问题,每个活动相当于是待放入背包的物品,只能安排或者不安排,活动时长可视为物品的重量,至于活动的价值本项目默认设置为该项目耗费的时长,以此类推,背包的容量就是每一个时间段的时长,比如09:00到12:00这个sesison期间的时长是180分钟。所以该问题的日程安排算法可以间接通过01背包问题的解法来运算。
原题
Problem: Conference Track Management You are planning a big programming conference and have received many proposals whichhave passed the initial screen process but you're having trouble fitting them intothe time constraints of the day -- there are so many possibilities! So you write aprogram to do it for you. #The conference has multiple tracks each of which has a morning and afternoon session.#Each session contains multiple talks.#Morning sessions begin at 9am and must finish before 12 noon, for lunch.#Afternoon sessions begin at 1pm and must finish in time for the networking event.#The networking event can start no earlier than 4:00 and no later than 5:00.#No talk title has numbers in it.#All talk lengths are either in minutes (not hours) or lightning (5 minutes).#Presenters will be very punctual; there needs to be no gap between sessions. Note that depending on how you choose to complete this problem, your solution maygive a different ordering or combination of talks into tracks. This is acceptable;you don’t need to exactly duplicate the sample output given here. Test input:Writing Fast Tests Against Enterprise Rails 60minOverdoing it in Python 45minLua for the Masses 30minRuby Errors from Mismatched Gem Versions 45minCommon Ruby Errors 45minRails for Python Developers lightningCommunicating Over Distance 60minAccounting-Driven Development 45minWoah 30minSit Down and Write 30minPair Programming vs Noise 45minRails Magic 60minRuby on Rails: Why We Should Move On 60minClojure Ate Scala (on my project) 45minProgramming in the Boondocks of Seattle 30minRuby vs. Clojure for Back-End Development 30minRuby on Rails Legacy App Maintenance 60minA World Without HackerNews 30minUser Interface CSS in Rails Apps 30min Test output:Track 1:09:00AM Writing Fast Tests Against Enterprise Rails 60min10:00AM Overdoing it in Python 45min10:45AM Lua for the Masses 30min11:15AM Ruby Errors from Mismatched Gem Versions 45min12:00PM Lunch01:00PM Ruby on Rails: Why We Should Move On 60min02:00PM Common Ruby Errors 45min02:45PM Pair Programming vs Noise 45min03:30PM Programming in the Boondocks of Seattle 30min04:00PM Ruby vs. Clojure for Back-End Development 30min04:30PM User Interface CSS in Rails Apps 30min05:00PM Networking Event Track 2:09:00AM Communicating Over Distance 60min10:00AM Rails Magic 60min11:00AM Woah 30min11:30AM Sit Down and Write 30min12:00PM Lunch01:00PM Accounting-Driven Development 45min01:45PM Clojure Ate Scala (on my project) 45min02:30PM A World Without HackerNews 30min03:00PM Ruby on Rails Legacy App Maintenance 60min04:00PM Rails for Python Developers lightning05:00PM Networking Event