《Data Structure and Algorithm with Python》

Chapter 0 Introduce

这片文章主要是对《Data Structure and Algorithm with Python》这一本书学习是的笔记或者一些感悟,目前由于时间比较有限,笔记应该会比较的简单,而且目前也是第一次学习的状态,不会有特别多深入的理解,不过会在不断的学习后不断的更新。也希望大家如果有什么不一样的理解,尽管分享给我。

Chapter 1 Basic Knowledge

该章简单的介绍了一下数据类型、方法、对象以及读写文件的方法。

1.1 数据类型

 int, float, str, list, dict

数据类型的基本表达形式

1.2 数据类型的转换

数据类型的转换

1.3 Methods

主要介绍了2种方法:
Accessor methods: 是可以访问当前数据但不改变它的方法。
Mutator methods: 是可以改变当前数据的方法。

1.4 Class

构造函数(Constructor):通过在对象本身中放入数据的引用来对对象实例化。
Self:特殊变量self总是指向当前的对象,而且必须放在每个方法的第一个参数

1.5 Operator Overloading

有些情况下需要对操作符进行重新的定义,来实现不同的功能。通常x的类型来决定调用的operate method。
比如说,对于“+”操作,当要进行整数的相加时,x的输入为int类型,这时就会调用整数相加的operate method;当要进行string拼接时,x的输入为str类型,这时就会调用string拼接的operate method。
operate method

1.6 Reading File

(Read single line from a file. Use for-loop)

  1. open file
  2. using forloop to get each line in the file
  3. file.close()

(Read multi-line from a file. Use while-loop)

  1. Open file
  2. Read the first line
  3. While-loop to determine the ending
  4. If-elif which contain some process
  5. file.close()

1.7 Details for me

  1. 在读取文件内容是,readline()是读一行,readlines()是读多行。
  2. 向list中添加元素:
    list = list + [item] / list.append(item)
    (前者时间复杂度高于后者)
  3. A Container Class(类容器):
    一般来说,是用来装实例化的类的容器,该容器也是一个类,包含(list||dict)以及一个迭代器(iter),并通过yield来对容器中的每个元素进行迭代。

1.8 Questions at the end of chapter1

• What two parts are needed for the accumulator pattern?
append & iteration

• When do you need to use the loop and a half pattern for reading from a file?
single line for for-loop & multi-line for the loop and a half

• What is the purpose of a class definition?
to give a special type of object some features or be a container

• What is an object and how do we create one?
an object is containing its data and method operating on its data.
using constructor and reference of data

• What is a mutator method(更改器法)?
是可以改变当前数据的方法

• What is an accessor method?
是可以访问当前数据但不改变它的方法

Chapter 2 Algorithm Analysis

这一章我准备结合《算法(第4版)》中的算法分析部分进行总结,不过应该是在学完《Data Structure and Algorithm with Python》这本书大部分内容之后进行。

/n
/n

Chapter 3 Recursion

“Some say that writing recursive functions is a declarative approach rather than an imperative approach. ”

虽然标题为递归,但是其实讲了很多程序的执行过程,也算是对基础概念的补充以及为后续算法做准备。该章首先对不同的域进行了区分,然后简单的介绍了一下Run-Time Stack和Heap是如何工作的,最后讲解了如何写递归函数,并简单提了一下Reflection。
(其实里面有一些东西还是有一点模糊,可能我理解能力有限,如果后续接触到相关内容的资料或书籍,我会进行补充,如果有什么错误或不同见解的地方,欢迎大家在评论区指正和分享)

3.1 Scope

Scope of identifier:Local, Enclosing, Global, Build-In.

  1. Local scope: 计算机当前正在执行的函数的作用域
  2. Enclosing scope: 这里应该是Local scope 向外一层的函数作用域(or作用域)。(封包似乎在python和javaScript中有不同的理解。)
  3. Global scope: 全局作用域是最外层的作用域,将函数作用域包含在内。
  4. Build-In scope: 内置作用域,一般我们是不能进行操作,但会存在一些内置的identifier可以让我们调用。

3.2 Run-Time Stack & Heap

Python splits the RAM up into two parts called the Run-time Stack and the Heap

  1. Run-Time Stack:当一个函数被调用时,Python的解释器会向run-time stack内送入一个活动记录(这个activation record包含着所有的在新的local scope中定义的变量);当函数返回时,解释器会将活动记录弹出。
  2. Heap:是存放对象的地方。对对象的引用储存于run-time stack中,而这些引用都是指向heap的。

3.3 Recursion

  1. write base case which is a if statement that handles a very simple case(like Mathematical induction)
  2. write recursive case to achieve recursion
  3. return a result if we need
# some example of recursion
def reverse(seq):
    # if str is empty, then emptype() will return an empty sequence
    seqtype = type(seq)
    emptype = seqtype()
    # Base case
    if seq == emptype:
        return seq

    # Recursive case
    str1 = reverse(seq[1:])
    # Return
    return str1 + seq[0:1]


def reverseS(str):
    def revIndex(index):
        if index == -1:
            return ""
        str1 = revIndex(index-1)
        return str[index] + str1
    return revIndex(len(str)-1)



def main():
    A = '123456789'
    B = [1, 2, 3, 4]
    print(reverse(A))
    print(reverse(B))
    print(reverseS(A))


if __name__ == "__main__":
    main()

3.4 Reflection

Reflection 是指能够检查对象的属性(type)的能力,即对不同的数据类型,进行不同的处理,类似于Operator Overloading.

3.5 Questions

  • How does Python determine the meaning of an identifier in a program?
    在域中查找,查找的顺序为:Local scope->Enclosing scope->Global scope->Build-In scope
  • What happens to the run-time stack when a function is called?
    解释器会向该stack送入一个活动记录
  • What happens to the run-time stack when a function returns from a call?
    会将之前送入的活动记录弹出来
  • What are the two important parts to a recursive function and which part comes
    first?
    base case and recursive case
  • Exactly what happens when a return statement is executed?
    会返回一个确定变量(比如一个字符串or一个整数之类的)
  • Why should we write recursive functions?
    cuz it can solve some problems in a very simple way.
  • What are the computational complexities of various recursive functions?
    O(n)(盲猜,因为在求累加时,近似于斜率为a的直线)

Chapter 4 Sequence

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值