Python Linked List
上周日教导一个科班非技术的朋友学习 Python 编程。他的 Python 水平大概就是看了几篇短的 Python 介绍博客、会流程控制和全局函数编写。
具体教导思路是从自己实现一个链表出发,研究学习 Python 数据结构、接口、算法的实现和运用、然后:
0. 学会画图表达对象之间的关联、数据结构的操作、并实现它。
- 慢慢用 Python 的特性去优化链表、学习 Python 特性与最佳实践;
- 刷 Leetcode 链表题目、锻炼思维;
- 熟悉之后、在进行二叉排序树的 0、1、2。
昨天算是实现了一个带遍历、插入、删除的 LinkedList。但是写的很长、大概用到的“新”特性只有 Python 的类。
今天打算让他快速前进,尝试拔苗助长ing:
- 理解成员函数的self、了解函数的默认参数;
- 理解抛出异常的代码写法和运行现象;
- 还有再学习一下类的特殊方法
__len__和__str__; - 理解鸭子类型:
- Python 有个逻辑:不管你是什么动物,会嘎嘎叫的就是鸭子;
- 你想想看 ListLink.head 和 Node.next 是不是一个东西?
- 可不可以起成同一个名字 把它当成嘎嘎叫方法 (next),从而简化我们的代码;
- 如果把 ListLink 当作鸭子类,那么Node也是一个鸭子类;
- 关键:Node 和 ListLink 一样,也有 next 属性;两者的 next 属性的性质一样,要么是 None,要么是一个有 data 有 next 的对象;
- 另一种理解:链表第一个结点之后的部分还是一个完整的链表。
- 理解函数是一等公民 -- 下面代码还没实现、因为已经有
__iter__了没有必要了。
个人感觉4讲早了,但是看他写的又长又臭,忍不住不教。
代码:
class Stack:
class Node:
def __init__(self, data, next = None):
self.data = data
self.next = next
def __init__(self):
self.next = None
def push(self, data = None, index = 0):
if index < 0 or index > len(self):
raise IndexError("Given index is invalid.")
cur = self
for i in range(index):
cur = cur.next
cur.next = Node(data, cur.next)
def pop(self, index = 0):
if index < 0 or index >= len(self):
raise IndexError("Given index is invalid.")
cur = self
for i in range(index):
cur = cur.next
retval = cur.next.data
cur.next = cur.next.next
return retval
def __iter__(self):
cur = self
while cur.next is not None:
yield cur.next.data
cur = cur.next
def __len__(self):
len = 0
cur = self
while cur.next is not None:
len = len + 1
cur = cur.next
return len
def __str__(self):
cur = self
if cur.next is None:
return "[]"
st = "["
while cur.next.next is not None:
st = st + str(cur.next.data) + ", "
cur = cur.next
st = st + str(cur.next.data) + "]"
return st
测试:
s = Stack()
print(s)
s.push(1)
print(s)
s.push(0.5)
print(s)
s.push(1.5, 1)
print(s)
s.push(2, 3)
print(s)
print(s.pop())
print(s)
print(s.pop(2))
print(s)
输出:
[]
[1]
[0.5, 1]
[0.5, 1.5, 1]
[0.5, 1.5, 1, 2]
0.5
[1.5, 1, 2]
2
[1.5, 1]
Python Linked List的更多相关文章
- LeetCode with Python -> Linked List
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- python 实现单链表
#! /usr/bin/env python ### ### Linked List python implementation ### ### @reference Data Structures ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- [LeetCode]题解(python):092 Reverse Linked List II
题目来源 https://leetcode.com/problems/reverse-linked-list-ii/ Reverse a linked list from position m to ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 【LeetCode】237. Delete Node in a Linked List 解题报告 (Java&Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 设置当前节点的值为下一个 日期 [LeetCode] ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
随机推荐
- codeforces 696B B. Puzzles(树形dp+概率)
题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Ntrip通讯协议1.0
Ntrip通讯协议1.0 1 什么是Ntrip? CORS(Continuously Operating Reference Stations)就是网络基准站,通过网络收发GPS差分数据.用户访问CO ...
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- 【POJ 3580】 SuperMemo
[题目链接] 点击打开链接 [算法] 本题也是Splay区间操作的模板题,不过要比BZOJ 3223要稍微复杂一些,做完此题后,我终于对Splay有了更深入的理解,有“拨开云雾见青天”的感觉 本题还是 ...
- 利用PyCharm的Profile工具进行Python性能分析
Profile:PyCharm提供了性能分析工具Run->Profile,如下图所示.利用Profile工具可以对代码进行性能分析,找出瓶颈所在. 测试:下面以一段测试代码来说明如何使用pych ...
- Servlet3.0之九:web模块化
一.使用web-fragment.xml 在Servlet 3.0中,可以使用标注来设置Servlet的相关信息.实际上,Web容器并不仅读取/WEB-INF/classes中的Servlet标注信息 ...
- 出现ImportError: No module named win32api异常
ImportError: No module named win32api出现异常 实际是需要安装和自己python兼容的win32all 在http://starship.python.net/cr ...
- HTTP错误code大全
100 - Continue 101 - Switching Protocols Top Success Codes 200 - OK 201 - Created 202 - Accepted 203 ...
- WebService安全解决方案—简单握手协议
具体方案如下图: 2.解决方案分析 A.SiteA每次向SiteB发送的请求参数都不一样,造成伪造者难以模仿和推敲里面的算法过程. B.伪造者获得了SayHelloRequest的数据,它向SiteB ...
- notepad++的NppFTP插件远程连接linux操作系统
1.首先要有NppFTP插件,如果没有可以去下面链接或者其他网站下载: https://sourceforge.net/projects/nppftp/files/latest/download ...