《Problem Solving with Algorithms and Data Structures using python》 的学习笔记和课后作业(一)
包含本书第一章,第二章。
这本书的 豆瓣评分 高达9.3,python作为接近算法伪码的一种脚本语言,其实用它写算法是极好的,可以将注意力集中在算法本身。
但是由于python性能的问题,用python写算法不算太主流,因此市面上介绍算法的书也多以使用c/c++或者Java居多。
这本书几乎是用python介绍算法豆瓣评分最高的一本书了,网上可以下到pdf,但最好的阅读方式是直接使用这本书的 网站 。这本书貌似也是拿ipython notebook写的,还可以直接在网站上运行示例程序。self check的部分还有作者视频讲解,这才是编程类书籍的未来嘛!
这里我记录每章的学习笔记,同时记录每章课后作业的个人解决代码,统一使用python3。
这章主要给python做了个简短介绍,魔术方法部分以前没有仔细学习过,看看感觉挺好用,可以让自定义的方法看起来像内建的方法。
python 官方文档关于operator的表格
Operation Syntax Function Addition a + b add(a, b) Concatenation seq1 + seq2 concat(seq1, seq2) Containment Test obj in seq contains(seq, obj) Division a / b div(a, b) (without __future__.division) Division a / b truediv(a, b) (with __future__.division) Division a // b floordiv(a, b) Bitwise And a & b and_(a, b) Bitwise Exclusive Or a ^ b xor(a, b) Bitwise Inversion ~ a invert(a) Bitwise Or a $ $ b or_(a, b) Exponentiation a ** b pow(a, b) Identity a is b is_(a, b) Identity a is not b is_not(a, b) Indexed Assignment obj[k] = v setitem(obj, k, v) Indexed Deletion del obj[k] delitem(obj, k) Indexing obj[k] getitem(obj, k) Left Shift a<<b lshift(a, b) Modulo a % b mod(a, b) Multiplication a * b mul(a, b) Negation (Arithmetic) - a neg(a) Negation (Logical) not a not_(a) Positive + a pos(a) Right Shift a >> b rshift(a, b) Sequence Repetition seq * i repeat(seq, i) Slice Assignment seq[i:j] = values setitem(seq, slice(i, j), values) Slice Deletion del seq[i:j] delitem(seq, slice(i, j)) Slicing seq[i:j] getitem(seq, slice(i, j)) String Formatting s % obj mod(s, obj) Subtraction a - b sub(a, b) Truth Test obj truth(obj) Ordering a<b lt(a, b) Ordering a<= b le(a, b) Equality a == b eq(a, b) Difference a != b ne(a, b) Ordering a >= b ge(a, b) Ordering a > b gt(a, b)两个不错的学习链接:
PYTHON-进阶-魔术方法小结(方法运算符重载)
Python 魔术方法指南
作业链接
暂时完成q1~q12
In[1]: def gcd(m,n): """求两个数最大公因数""" while m%n != 0: oldm = m oldn = n m = oldn n = oldm%oldn return n class Fraction: """处理分数的类""" def __init__(self,top,bottom): # q5 检查分子分母是否都是整数 if not isinstance(top, int) or not isinstance(bottom, int): raise Exception("top or bottom of Fraction is not int type!") # q2 初始化时直接约分 common = gcd(top,bottom) self.num = top//common self.den = bottom//common def __str__(self): return str(self.num)+"/"+str(self.den) def show(self): print(self.num,"/",self.den) def __add__(self,other): # q2 newnum = self.num*other.den + \ self.den*other.num newden = self.den * other.den return Fraction(newnum,newden) def __eq__(self, other): firstnum = self.num * other.den secondnum = other.num * self.den return firstnum == secondnum # q1 def getNum(self): return self.num def getDen(self): return self.den # q3 def __sub__(self, other): newnum = self.num*other.den - \ other.num*self.den newden = self.den * other.den return Fraction(newnum, newden) def __mul__(self, other): return Fraction(self.num*other.num, self.den*other.den) def __truediv__(self, other): return Fraction(self.num*other.den, self.den*other.num) # q4 def __gt__(self, other): if self.num*other.den > self.den*other.num: return True else: return False def __ge__(self, other): if self.num*other.den >= self.den*other.num: return True else: return False def __lt__(self, other): if self.num*other.den < self.den*other.num: return True else: return False def __le__(self, other): if self.num*other.den < self.den*other.num: return True else: return False def __ne__(self, other): if self.num*other.den != self.den*other.num: return True else: return False # q7 def __radd__(self,other_int): """ Python will first try (4).__add__(myobj), and if that returns NotImplemented Python will check if the right-hand operand implements __radd__, and if it does, it will call myobj.__radd__(4) rather than raising a TypeError. """ newnum = self.num + \ self.den*other_int return Fraction(newnum,self.den) # q8 def __iadd__(self, other): """a = iadd(a, b) is equivalent to a += b.""" newnum = self.num*other.den + \ self.den*other.num newden = self.den * other.den return Fraction(newnum, newden) # q9 def __repr__(self): """ In short, the goal of