25

ankur帮我们做codereview后,给我们提出了以下应该注意的几个问题:

  • Do not add comments saying “add by engineer”. Cvs can keep track of who made changes. If you want to add comments please add what the specific snippet of code does.
  • Avoid hard coded constants if you think the constant might need to be changed for other devices. It is better to avoid them totally, so that when you define the constant you can name it to be something descriptive of what the constant does.
  • Please do not have device specific defines. Have defines based on the functionality instead. It is much more useful for other engineers so that they can look at the code and figure out what the define does.
  • Make you best attempt to have correct spellings in the code. It does help a lot in searching. For example the correct spelling for Constant.UI_SLECET_LANGUGE_SECRREN_X  is Constant.UI_SELECT_LANGUAGE_SCREEN_X
  • Refrain from commenting out code. Just remove it totally so that the code is easier to read.

特别是功能性的名字需要注意,高亮显示一下……

06

最近研究编写firefox插件,发现firefox的架构相当的值得揣摩。

firefox和thunderbird都是运行在xulrunner上,本身类似个插件。底层由xpcom来实现跨平台,它类似ms的com,非常容易扩展;view层是用xul来描述的,xul类似html,是cross platform user interface language的简写;javascript来粘合xpcom和xul的。

如果在j2me平台上,我们利用j2me实现底层的com组件,用类xml的语言来描述界面,用一种脚本来粘合两者,这样作适配只需要在xml层实施,易可视化操作,逻辑由com组件来实现,而且组件易扩展.

firefox扩展制作的相关资源:

extension developer(含有javascript shell):https://addons.mozilla.org/en-US/firefox/addon/7434

extension wizard:http://ted.mielczarek.org/code/mozilla/extensionwiz/

xul explorer:https://developer.mozilla.org/en/XUL_Explorer

16

1.蒙提霍尔问题

假設你正在參加一個遊戲節目,你被要求在三扇門中選擇一扇:其中一扇後面有一輛車;其餘兩扇後面則是山羊。你選擇了一道門,假設是一號門,然後知道門後面 有甚麼的主持人,開啟了另一扇後面有山羊的門,假設是三號門。他然後問你:「你想選擇二號門嗎?」轉換你的選擇對你來說是一種優勢嗎?

解答:把选择的和没选的当作一个整体,概率分别是1/3,2/3,然后当主持人开启一扇门之后,转换后可以使的概率变成2/3

或者,选择山羊1,概率1/3,转换是优势

选择山羊2,概率1/3,转换是优势

选择汽车,概率1/3,转换不是优势

所以选择转换的概率是2/3

参考:http://en.wikipedia.org/wiki/Monty_Hall_problem

2.海盗博弈

有五个理性的海盗,A, B, C, D和E,找到了100个金币,需要想办法分配金币。

海盗们有严格的等级制度:A比B职位高,B比C高,C比D高,D比E高。

海盗世界的分配原则是:等级最高的海盗提出一种分配方案。所有的海盗投票决定是否接受分配,包括提议人。并且在票数相同的情况下,提议人有决定权。如果提议通过,那么海盗们按照提议分配金币。如果没有通过,那么提议人将被扔出船外,然后由下一个最高职位的海盗提出新的分配方案。

海盗们基于三个因素来做决定。首先,要能存活下来。其次,自己得到的利益最大化。最后,在所有其他条件相同的情况下,优先选择把别人扔出船外。

解答:

直觉上认为,A海盗会给自己分配很少,以避免被扔出船外。然而这和理论结果相差甚远。

让我们反过来看:如果只剩下D和E,D给自己100个金币,给E 0个。因为D有决定权,所以分配达成。

如果剩下三个人(C,D和E),C知道D下轮会给E 0个金币,所以C这轮给E 1个金币,让E支持自己以使得提议通过。因此如果剩下三个人,结果是C:99,D:0,E:1。

如果B, C, D 和 E 剩下, B 知道上述结果。所以为了避免被扔出去,他只需要给D 1个金币,因为他有决定权,只需要D的支持就足够了。因此他会提议 B:99, C:0, D:1,E:0。有人可能想到提议B:99, C:0, D:0,E:1,因为E知道即使把B扔出去,也不会得到更多了。但由于海盗会优先把别人扔出去,所以E会选择杀死B,然后仍然可以从C的提议中得到相同金 币。

假设A知道所有的一切,他就能选择让C和E来支持他,提议变成:

  • A: 98金币
  • B: 0金币
  • C: 1金币
  • D: 0金币
  • E: 1金币

同样的 A:98,B:0,C:0,D:1,E:1 或者其他的提议都不是最好的,因为D会选择把A扔出去,然后从B那里得到相同的金币。


参考:http://en.wikipedia.org/wiki/Pirate_game

Tagged with:
04

在自己的机子上装上了openssh,开启/usr/sbin/sshd时,总是:

Could not load host key: /etc/ssh/ssh_host_dsa_key

Could not load host key: /etc/ssh/ssh_host_rsa_key

到自己的/etc/ssh下发现没有上面两个文件,后来

/etc/rc.s/sshd start

启动服务成功,并且在/etc/ssh下生成了上面不存在的文件了

但自己的ssh localhost总是失败:

ssh_exchange_identification: Connection closed by remote host

检查了/etc/hosts.allow和/etc/hosts.deny发现在/etc/hosts.deny中有

