Course Notes:CS662

From Xi's Knowledge Base

Jump to: navigation, search
Solar Vistas Notebook>>Course Notes>>CS662 Course Notes - '''CS662 ''Artificial Intelligence Programming''''' * Tutor: Brooks C. * Time: ==AI Programming== === Some Application=== ===Agent=== *| Agent metaphor *| Agent to be open-ended ?| A Markov chain is episodic or sequential? ==Wikipeida== ==Python== ===Tips=== ===='''and-or'''==== '''''and''''' and '''''or''''' return the '''''value''''' of one of the expressions they compared not simple boolean. =====1. and===== >>> 'a' and 'b' 1 'b' >>> '' and 'b' 2 '' >>> 'a' and 'b' and 'c' 3 'c' #When use '''''and''''', it calculate bool value from ''left'' to ''right''. ''0, ' ', [], (), {}, None'' are considered ''False''; Anything else will be ''True''. #If one of the expression is False, then '''''and''''' return the first ''False'' value. #If all the expression is ''True'', '''''and'''''return the last ''True'' value. =====2. or===== >>> 'a' or 'b' 1 'a' >>> '' or 'b' 2 'b' >>> '' or [] or {} 3 {} >>> def sidefx(): ... print "in sidefx()" ... return 1 >>> 'a' or sidefx() 4 'a' #When use '''''or''''', it calculate bool value from ''left'' to ''right'' like '''''and'''''. #If one of the expression is ''True'', then '''''or''''' return the first ''True'' value. #If all the expression is ''False'', '''''or'''''return the last ''False'' value. =====3. C-style '''''bool ? a : b''''' expression===== >>> a = "first" >>> b = "second" >>> 1 and a or b 1 'first' >>> 0 and a or b 2 'second' #这个语法看起来类似于 C 语言中的 bool ? a : b 表达式。整个表达式从左到右进行演算,所以先进行 and 表达式的演算。 1 and 'first' 演算值为 'first',然后 'first' or 'second' 的演算值为 'first'。 #0 and 'first' 演算值为 False,然后 0 or 'second' 演算值为 'second'。 然而,由于这种 Python 表达式单单只是进行布尔逻辑运算,并不是语言的特定构成,这是 and-or 技巧和 C 语言中的 bool ? a : b 语法非常重要的不同。如果 a 为假,表达式就不会按你期望的那样工作了。(你能知道我被这个问题折腾过吗?不只一次?) =====4. DO NOT USE===== >>> a = "" >>> b = "second" >>> 1 and a or b 1 'second' #由于 a 是一个空字符串,在 Python 的布尔上下文中空字符串被认为是假的,1 and '' 的演算值为 '',最后 '' or 'second' 的演算值为 'second'。噢!这个值并不是你想要的。 and-or 技巧,bool and a or b 表达式,当 a 在布尔上下文中的值为假时,不会像 C 语言表达式 bool ? a : b 那样工作。 在 and-or 技巧后面真正的技巧是,确保 a 的值决不会为假。最常用的方式是使 a 成为 [a] 、 b 成为 [b],然后使用返回值列表的第一个元素,应该是 a 或 b中的某一个。 =====5. Safe use===== >>> a = "" >>> b = "second" >>> (1 and [a] or [b])[0] 1 '' #由于 [a] 是一个非空列表,所以它决不会为假。即使 a 是 0 或者 '' 或者其它假值,列表 [a] 也为真,因为它有一个元素。 到现在为止,这个技巧可能看上去问题超过了它的价值。毕竟,使用 if 语句可以完成相同的事情,那为什么要经历这些麻烦事呢?哦,在很多情况下,你要在两个常量值中进行选择,由于你知道 a 的值总是为真,所以你可以使用这种较为简单的语法而且不用担心。对于使用更为复杂的安全形式,依然有很好的理由要求这样做。例如,在 Python 语言的某些情况下 if 语句是不允许使用的,比如在 lambda 函数中。 注:这里由于语句是从左向右解释的。我们可以这样的来理解。 and语句: 0and* 不需要再考虑*是0还是1,结果是0 1and* 需要考虑*是0还是1来决定结果。 1or* 不需要考虑后面的*,结果为1 0or* 需要考虑后面的*来决定结果 example: ''(expression 1)'' and ''(expression 2)'' or ''(expression 3)'' if ''expression 1'' is '''''true''''' then the '''value''' is ''expression 2'', else the '''value''' will be ''expression 3'' ===List Comprehensions=== ====Some cool list comprehension tricks:==== params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} [k for k, v in params.items()] [(v,k) for k,v in params.items()] ["%s=%s" % (k, v) for k, v in params.items()] [x + y / 2 for (x,y) in zip([1,2,3,4], [5,6,7,8])] thenewlist = [x for x in theoldlist if x > 5] ====Class variables==== =====Hesitance===== ====Hello==== ==Extra Informations== ====AT&T Automatic Telephone Custom Service Director==== ==Assignment== ====Assignment 1 - Intro. (Due 9/4)==== ====Assignment 2 - Search. (Due 9/13)==== ====Assignment 3 - Focused Crawling. (Due 9/20)====
Personal tools