Ruby 语言 思想驱动生活

June 29, 2007

绿色兵团

Filed under: 生活 — liubin @ 23:48

真是没见识,前些天才知道这首歌,好像02年就有了。
作者好像是青年小伙子,两个人的组合,他们的主页应该是http://www.youthnb.com/

这个是小河的版本。在原来的基础上更加有趣了。
歌词:
在一个漆黑的夜晚, 我和你背着降落伞
腰间的小刀一直闪, 再闪
就要去第一关, KAKA

为保卫祖国的安全, 为保卫人类的家园
为夺回列宁的遗产, 遗产
我们跟丫作战, KAKA

火箭筒有三发炮弹, 见到了梯子最危险
捡来的手枪有时间, 时间最后像狗一样, 过关
(为祖国健康工作五十年,我们是绿色兵团)

百度的一道题

Filed under: Ruby — liubin @ 22:59

题目:有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。

用Ruby来解,不知道结果对不对。

  1. #define the Ant class
  2. class Ant
  3.   attr_accessor :direction,:pos,:id
  4.  
  5.   def veer # turn back when run into other ants
  6.     @direction = 1 - @direction
  7.   end
  8.  
  9.   def arrived?
  10.      return true if @pos == 0 or @pos == 27
  11.      return false
  12.   end
  13.  
  14.   def step
  15.      if @direction == 1 then
  16.          @pos = @pos + 1
  17.      else
  18.          @pos = @pos - 1
  19.      end
  20.   end
  21.  
  22.   def run_into(a) # if run into other ant,return true if so
  23.       return false if a.direction == self.direction 
  24.       return true  if a.pos == self.pos
  25.       return false
  26.   end
  27.  
  28.   def to_s
  29.      "id:#{@id}.direction:#{@direction}.pos:#{@pos}"
  30.   end
  31.  
  32. end # end of Ant
  33.  
  34. class Hash
  35.    def keys
  36.        a = Array.new
  37.        self.each_key do |k|
  38.          a.push k
  39.        end
  40.        a
  41.    end
  42. end
  43.  
  44. #initialize the ant[5] array
  45. def create_ant(d1,d2,d3,d4,d5)
  46.     a1,a2,a3,a4,a5=nil,nil,nil,nil,nil
  47.     (1..5).each do |i|
  48.        eval " a#{i} = Ant.new;a#{i}.id = #{i};a#{i}.direction = d#{i};"
  49.     end
  50.  
  51.     a1.pos = 3
  52.     a2.pos = 7
  53.     a3.pos = 11
  54.     a4.pos = 17
  55.     a5.pos = 23
  56.     return [a1,a2,a3,a4,a5]
  57.    
  58. end
  59.  
  60. #if all ant has arrived ,return true
  61. def all_arrived a
  62.    a.each do |a1|
  63.       return false unless a1.arrived?
  64.    end
  65.    return true
  66. end
  67.  
  68. # get one plan's cost
  69. # p is for puts the ant's progress if it is set to true
  70. def travel(d1,d2,d3,d4,d5,p=false)
  71.    c = 0 #total cost
  72.    x = 0 #loop variables
  73.    onway = create_ant(d1,d2,d3,d4,d5)
  74.    while true
  75.      break if all_arrived(onway)
  76.      x = 0
  77.      while (x < onway.length-1) do
  78.          (x=x+1 ; next) if onway[x].arrived?
  79.      if onway[x].run_into onway[x+1] then
  80.         onway[x].veer
  81.         onway[x+1].veer
  82.             x = x + 2
  83.      else
  84.         x = x + 1
  85.      end
  86.      end
  87.      onway.each do |ai|
  88.          ai.step unless ai.arrived?
  89.      end
  90.      c = c + 1
  91.      print "   step#{format("%2d",c)} : #{format("%-2d",onway[0].pos)},#{format("%-2d",onway[1].pos)}," if p
  92.      print "#{format("%-2d",onway[2].pos)},#{format("%-2d",onway[3].pos)},#{format("%-2d",onway[4].pos)} \n" if p
  93.  
  94.    end
  95.    c
  96. end
  97.  
  98. #path = {path length=><Array of direction info>}
  99. path = Hash.new
  100.  
  101. [1,0].each do |d1|
  102. [1,0].each do |d2|
  103. [1,0].each do |d3|
  104. [1,0].each do |d4|
  105. [1,0].each do |d5|
  106.     cost = travel(d1,d2,d3,d4,d5)
  107.     (path[cost]||=Array.new).push binding
  108. end
  109. end
  110. end
  111. end
  112. end
  113.  
  114. puts "short path length : #{path.keys.sort.first},path count :#{path[path.keys.sort.first].length}"
  115.  
  116. path[path.keys.sort.first].each_with_index do |b,i|
  117.     puts " short path  #{format("%2d",i+1)}:"
  118.     eval " print '   direction : ',d1,' ',d2,' ',d3,' ',d4,' ',d5,\"\n\"",b
  119.     #eval "travel(d1,d2,d3,d4,d5,true)", b
  120. end
  121.  
  122. puts "long path length : #{path.keys.sort.last},path count :#{path[path.keys.sort.last].length}"
  123.  
  124. path[path.keys.sort.last].each_with_index do |b,i|
  125.     puts " long path #{format("%2d",i+1)}:"
  126.     eval " print '   direction : ',d1,' ',d2,' ',d3,' ',d4,' ',d5,\"\n\"",b
  127.     #eval "travel(d1,d2,d3,d4,d5,true)", b
  128. end