ALL:ALL:DENY

把这行注释掉,就OK了

enjoy it!

03

接上程序设计风格(一)

  • 一致性和习惯性
  • 注释

3.一致性和习惯性

一致性高于习惯又来自习惯。

习惯性:各种语言的习惯写法,层次多的时候用花括号,if-else的缩进,switch的break和default

函数宏:副作用,效率低,易出错,所以少用函数宏

魔法数:常量(不是宏)。灵活性写法:用函数,宏………

4.注释

注释应当简练概括,与代码相辅相成。一般全局变量,常量,函数,结构/类型需要注释。

当注释过于复杂,说明代码需要修改/重写了。

Tagged with:
28

最近在看<程序设计实践>,很是受用,故记下之。

好的程序设计的风格具有以下几点:

  • 具有功能说明性的名字
  • 直接了当的逻辑和自然的表达式
  • 一致性和习惯性
  • 注释

这篇写前两点。

1.具有功能说明性的名字

名字应该简单,并且说明它的功能性。一般全局变量具有足够长,足够的说明性,而局部变量则可简短些。

命名约定:numPending/num_pending,看个人喜好,个人喜欢前种用于函数名/结构性的名字,后者用于变量名.

名字也可以加上类型和功能方面的文字,全局变量可以以大写字母开头,局部变量以小写开头,常量则全部大写。

如局部变量中:i,j作为循环变量,p,q作为指针,s,t作为字符串变量,pch表示字符指针……..

在有命名空间的语言里,名字更应当注意上下文,简练,但又不失功能性说明。

函数名,最好用动词+名词,返回布尔型值的函数,is*,in*等名字就很不错。

2. 直接了当的逻辑和自然的表达式

缩进。

运算符的优先级,多使用()。

逻辑简明。如,测试中的否定看是否能改进。

慎用三元运算符。

当心副作用。如++,–等运算的与副作用有关执行顺序;函数参数求值的顺序。

Tagged with:
21

今天把把emacs的C开发环境鼓捣出来,首先上图:

1.color theme

color-theme.el

;; color theme
(require ‘color-theme)
(color-theme-classic)

2.line number

我用的是wp-line-number

参考网站:http://homepage1.nifty.com/blankspace/emacs/elisp.html

;;line number
(set-scroll-bar-mode nil) ;no scroll bar
(require ‘wb-line-number)
(wb-line-number-toggle)

3.cedet(Collection of Emacs Development Environment Tools)

这是个非常强大的东东,专门为emacs开发的用于开发环境的工具

参考地址:http://cedet.sourceforge.net/

;; Load CEDET
(load-file “/usr/share/emacs/site-lisp/cedet/common/cedet.el”)

;; Enabling various SEMANTIC minor modes.  See semantic/INSTALL for more ideas.
;; Select one of the following:

;; * This enables the database and idle reparse engines
;;(semantic-load-enable-minimum-features)

;; * This enables some tools useful for coding, such as summary mode
;;   imenu support, and the semantic navigator
(semantic-load-enable-code-helpers)

;; * This enables even more coding tools such as the nascent intellisense mode
;;   decoration mode, and stickyfunc mode (plus regular code helpers)
;; (semantic-load-enable-guady-code-helpers)

;; * This turns on which-func support (Plus all other code helpers)
;; (semantic-load-enable-excessive-code-helpers)

;; This turns on modes that aid in grammar writing and semantic tool
;; development.  It does not enable any other features such as code
;; helpers above.
;; (semantic-load-enable-semantic-debugging-helpers)

;;speedbar
(global-set-key [(f4)] ’speedbar-get-focus)

;;ia-complete
(define-key c-mode-base-map [(meta ?/)] ’semantic-ia-complete-symbol-menu)

4.ecb(emacs code browser)

阅读代码的,也是个非常强大的工具,跟cedet绝配.

参考地址:http://ecb.sourceforge.net/

;;ecb
(add-to-list ‘load-path “/usr/share/emacs/site-lisp/ecb”)
(require ‘ecb)
(setq ecb-layout-name “right1″)
(setq ecb-tip-of-the-day nil);switch off tip of the day
;;(ecb-activate)

5.gdb

调试工具,相比大家都听说过.

我喜欢调成多个窗口,什么stack,break point等等

参考地址:http://www.inet.net.nz/~nickrob/

;;gdb
(add-to-list ‘load-path “/usr/share/emacs/site-lisp/gdb”)
(load-library “multi-gud.el”)
(load-library “multi-gdb-ui.el”)
(setq gdb-many-windows t)

Tagged with:
18

《算法导论》p75的最后一段话讲到:

当MAX-HEAPIFY作用在一棵以结点i为根的、大小为n的子树上时……………i结点的子树大小至多为2n/3(最坏情况发生在最低层恰好半满的时候)……….

为什么子树大小至多为2n/3?
思索N久:
n0+n1+n2=n    (1)
n1+2n2+1=n    (2)
由(1),(2)得,
n0=n2+1,因为二叉堆是完全二叉树,且最后一层半满,即n1=0,得:
n0=(n+1)/2
则最后一层为x,倒数第二层的叶子结点为x/2:
x+x/2=n0=>x=(n+1)/3
所以子树大小至多为
(x+n-1)/2=((n+1)/3+n-1)/2=(2n-1)/3<=2n/3

Tagged with:
preload preload preload