June 28, 2007

Sql/mp的Numeric和Decimal

Filed under: 技术 — liubin @ 19:20

Sql/mp是hp的NSK系统上的数据库,应该是老版本的了,新的叫SQL/MX。
SQL/mp里为什么要弄这么多的数值型的数据类型:NUMERIC,SMALLINT,INTEGER,LARGEINT,FLOAT,REAL,DOUBLE PRECISION,DECIMAL等。
而且这些类型好像还都能用在DDL语句里,而有些又是互相重合的,真的有点混。

今天主要看了一下NUMERIC和DECIMAL这两个类型。
这两个光看字面很难区分,numeric就是数字的意思,decimal是十进制的意思,不像int和float那样有没有小数点那么区别明显。
其实这两个类型的区别应该是在内部存贮上:
Numeric就是binary的,也就是内部形式。比如1,会存为0×0001
Decimal是基于ascii的,比如1会存为0×0031
(more…)

June 23, 2007

侯耀文也走了

Filed under: 生活 — liubin @ 21:04

刚才在水母上看到的。
前不久好像马季刚走,个人喜欢超过侯宝林的大师。
侯耀文虽然不如他老爸,但是也是很喜欢的。
可惜。
个人不是很喜欢郭德纲,不知道今后的相声会如何。

亨利真的去了巴萨了

Filed under: 生活 — liubin @ 20:03

去年就应该去了,这么好的人没拿到过欧冠。
虽然不能保证巴萨明年还能拿到欧冠。
希望亨利走好,毕竟年纪这么大了。

June 22, 2007

binding

Filed under: Ruby — liubin @ 19:47

今天看Ruby Cookbook发现了这里面对binding的解释是目前看到的比较通俗易懂的了。
A Binding object is a bookmark of the Ruby interpreter’s state. It tracks the values of any local variables you have defined, whether you are inside a class or method definition, and so on.

Once you have a Binding object, you can pass it into eval to run code in the same context as when you created the Binding. All the local variables you had back then will be available. If you called Kernel#binding within a class definition, you’ll also be able to define new methods of that class, and set class and instance variables.

binding方法会返回一个Binding对象的实例,它记载了创建它的时候的环境,比如当时的变量等。然后你可以把这个binding变量传给方法等,它经常作为eval的第二个参数,这样eval里面就不会因为找不到变量出错了。
(more…)

June 21, 2007

不喜欢姚政

Filed under: 生活 — liubin @ 20:34

今天看了他的的一段视频,至少是不喜欢他模仿万晓利。

NSK中进程管理的两个命令

Filed under: 技术 — liubin @ 19:16

status是显示进程信息,类似ps,status *,user表示显示所有当前用户的进程
status *,term表示由当前term启动的进程。
stop则是停止一进程,类似kill

还有一个命令就是run,用来启动一个程序,而且能启动时候会给进程付与一个名字,比如下面的$y4s5。

$VOL2 LIU 58> status *,user
Process Pri PFR %WT Userid Program file Hometerm
$Y4RZ 0,241 150 R 000 96,5 $SYSTEM.SYS04.TACL $ZTN00.#PT7BE
U0
$Y4S5 0,547 149 015 96,5 $SYSTEM.SYS04.EMSDIST $ZTN00.#PT7BE
TS
$VOL2 LIU 59> stop $y4s5
$VOL2 LIU 60> status *,term
Process Pri PFR %WT Userid Program file Hometerm
$Y4RZ 0,241 150 R 000 96,5 $SYSTEM.SYS04.TACL $ZTN00.#PT7BE

June 19, 2007

Google的本地化?

Filed under: 垃圾 — liubin @ 21:22

您好!

我了一技,致了某些已停的 AdWords
告系列容网中得展示次。此确您的已受到影。致了您的某些已停的告系列容网展示生次和
用。

我的工程已解此,您受到影的告系列已恢复正常。我向您的放了97.37 CNY
的信用,以您停的告系列生的用行。要在 AdWords 中查看信用,按以下步操作:

1. 通 https://adwords.google.com 登 AdWords 。
2. “我的”。
3. “算首”。
4. 您的信用示在算摘要中的整列收支下。

如果您有其他,支持中心,网址

https://adwords.google.com/support&hl=zh_CN,中可以找到多常的解答。

由此可能您的任何不便,我深表歉意。感您的理解。我的工程正努力找此的根源,并且采取相措施确保以后不再生似
情。

衷心感!

Google AdWords 小敬上
原文http://www.newsmth.net/bbstcon.php?board=ITExpress&gid=304665

June 18, 2007

本赛季最后一个冠军

Filed under: 生活 — liubin @ 19:59

今晨,西甲,皇家马德里第30次夺冠。
巴萨不该怪别人,只能明年再来了,亨利会来吗?

国米今年的冠军实在吗?希望明年再来证明。

曼联还是不错的,一直比较喜欢。不过明年如何看英超呢?

还有我仁,明年连欧冠都不能参加,马凯也要被卖了,克洛泽能来吗?

不过好像下个月开始就是亚洲杯了,可惜,这个节骨眼,看还是不看,怎么看?

两个人的100%。
我50%,她50%。

Older Posts »

Powered by WordPress